katnip999
asked on
Dos Batch Script - File Rename with report names contained within the file
Hello Experts -
I'm looking for a Batch - Dos - File Rename script that will do the following:
1) FIND files in a specified directory that start with the two numbers 00
2) OPEN the file and locate the report title - which is located on the first line - starting at col 43 to col 60. (This contains the name of the report.) Rename the file - with the name of the report found in col 43 through 60.
So for example - I have a TEST directory that contain several file that are named as follows: 005585.txt, 006522.txt, 001244.txt
Once I execute the batch dos batch file - each file will be renamed according to the report name that is contained within the file - ACH Transactions.txt, ATM Time RPT.txt Draft Overdrawn RPT.txt
Thank you for your assistance. ~k
I'm looking for a Batch - Dos - File Rename script that will do the following:
1) FIND files in a specified directory that start with the two numbers 00
2) OPEN the file and locate the report title - which is located on the first line - starting at col 43 to col 60. (This contains the name of the report.) Rename the file - with the name of the report found in col 43 through 60.
So for example - I have a TEST directory that contain several file that are named as follows: 005585.txt, 006522.txt, 001244.txt
Once I execute the batch dos batch file - each file will be renamed according to the report name that is contained within the file - ACH Transactions.txt, ATM Time RPT.txt Draft Overdrawn RPT.txt
Thank you for your assistance. ~k
ASKER
Very Close! But it just re-named one file with "Title.txt".
The "tokens=43-60" that I must re-thought. Cause as is, it will give you the first word of line one as a title.
May I suggest that you test the script in a test folder before you use it in your "real" folder?
Cheers
May I suggest that you test the script in a test folder before you use it in your "real" folder?
Cheers
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes- dragon - you got it. Thank you. Just one more thing. It only does one file. How do you set a loop for this script to go through the directory to rename a dozen or so files in that directory? Thanks again! ~k
Hey Steve, glad you kicked in :)
Since katnip999 used "starting at col 43 to col 60", and his result in testing my unfinnished script was renaming to "Title.txt", could I be possibly assume that his columns are actually words seperated by spaces, instead or caracter position?
Cheers
Since katnip999 used "starting at col 43 to col 60", and his result in testing my unfinnished script was renaming to "Title.txt", could I be possibly assume that his columns are actually words seperated by spaces, instead or caracter position?
Cheers
I just assumed the filename was at those characters in the line... and used numbers to make checking easier thats all.
When I run it katnip999 it I get something like this:
Mon 26/09/2011,23:19:15.75,"c: a\ee\test\ 00aaa.txt" ,"title-of -the-file. "
Mon 26/09/2011,23:19:15.75,"c: a\ee\test\ 00bbb.txt" ,"title-of -the-file. "
Mon 26/09/2011,23:19:15.75,"c: a\ee\test\ 00ccc.txt" ,"title-of -the-file. "
Mon 26/09/2011,23:19:15.75,"c: a\ee\test\ 00ddd.txt" ,"title-of -the-file. "
The renames show fail on last three because I copied the file but didn't change the contents but then editing the first line on those three files and it renamed OK.
Steve
When I run it katnip999 it I get something like this:
Mon 26/09/2011,23:19:15.75,"c:
Mon 26/09/2011,23:19:15.75,"c:
Mon 26/09/2011,23:19:15.75,"c:
Mon 26/09/2011,23:19:15.75,"c:
The renames show fail on last three because I copied the file but didn't change the contents but then editing the first line on those three files and it renamed OK.
Steve
You had still put "title.txt" hard coded in your script ReneGe :-)
This is so funny... I'm glad I put a disclaimer :)
Here is a modified versionthat will create test files, then rename them as you needed.
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET LogFile=%~n0.log
REM CREATING TEST FILES
REM 1234567890123456789012345678901234567890123456789012345678901234567890
REM 0 1 2 3 4 5 6 7
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name 1231soerno rer>00file1.txt
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name 1232soerno rer>00file2.txt
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name 1233soerno rer>00file3.txt
FOR /F "delims=" %%A IN ('DIR /B /S 00*.txt') DO (
ECHO File found:[%%A]
CALL :ReadTitle "%%A"
ECHO Renaming "%%A" to "!NewFileName!"
REN "%%A" "!NewFileName!"
ECHO %date%,%time%,"%%A","!NewFileName!">>"%LogFile%"
)
PAUSE
EXIT /b
:ReadTitle
FOR /F "delims=" %%N IN ('TYPE "%~1"') DO (
SET Title=%%N
SET Title=!title:~42,18!
SET NewFileName=!Title!.txt
EXIT /b
)
ASKER
Well - both scripts are extremely close - but something is still not quite right when I test it. The log output shows that the files name have changed:
Mon 09/26/2011,15:59:42.24,"C: \tmp-dvr\0 03829.txt" ,"LOANS W/ INVALID COL.txt"
Mon 09/26/2011,15:59:42.24,"C: \tmp-dvr\0 03833.txt" ,"FIXED LOANS W/O INDE.txt"
Mon 09/26/2011,15:59:42.24,"C: \tmp-dvr\0 03849.txt" ,"CLK - Visa w/negativ.txt"
However - the file names to do change in the directory. Here is the output of when I run the batch file.
File found:[C:\tmp-dvr\003829.t xt]
Renaming "C:\tmp-dvr\003829.txt" to "LOANS W/ INVALID COL.txt"
The system cannot find the path specified.
File found:[C:\tmp-dvr\003833.t xt]
Renaming "C:\tmp-dvr\003833.txt" to "FIXED LOANS W/O INDE.txt"
The system cannot find the path specified.
File found:[C:\tmp-dvr\003849.t xt]
Renaming "C:\tmp-dvr\003849.txt" to "CLK - Visa w/negativ.txt"
The system cannot find the path specified.
Press any key to continue . . .
And the files are there!
C:\tmp-dvr>dir
Volume in drive C has no label.
Volume Serial Number is 7439-2CDE
Directory of C:\tmp-dvr
09/26/2011 04:05 PM <DIR> .
09/26/2011 04:05 PM <DIR> ..
09/26/2011 11:27 AM 1,002 003829.txt
09/26/2011 11:27 AM 665 003833.txt
09/26/2011 11:28 AM 641 003849.txt
09/26/2011 03:43 PM 489 Copy of rename.bat
09/26/2011 04:06 PM 490 rename.bat
09/26/2011 04:06 PM 948 rename.log
+++++++++++++++++++++
I'm thinking may be it's be cause we are trying to change the name using through the directory path c:/tmp-dvr - rather than changing it at the root directory. May set my temp file "tmp-dvr" as the root directory in the script??
Mon 09/26/2011,15:59:42.24,"C:
Mon 09/26/2011,15:59:42.24,"C:
Mon 09/26/2011,15:59:42.24,"C:
However - the file names to do change in the directory. Here is the output of when I run the batch file.
File found:[C:\tmp-dvr\003829.t
Renaming "C:\tmp-dvr\003829.txt" to "LOANS W/ INVALID COL.txt"
The system cannot find the path specified.
File found:[C:\tmp-dvr\003833.t
Renaming "C:\tmp-dvr\003833.txt" to "FIXED LOANS W/O INDE.txt"
The system cannot find the path specified.
File found:[C:\tmp-dvr\003849.t
Renaming "C:\tmp-dvr\003849.txt" to "CLK - Visa w/negativ.txt"
The system cannot find the path specified.
Press any key to continue . . .
And the files are there!
C:\tmp-dvr>dir
Volume in drive C has no label.
Volume Serial Number is 7439-2CDE
Directory of C:\tmp-dvr
09/26/2011 04:05 PM <DIR> .
09/26/2011 04:05 PM <DIR> ..
09/26/2011 11:27 AM 1,002 003829.txt
09/26/2011 11:27 AM 665 003833.txt
09/26/2011 11:28 AM 641 003849.txt
09/26/2011 03:43 PM 489 Copy of rename.bat
09/26/2011 04:06 PM 490 rename.bat
09/26/2011 04:06 PM 948 rename.log
+++++++++++++++++++++
I'm thinking may be it's be cause we are trying to change the name using through the directory path c:/tmp-dvr - rather than changing it at the root directory. May set my temp file "tmp-dvr" as the root directory in the script??
ASKER
Let me clean up my last question - so it will make sense!
I'm thinking maybe it's because we are trying to change the name using through the directory path c:\tmp-dvr - rather than changing it at the root directory. Maybe if my temp file "tmp-dvr" is set as the root directory in the script - the above error would not occur??
I'm thinking maybe it's because we are trying to change the name using through the directory path c:\tmp-dvr - rather than changing it at the root directory. Maybe if my temp file "tmp-dvr" is set as the root directory in the script - the above error would not occur??
That is because you have "/" in your file name.
Try this
Try this
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET LogFile=%~n0.log
REM CREATING TEST FILES
REM 1234567890123456789012345678901234567890123456789012345678901234567890
REM 0 1 2 3 4 5 6 7
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name /1231soerno rer>00file1.txt
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name /1232soerno rer>00file2.txt
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name /1233soerno rer>00file3.txt
FOR /F "delims=" %%A IN ('DIR /B /S 00*.txt') DO (
ECHO File found:[%%A]
CALL :ReadTitle "%%A"
ECHO Renaming "%%A" to "!NewFileName!"
REN "%%A" "!NewFileName!"
ECHO %date%,%time%,"%%A","!NewFileName!">>"%LogFile%"
)
PAUSE
EXIT /b
:ReadTitle
FOR /F "delims=" %%N IN ('TYPE "%~1"') DO (
SET Title=%%N
SET Title=!title:~42,18!
SET Title=!title:/=!
SET NewFileName=!Title!.txt
EXIT /b
)
Or if you want to have a "-" in the place of "/", change line 29 by: SET Title=!title:/=-!
By the way, it's resolution was a team effort. In the event that I provide the conclusive solution, please equally split points with Steve.
Cheers,
Rene
Cheers,
Rene
One more thing, in lines 8,9 and 10, replace:
...New File Name /1231soerno...
by
...New File Name/1231soerno...
The extra space added was renaming all the files with the same name, so it would not work.
...New File Name /1231soerno...
by
...New File Name/1231soerno...
The extra space added was renaming all the files with the same name, so it would not work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well spotted there Rene!... far too late here, boys all deposited in bed at last and pc shutting down for a couple of hours!
Thanks Steve!
Have yourself a nice rest
Have yourself a nice rest
Hahah, well... if you call bed after 1:30, one boy arriving about 3 and getting out of bed at 6 a rest...
katnip99 - Of course you also need to be careful of other characters, especially the off hand:
\ : | < > & ^ " !
Are these manually entered filenames that could end up having such possibly dodgy characters in?
Have added a quick errorlevel checks to ReneGe's last script and it records the errorlevel and error output from the rename in the logfile too
Steve
katnip99 - Of course you also need to be careful of other characters, especially the off hand:
\ : | < > & ^ " !
Are these manually entered filenames that could end up having such possibly dodgy characters in?
Have added a quick errorlevel checks to ReneGe's last script and it records the errorlevel and error output from the rename in the logfile too
Steve
@ECHO OFF
SETLOCAL enabledelayedexpansion
SET LogFile=%~n0.log
REM CREATING TEST FILES
REM 1234567890123456789012345678901234567890123456789012345678901234567890
REM 0 1 2 3 4 5 6 7
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name/1231soerno rer>00file1.txt
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name/1232soerno rer>00file2.txt
ECHO jwnpgr grgrge rjgejg 9ejg wsejgwejgrwejhh New File Name/1233soerno rer>00file3.txt
FOR /F "delims=" %%A IN ('DIR /B /S 00*.txt') DO (
ECHO File found:[%%A]
CALL :ReadTitle "%%A"
ECHO Renaming "%%A" to "!NewFileName!"
REN "%%A" "!NewFileName!" 2>>"%LOGFILE%"
if Errorlevel 1 echo ** There was an error (%errorlevel%) renaming to "!NewFileName!". See log for error message **
ECHO %date%,%time%,"%%A","!NewFileName!",%errorlevel%>>"%LogFile%"
)
PAUSE
EXIT /b
:ReadTitle
FOR /F "delims=" %%N IN ('TYPE "%~1"') DO (
SET Title=%%N
SET Title=!title:~42,18!
SET Title=!title:/=-!
SET NewFileName=!Title!.txt
EXIT /b
)
@Steve
-I hope you had a good "spasm" of sleep ;)
-I was wondering if something like this could be done:
FOR %%A IN (^\,^:,^|,^<,^>,^&,^^,^",^ !) DO SET Title=!title:%%A=-!
Changed line 18 AND 19 to:
SET ERROR=!Errorlevel!
IF !ERROR! NEQ 0 echo ** There was an error (!ERROR!) renaming to "!NewFileName!". See log for error message **
ECHO %date%,%time%,"%%A","!NewF ileName!", !ERROR!>>" %LogFile%"
Cheers
-I hope you had a good "spasm" of sleep ;)
-I was wondering if something like this could be done:
FOR %%A IN (^\,^:,^|,^<,^>,^&,^^,^",^
Changed line 18 AND 19 to:
SET ERROR=!Errorlevel!
IF !ERROR! NEQ 0 echo ** There was an error (!ERROR!) renaming to "!NewFileName!". See log for error message **
ECHO %date%,%time%,"%%A","!NewF
Cheers
ASKER
Outstanding. Great Job! Thanks again to both of you. ~k
No problem, works best when we bounce ideas of each other I guess.
Steve
Steve
You'r welcome.
@Steve, until the next one! Cheers
@Steve, until the next one! Cheers
I did not test it, and know it will not work as is.
Will try to update it tomorrow, unless another expert debug or improve it.
Cheers,
Rene
Open in new window