Page 1 of 1
Rename the PDF file
Posted: 08 Jul 2011 03:51
by kumar_kondapalli
Hi ,
I have written a batch file which Appends the date & time created . But It appends for all the files in a folder.
To be brief.
I have created a Batch file which appends the date for the .pdf in a folder. For ex : I have test.pdf in a folder c:\test\test.pdf. Once i ran the bat file it changes to (test_TestProcess_Fri-07-08-2011_15.16.18PM.pdf). It works perfectly. But next time when i have test2.pdf in the same folder it modifies Test.pdf and Test2.pdf in the below format
1) test_TestProcess_Fri-07-08-2011_15.16.18PM_TestProcess_Fri-07-08-2011_15.18.25PM.pdf
2) test2_TestProcess_Fri-07-08-2011_15.18.25PM.pdf.
I want the bat file to append date & time for each file and not to change for the previous files which it got appended previously. Can any one help me on the same.
Below is my .Bat file code.
@echo on
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
REM -- Directory details
SET CURR_DATE=%Year%%Month%%Day%%DayW%
SET CURR_TIMESTAMP=%All%_%Allm%%AMPM%
SET CURR_TIMESTAMP1=%Year%%Month%%Day%-%Hour%%Min%%Sec%
SET PROJECT_NAME=TestProcess
echo %CURR_TIMESTAMP%
for /f "tokens=*" %%a in ('dir /b /a-d *.pdf 2^>NUL') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Re: Rename the PDF file
Posted: 08 Jul 2011 06:19
by dbenham
This should be fairly straight forward using regular expressions. But the regular expression capabilities of FINDSTR are primitive, so the solution is more verbose and less precise than I had hoped. Also the FINDSTR has a limit to the size of the search string, so I had to break the regular expression into two searches, which further limits the precision. (I never knew about the size limit until I started working with your problem.)
Still, this should work for you.
I paramatized the extension name in case you want to use this batch for files other than PDF.
The regular expressions have been tested, but the incorporation into your script has not been tested. The following code should replace the last 3 lines of your code:
Code: Select all
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>NUL dir /b /a-d *.%ext%|findstr /r /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-9][0-9]_"|findstr /ri /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
You probably should include SETLOCAL at the top after @ECHO OFF so that you don't litter your environment with leftover definitions.
Dave Benham
Re: Rename the PDF file
Posted: 08 Jul 2011 06:48
by kumar_kondapalli
Can you please advise...
I have put the code in last 3 lines and it is not working . Also I think you are specifying the path of c:\ in the code right??
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>NUL dir /b /a-d *.%ext%|findstr /r /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-9][0-9]_"|findstr /ri /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Steps i have tried is ....
1) Replaced the last 3 lines of your code in to the bat file .
2) Created a folder (Test123folder) in Drive c:\
3) Now in folder Test123folder i have one pdf file as well as the bat file
4) Run the Bat file and nothing happens
Any help and suggestion is highly appreciated.
Thanks,
Santosh Kumar
Re: Rename the PDF file
Posted: 08 Jul 2011 06:57
by kumar_kondapalli
Dave ,
Also just a clarification will it depends up on my .pdf size?.
Thanks,
San
Re: Rename the PDF file
Posted: 08 Jul 2011 11:11
by dbenham
File size should not matter.
There is no reference to the c: drive. It will look for the files in your current directory, just like your original code. The
/c: syntax is an option to the FINDSTR command signifying what follows is a search string.
There were two bugs:
1) I never tested the commands in the context of a FOR loop and I forgot the pipe | characters needed to be escaped like you escaped the > with your stderr redirection.
2) My original code was only including files that already had the timestamp in the filename instead of filtering out such files. I fixed this by adding the /v option to both FINDSTR commands.
Here is the corrected code:
Code: Select all
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>nul dir /b /a-d *.%ext%^|findstr /rv /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-9][0-9]_"^|findstr /rvi /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Hopefully everything works now
Dave Benham
Re: Rename the PDF file
Posted: 09 Jul 2011 03:55
by kumar_kondapalli
Dave,
Thanks a lot and it works perfectly
. Hope it will be very useful for other folks in this forum....
Ton Cheers to Dave Benham.. Guys ...
I have another question ie
Can we use a schedular to run this bat file. Ie I want the above bat file to run on every week/every month on Sunday... Can this is possible ?
PS : Can we call one bat file from another bat file??.
Kindly please advise...
Thanks,
San
Re: Rename the PDF file
Posted: 11 Jul 2011 04:17
by shiva
hai,
i have changed some of the things in the batch file that was given,
it was working fine, i have verified even,
the batch file is:
@echo on
set filename=%1
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
@For /F "tokens=1,2,3 delims=:,. " %%A in ('echo %time%') do @(
Set Hour=%%A
Set Min=%%B
Set Sec=%%C
Set Allm=%%A.%%B.%%C
)
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
REM -- Directory details
SET CURR_DATE=%Year%%Month%%Day%%DayW%
SET CURR_TIMESTAMP=%All%_%Allm%%AMPM%
SET CURR_TIMESTAMP1=%Year%%Month%%Day%-%Hour%%Min%%Sec%
SET PROJECT_NAME=TestProcess
echo %CURR_TIMESTAMP%
for /f "tokens=*" %%a in ('dir /b /a-d %filename% 2^>NUL') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
rem end of file
save as ren.bat
and call as ren.bat a.pdf
this will change the file name to a_TestProcess_Mon-07-11-2011_15.42.47PM.pdf,
thank you,
shiva
Re: Rename the PDF file
Posted: 14 Jul 2011 03:30
by kumar_kondapalli
Hey thanks for all your help
...
Can you please let me know how to display the time in 12hr format?
Thanks,
San
Re: Rename the PDF file
Posted: 15 Jul 2011 08:48
by kumar_kondapalli
Hi All,
I have modified the below batch file to display in 12hr format but it gives me the below error can any one help me please..
Error is as follows:
Fri-07-15-2011_08:11-PM
A duplicate file name exists, or the file
cannot be found.
"Duplicate file name exists"
Can you please help??
@echo off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
for /f "tokens=1 delims=: " %%h in ('time /T') do set hour=%%h
for /f "tokens=2 delims=: " %%m in ('time /T') do set minutes=%%m
for /f "tokens=3 delims=: " %%a in ('time /T') do set ampm=%%a
set NOW=%hour%:%minutes%-%ampm%
@For /F "tokens=3 delims=: " %%A in ('time /t ') do @(
Set AMPM=%%A
)
REM -- Directory details
SET CURR_DATE=%Year%%Month%%Day%%DayW%
SET CURR_TIMESTAMP=%All%_%NOW%
SET CURR_TIMESTAMP1=%Year%%Month%%Day%-%Hour%%Min%%Sec%
SET PROJECT_NAME=TestProcess
echo %CURR_TIMESTAMP%
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>nul dir /b /a-d *.%ext%^|findstr /rv /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-
9][0-9]_"^|findstr /rvi /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Re: Rename the PDF file
Posted: 15 Jul 2011 09:28
by kumar_kondapalli
Special thanks to every one
... I made it working ... Please find the below code to display in 12 hr format
@echo off
@For /F "tokens=1,2,3,4 delims=/ " %%A in ('Date /t') do @(
Set DayW=%%A
Set Day=%%B
Set Month=%%C
Set Year=%%D
Set All=%%A-%%B-%%C-%%D
)
for /f "tokens=1 delims=: " %%h in ('time /T') do set hour=%%h
for /f "tokens=2 delims=: " %%m in ('time /T') do set minutes=%%m
for /f "tokens=3 delims=: " %%a in ('time /T') do set ampm=%%a
set NOW=%hour%_%minutes%_%ampm%
REM -- Directory details
SET CURR_DATE=%Year%%Month%%Day%%DayW%
SET CURR_TIMESTAMP=%All%_%NOW%
SET CURR_TIMESTAMP2=%All%
SET CURR_TIMESTAMP1=%Year%%Month%%Day%-%Hour%%Min%%Sec%
SET PROJECT_NAME=TestProcess
echo %CURR_TIMESTAMP%
set "ext=pdf"
for /f "tokens=*" %%a in ('2^>nul dir /b /a-d *.%ext%^|findstr /rv /c:"_[SMTWF][uoehra][neduit]-[01][0-9]-[0-3][0-9]-20[0-
9][0-9]_"^|findstr /rvi /c:"_[012][0-9]\.[0-5][0-9]\.[0-5][0-9][AP]M\.%ext%$"') do (
echo %%a
echo %%~na
echo %%~xa
ren """%%a""" """%%~na"""_%PROJECT_NAME%_%CURR_TIMESTAMP%%%~xa
)
Thanks,
San