Solved

Script to rename certain files in specified paths

Posted on 2007-03-18
8
185 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
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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 400 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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