Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to rename certain files in specified paths

Posted on 2007-03-18
8
Medium Priority
?
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 70

Assisted Solution

by:KCTS
KCTS earned 400 total points
ID: 18745219
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
ID: 18745423
Sweet.  That looks promising. I'll have a play with that.

Rob
0
 
LVL 38

Expert Comment

by:Shift-3
ID: 18745858
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Author Comment

by:robjeeves
ID: 18746079
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
 
LVL 38

Expert Comment

by:Shift-3
ID: 18746129
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
ID: 18746142
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 1600 total points
ID: 18746167
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
ID: 18746246
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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