Home > Back-end >  How to rename multiple files by increasing a single character in each one?
How to rename multiple files by increasing a single character in each one?

Time:01-25

I want to automate a process to rename multiple files by increasing a single character for their order upon insertion of a new file (in Windows File Explorer, Windows 10). For example, I have a folder with 4 pdfs with the following names:

Tab A - Name_1.pdf
Tab B - Name_2.pdf
Tab C - Name_3.pdf
Tab D - Name_4.pdf

I want to add a pdf to this which is the new tab B. For example, its name is "Tab B - New_Name.pdf". Currently, I need to manually go and rename each file afterwards to reorder them. In this case, the files after the addition need to be as such:

Tab A - Name_1.pdf
Tab B - New_Name.pdf
Tab C - Name_2.pdf
Tab D - Name_3.pdf
Tab E - Name_4.pdf

I needed to manually increase the tab character by one for the last three pdfs. In examples such as this, it doesn't take too long, but sometimes I have hundreds of pdfs going into the AA, BBB, CCCC, etc. ranges. How can this be automated?

I have done some looking into bash options but haven't found anything that is quite what I'm looking for here, especially with the order being with a character rather than an integer.

Thank you!

CodePudding user response:

For windows, check how to use Perl's rename

Or simply download it with Perl dependency.

$ ls -1
new.pdf
'Tab A - Name_1.pdf'
'Tab B - Name_2.pdf'
'Tab C - Name_3.pdf'
'Tab D - Name_4.pdf'

$ rename  '
    sub mytr{ shift; $_ = substr($_, 4, 1); y/B-Y/C-Z/; return " $_ " };
    s@ [A-Z] @ mytr($&)@e
' *.pdf

$ ls -1
new.pdf
'Tab A - Name_1.pdf'
'Tab C - Name_2.pdf'
'Tab D - Name_3.pdf'
'Tab E - Name_4.pdf'

$ mv new.pdf Tab\ B\ -\ Name_1.pdf 

$ ls -1
'Tab A - Name_1.pdf'
'Tab B - Name_1.pdf'
'Tab C - Name_2.pdf'
'Tab D - Name_3.pdf'
'Tab E - Name_4.pdf'
  • Related