endy
endy

Reputation: 3872

Bulk renaming files

So I have a series of files with the following format Their real file name followed by the date.

file_1_12DEC2011.pdf
file_2_12DEC2011.pdf
file_3_12DEC2011.pdf
file_4_12DEC2011.pdf
file_5_12DEC2011.pdf

I am trying to figure out How I can Increment the day by one day. For instance, this would be my desired result.

file_1_13DEC2011.pdf
file_2_13DEC2011.pdf
file_3_13DEC2011.pdf
file_4_13DEC2011.pdf
file_5_13DEC2011.pdf

Upvotes: 0

Views: 593

Answers (2)

Aacini
Aacini

Reputation: 67216

Yoy may try this .BATch file:

@echo off
setlocal EnableDelayedExpansion
for %%f in (*.pdf) do (
   set name=%%~Nf
   set day=!name:~-9,2!
   set /A day=1!day! %% 100 + 1
   if !day! lss 10 set day=0!day!
   ECHO ren %%f !name:~0,-9!!day!!name:~-7!
)

Test the file and, if the result is OK, delete the ECHO part in ren command.

This file should work with any name and any number, as long as the last 9 characters of the file name be the date in DDMMMYYYY format as shown above.

Upvotes: 1

kev
kev

Reputation: 161834

To do it safely, echo before mv:

for i in file_[1-5]_12DEC2011.pdf; do
    mv $i ${i/12/13}
done

To do it quickly, use the rename command:

$ rename 's/12/13/' file_[1-5]_12DEC2011.pdf

UPDATE: You can use awk to generate new file name, and put it in those command above.

for i in *_*.pdf; do
    mv $i `echo $i | awk -F_ -v OFS=_ '{sub(/[0-9]+/, $NF+1, $NF)}1'`
done

Upvotes: 1

Related Questions