Solved Rename multiple text files names based on con

October 12, 2011 at 08:08:18
Specs: Windows XP
Good Morning,
I have a question based where I need to write a batch file that will process all text files in a directory and rename the files based on a string in the first line of the text file. I searched the other answers here and they come close but dont match what im looking to do.

My experience is limited to basic cmd prompt batch file writing.
Directory of files c:\incoming\
Sample file name#1 RPT0001.summary.txt
Sample file name #2 RPT0002.summary.txt
Sample content of first line of file:
HDRABCEDF123456789020110101 (Where HDR=Constant , ABCDEF = Variable client,1234567890 = Variable file tag, 20110101= variable date)

I need the outputfilename to go from RPT0001.summary.txt to ABCDEF123456789020110101.summary.txt

I need it to cycle through for each file in the folder.

Any help would be greatly appreciated.


See More: Rename multiple text files names based on con

Report •

October 14, 2011 at 19:26:23
✔ Best Answer
Test this. Files will not be renamed until you are satisfied with the outcome and remove the REM command. The script is based on the assumption that the entire content of line 1 of each file is as shown in your post. No allowance is made for any filename containing spaces.

Good luck.

@echo off
setlocal enabledelayedexpansion

pushd c:\incoming\||echo Pushd failed - job terminated&& exit/b

for /f "tokens=*" %%1 in ('dir /b *.txt') do (
        set /p inline=<%%1&set inline=!inline:~3!

        echo %%1 will be renamed !inline!.summary.txt
REM        ren %%1 !inline!.summary.txt


Please come back & tell us if your problem is resolved.

Report •

October 14, 2011 at 20:49:59
I should have mentioned there were characters after position 19 in line 1

How much would that change this script? So far I am following what you are doing in he above script.

Thoughts ?


Report •

October 14, 2011 at 22:07:17
Disregard my last message. I got it by adding ,16 after the 3. Thanks sooooo Much!! this helped a great deal.


Report •

Related Solutions

October 14, 2011 at 22:30:58
I got it by adding ,16 after the 3

Does that not drop off the date? If you post a couple of examples of the full first lines I'll have a go at extracting the relevant filename. However if what you've done is satisfactory please ignore this suggestion.

Thanks for coming back to report your partial success.


Please come back & tell us if your problem is resolved.

Report •

November 17, 2011 at 07:51:23
Here's a followup question, Now that I have the data extracted from the first line, what if i also wanted to extract characters 4-9 on the second line as well. No forum discussion really addresses that.



Report •

November 18, 2011 at 01:39:56
Lazy method:

Use the more command to write the file to a temp file starting with line 2. Set /p the first line of the temp file then extract from there. e.g.

Untested - no warranties.

More +1 %%1 > temp.file
set /p line2=<temp.file
set fourtonine=!line2:~3,6!

Please come back & tell us if your problem is resolved.

Report •

Ask Question