开发者

Rename and renumber files in batch

开发者 https://www.devze.com 2023-04-13 02:05 出处:网络
(PHP>=5) * fake excuse: I\'m not a programmer, I\'m just learnin开发者_如何学Pythong... I\'ve read some files in my custom \'/img\' directory:

(PHP>=5) * fake excuse: I'm not a programmer, I'm just learnin开发者_如何学Pythong...

I've read some files in my custom '/img' directory:

  • 0.jpg
  • 1.jpg
  • 2.jpg
  • ....jpg
  • 10.jpg

So I got an array();

I need to rename each item in array according to their ID's known by jQuery (all file attributes are set in HTML table cells, each having ids and classes as needed). But the most disturbing thing that I'm not able to avoid file overwrites.

E.g. I rename simultaneously both 1.jpg and 3.jpg (Using jQuery I've reassigned IDs (and new filenames where built and passed to form): 1-st file got ID=3, 3-rd file got ID=1 )

  • 1.jpg -> 3.jpg
  • ...
  • AND
  • ...
  • 3.jpg -> 1.jpg

You'll see where is a trap.

So, the question is: Should I make a '/temp' directory to avoid overwrites and to write to it, so to rename() or mv() them later? Or there are much more transparent and faster ways to do the same?


There are several ways you can do it:

  • Move 3.jpg to temp-dir
  • Rename 1.jpg to 3.jpg
  • Rename 3.jpg to 1.jpg
  • Move temp/1.jpg back to the folder

  • Rename 3.jpg to 3.jpg~
  • Rename 1.jpg to 3.jpg
  • Rename 3.jpg~ to 1.jpg

  • Load 3.jpg in memory
  • Rename 1.jpg to 3.jpg
  • Create a file 1.jpg with the contents of the old 3.jpg (stored in your memory)

There are probably more ways, but I think these are the best. Hope it's helpful :)

-Edit- I think the last one is the fastest, but it consumes the most memory. If you don't like that, I would use the second one. But that's your choice :)


This package can rename files in directory using several strategies. It can traverse a directory recursively and rename files according to several different types of strategies. Each strategy defines how each file should be renamed or not.

Currently several types of strategies are implemented like transliteration of names in different idioms and prefix names with a time values. Additional strategies may be registered.

The renamed files may be copied to a separate directory. The class can also generate a report in CSV format about the renamed files.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号