Solved

Script to rename certain files in specified paths

Posted on 2007-03-18
8
186 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…

828 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