Solved

Script to rename certain files in specified paths

Posted on 2007-03-18
8
183 Views
Last Modified: 2013-11-25
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



0
Comment
Question by:robjeeves
  • 4
  • 3
8 Comments
 
LVL 70

Assisted Solution

by:KCTS
KCTS earned 100 total points
Comment Utility
There is a little free utility called PFrank which I think will do the trick. See http://www3.telus.net/pfrank/
0
 
LVL 9

Author Comment

by:robjeeves
Comment Utility
Sweet.  That looks promising. I'll have a play with that.

Rob
0
 
LVL 38

Expert Comment

by:Shift-3
Comment Utility
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
0
 
LVL 9

Author Comment

by:robjeeves
Comment Utility
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
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 38

Expert Comment

by:Shift-3
Comment Utility
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
0
 
LVL 9

Author Comment

by:robjeeves
Comment Utility
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
0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 400 total points
Comment Utility
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=1" %%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\%1 %file%.pdf" "%newdate% %file%.pdf"
goto :eof

:_end
endlocal
0
 
LVL 9

Author Comment

by:robjeeves
Comment Utility
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now