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 444565Waiting for your early reply.
Regards
Sujeet
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.txtGood luck.
Please come back & tell us if your problem is resolved.
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
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.
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.
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.
your welcome
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |