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
katnip999Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ReneGeConnect With a Mentor Commented:

@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
)

Open in new window

0
 
ReneGeCommented:
Here is my idea on how to resolve your issue.

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


@ECHO OFF

SET LogFile=%~n0.log

FOR /F "delims=" %%A IN ('DIR /B /S 00*.txt') DO (
	ECHO File found:[%%A]
	CALL :ReadTitle "%%A"
	REN "%%A" "Title.txt"
	ECHO %date%,%time%,"%%A","Title.txt">>"%LogFile%"
)

EXIT


:ReadTitle
FOR /F "tokens=43-60" %%A IN ('TYPE  %~1') DO (
	SET Title=%%A
	exit /b
)

Open in new window

0
 
katnip999Author Commented:
Very Close! But it just re-named one file with "Title.txt".
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ReneGeCommented:
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
0
 
Steve KnightConnect With a Mentor IT ConsultancyCommented:
Slight adjustment might get you what you need.... this is 95% ReneGe's code so answer to him and all that if it does... assumes that the whole name is in those characters but can soon add .txt on or strip off unneeded spaces if a problem etc.

I think I have this value right to strip the value from your string:

SET Title=!title:~42,18!

I tested with text as per code attachment.

Steve

@ECHO OFF  
setlocal enabledelayedexpansion  
SET LogFile=%~n0.log  
 
FOR /F "delims=" %%A IN ('DIR /B /S 00*.txt') DO (  
        ECHO File found:[%%A]  
        CALL :ReadTitle "%%A"  
        echo Renaming "%%A" to "!title!"
        REN "%%A" "!title!"  
        ECHO %date%,%time%,"%%A","!title!">>"%LogFile%"  
)  
 
EXIT   /b
 
 
:ReadTitle  
FOR /F "delims=" %%N IN ('TYPE  "%~1"') DO (
        SET Title=%%N
        SET Title=!title:~42,18!
        exit /b
)
1234567890123456789012345678901234567890123456789012345678901234567890
0        1         2         3         4         5         6         7

Giving:

c:\ee\test>t
File found:[C:\Documents and Settings\stephen\test\00abcd.txt]
Renaming "C:\Documents and Settings\stephen\test\00abcd.txt" to "345678901234567
890"

c:\ee\test>dir

26/09/2011  22:44                  146 345678901234567890

Open in new window

0
 
katnip999Author Commented:
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
0
 
ReneGeCommented:
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
0
 
Steve KnightIT ConsultancyCommented:
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
0
 
Steve KnightIT ConsultancyCommented:
You had still put "title.txt" hard coded in your script ReneGe :-)
0
 
ReneGeCommented:
This is so funny... I'm glad I put a disclaimer :)
0
 
ReneGeCommented:
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
)

Open in new window

0
 
katnip999Author Commented:
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\003829.txt","LOANS W/ INVALID COL.txt"  
Mon 09/26/2011,15:59:42.24,"C:\tmp-dvr\003833.txt","FIXED LOANS W/O INDE.txt"  
Mon 09/26/2011,15:59:42.24,"C:\tmp-dvr\003849.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.txt]
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.txt]
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.txt]
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??
0
 
katnip999Author Commented:
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??
0
 
ReneGeCommented:
That is because you have "/" in your file name.

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
)

Open in new window

0
 
ReneGeCommented:
Or if you want to have a "-" in the place of "/", change line 29 by: SET Title=!title:/=-!
0
 
ReneGeCommented:
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
0
 
ReneGeCommented:
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.
0
 
Steve KnightIT ConsultancyCommented:
Well spotted there Rene!... far too late here, boys all deposited in bed at last and pc shutting down for a couple of hours!
0
 
ReneGeCommented:
Thanks Steve!
Have yourself a nice rest
0
 
Steve KnightIT ConsultancyCommented:
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
@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
)

Open in new window

0
 
ReneGeCommented:
@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","!NewFileName!",!ERROR!>>"%LogFile%"

Cheers
0
 
katnip999Author Commented:
Outstanding. Great Job! Thanks again to both of you. ~k
0
 
Steve KnightIT ConsultancyCommented:
No problem, works best when we bounce ideas of each other I guess.
Steve
0
 
ReneGeCommented:
You'r welcome.

@Steve, until the next one! Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.