Batch to rename .txt file based on content

Microsoft Windows xp professional w/serv...
August 4, 2010 at 11:20:59
Specs: XP
Hi, new to the forum and batch files in general. I am looking to rename 2 text files from a floppy and cut and copy them to a folder on my hard drive. Specifically the floppy will have a LOG.TXT and a LOGDEL.TXT file on it. I want to copy the file from the floppy, rename it based on contents in the text file (line and column specified), paste it to a folder on my hard drive and then delete the file from the floppy.

e.g. Find and copy log.txt, rename it log_1234567.txt, paste to specified directory, delete log.txt from the floppy and do the same for logdel.txt.

Any help is appreciated.


See More: Batch to rename .txt file based on content

Report •


#1
August 4, 2010 at 11:42:32
If you're using, let's say, row 2, column 3:

=====================================
@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=1-3 delims= " %%a in (a:\LOG.TXT) do (
set /a N+=1
if !N! equ 2 set T=%%c
)

move a:\LOG.TXT d:\LOG_!T!.TXT


=====================================
Helping others achieve escape felicity

M2


Report •

#2
August 4, 2010 at 12:19:21
Thanks for the response. The batch successfully moves the file but is renaming LOG_.txt. I put the following in to a text file to test the batch:

1234567890abcdefghijkl
mnopqrstuvwxyz1a2a3a4a
5a6a7a8a9a0a1b2b3b4b5b6b
7b8b9b0b1c2c3c4c5c6c7c8c

What I would like to do is, for example, capture 7 characters from line 2 starting with s, so the new file name should be LOG_stuvwxy.txt. Is this possible?


Report •

#3
August 4, 2010 at 12:50:51
@echo off & setLocal EnableDELAYedeXpansion

set N=
for /f "tokens=* delims= " %%a in (f:\LOG.TXT) do (
set /a N+=1
if !N! equ 2 set T=%%a
)

set T=!T:~6,7!
move f:\LOG.TXT d:\LOG_!T!.TXT


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
August 4, 2010 at 13:19:27
That did it, thanks for your help!

Report •


Ask Question