rename a.txt with data in that file

September 29, 2011 at 00:27:50
Specs: DOS
i have a.txt file which contains some data as follows

File a.txt shows following lines
1. *************
2. testing serial number
3. Date: 29-09-2011 LR No. 4589A Sr. No. T 12345
4. **************

I want to rename the file "a.txt" with "T 12345.txt" ( line 3 contains that data)

Pls. send me the batch file which can do above task.
* I received so many a.txt file each day and I have to rename all that a.txt file which contains different Sr. No. ..............
* Sr. No. samples : T 12345, R 00034, R 0001255, T 444565

Waiting for your early reply.

Regards
Sujeet


See More: rename a.txt with data in that file

Report •


#1
September 29, 2011 at 14:27:44
Test this:

@echo off
cls
setlocal

more +2 a.txt>%temp%\a.txt

set /p line=<%temp%\a.txt

for /f "tokens=1-8*" %%1 in ("%line%") do (
    echo a.txt being renamed %%9.txt
    REM ren "a.txt" "%%9.txt"
)

del %temp%\a.txt

Good luck.


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


Report •

#2
September 29, 2011 at 15:51:57
here is a much more simple way of doing it:

@echo off

findstr Sr. a.txt > temp.txt

for /f "tokens=9-10" %%1 in (temp.txt) do (ren "a.txt" "%%1%%2.txt")

del temp.txt

Though that is based upon what you put down. If at any point there is more text in the documents it will change the token count and throw your name off


Report •

#3
September 29, 2011 at 17:48:19
Yes, a simple way but did you test for what ifs? What if the find string occurs more than once in the input file? Your loop must be terminated after the first iteration otherwise 'file not found' errors will be displayed.

Sorry, I don't understand what you mean by:
If at any point there is more text in the documents it will change the token count and throw your name off. I do not believe the OP posted the entire file. The number of tokens is not dependent on the quantity of text lines in the file but on the content of the specific line which was shown.

Thanks for your interest.


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


Report •

Related Solutions

#4
September 30, 2011 at 09:27:15
Add some additional text to the existing lines...lets say the beginning of line 3:

3. random text Date: 29-09-2011 LR No. 4589A Sr. No. T 12345

T 12345 will no longer be in the 9 and 10 token slot. Now you will get Sr. No. I was only putting down another way of doing it so that everyone can see that there are different options. It is not the end all, be all by any means. I copied Sujeet changrani text file and Wahine's code and it did not change the name, HOWEVER it did work great after I removed the REM statement.


Report •

#5
October 1, 2011 at 14:17:00
HOWEVER it did work great after I removed the REM statement.

Yes, of course, it would be extremely bad practice to rename files in a testing environment. Once testing is complete and the output is approved both the Echo command line and the REM command can be removed.

Again thanks for your interest CrashnBurn.


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


Report •

#6
October 3, 2011 at 08:38:31
your welcome

Report •


Ask Question