Solved

Dos Batch Script - File Rename with report names contained within the file

Posted on 2011-09-26
24
298 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:katnip999
  • 13
  • 6
  • 5
24 Comments
 
LVL 10

Expert Comment

by:ReneGe
ID: 36601729
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
 

Author Comment

by:katnip999
ID: 36601840
Very Close! But it just re-named one file with "Title.txt".
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36601890
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
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 36602300
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
 

Author Comment

by:katnip999
ID: 36602400
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 36602416
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36602460
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36602464
You had still put "title.txt" hard coded in your script ReneGe :-)
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36602470
This is so funny... I'm glad I put a disclaimer :)
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36602492
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
 

Author Comment

by:katnip999
ID: 36602604
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
 

Author Comment

by:katnip999
ID: 36602615
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 10

Expert Comment

by:ReneGe
ID: 36605216
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 36606038
Or if you want to have a "-" in the place of "/", change line 29 by: SET Title=!title:/=-!
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36607223
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 36608134
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
 
LVL 10

Accepted Solution

by:
ReneGe earned 250 total points
ID: 36608850

@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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36617420
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 36632846
Thanks Steve!
Have yourself a nice rest
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36707731
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 36709245
@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
 

Author Closing Comment

by:katnip999
ID: 36710525
Outstanding. Great Job! Thanks again to both of you. ~k
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36710547
No problem, works best when we bounce ideas of each other I guess.
Steve
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36710663
You'r welcome.

@Steve, until the next one! Cheers
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now