Link to home
Start Free TrialLog in
Avatar of robjeeves
robjeeves

asked on

Script to rename certain files in specified paths

G'day experts

I'd like help with the following

I need will provide a .txt file to be read of numerous paths,

ie
h:\client1\statutory
h:\client2\statutory

The script needs to look for any files in that path that match the following

"Company Statement ****.pdf"

The **** will be a date, as in 2004 or 2005 etc.  The tricky part (for me anyway) is any files it finds in the path that match "Company Statement ****.pdf need to be incremented by one, so 2004 becomes 2005, 2005 becomes 2006 etc.  There may be numerous .pdfs in the path that need changing.

Can this be done? It seems tricky to me but hoping one of you programming experts can save us from some serious manual mouse/keyboard labour :)

Thanks in advance

Rob



SOLUTION
Avatar of Brian Pierce
Brian Pierce
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of robjeeves
robjeeves

ASKER

Sweet.  That looks promising. I'll have a play with that.

Rob
In case that doesn't work, here's a way to do it in a batch file:

@echo off
setlocal

set file=Company Statement

for /F "tokens=*" %%G in (pathlist.txt) do (
 for /F "tokens=3 delims=. " %%H in ('dir "%%G\%file% ????.pdf" /B 2^>NUL') do (
  call :_process %%H "%%G"
 )
)

goto :_end

:_process
set newdate=%1
set /A newdate+=1
ren "%~2\%file% %1.pdf" "%file% %newdate%.pdf"
goto :eof

:_end
endlocal
HI Shift-3

That looks great.  Had a little play and it works with one caveat. If (which most folders do) have

2004 Company Statement
2005 Company Statement

The first rename fails as 2005 Company statement already exists.  I didn't really forse this problem.  Is there any way for the script to perhaps process the highest number it finds (eg 2005 becomes 2006, then 2004 becomes the newly avilable 2005

Thanks for help so far
Ah, I neglected to take that into account.  Sorting the output of the dir command in reverse alphabetical order should fix it.  Try this:

@echo off
setlocal

set file=Company Statement

for /F "tokens=*" %%G in (pathlist.txt) do (
 for /F "tokens=3 delims=. " %%H in ('dir "%%G\%file% ????.pdf" /B /O:-N 2^>NUL') do (
  call :_process %%H "%%G"
 )
)

goto :_end

:_process
set newdate=%1
set /A newdate+=1
ren "%~2\%file% %1.pdf" "%file% %newdate%.pdf"
goto :eof

:_end
endlocal
Thanks for that, could you make one small adjustment for me. I looked at the files and the name is

2007 Company Statement.pdf and not the date last like I originally said. I did try and look at ren "%~2\%file% %1.pdf" "%file% %newdate%.pdf" but I just don't understand it :-/

Thanks

Rob
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome mate

Thanks very much.  I'll do a split points as PFrank also looks good and would probably, given more time also enable this.

Thank you both for a great solution

Rob