Batch to rename txt file based on content

Microsoft Windows xp professional w/serv...
August 6, 2010 at 13:30:46
Specs: XP
Hi, I'm looking to create a batch file to rename text files based on the files content. I have logs delimited with ~. I want to count over a number of tildes and rename it based on the next 7 characters.

e.g. ~blah~blah~blah~blah~blah~capture~blah

Batch should count 6 ~'s and put next 7 characters into a variable to use to rename file: filename_capture.txt

Is this possible?

See More: Batch to rename txt file based on content

Report •

August 6, 2010 at 14:13:41
It is possible yes. This code will take next 7 characters after the 6th tilde (~) and rename the file of your choice.

SETLOCAL EnableDelayedExpansion

ECHO Enter the name of the text file. (Without the extension)
SET /p File=""

FOR /f "tokens=6 delims=~" %%a IN (%File%.txt) DO (
	SET Variable=%%a
SET Variable1=%Variable:~0,7%
RENAME %File% %File%_%Variable1%.txt

Report •

August 6, 2010 at 14:54:35
Thanks for the help! Much appreciated. I'm still very new to all this. I'm guessing if I change tokens=6 to tokens=7 it will count 7 ~'s? And if I change delims=~ to delims=^ it will count ^'s instead of tildes?

Report •

August 6, 2010 at 15:08:50
^ is a Caret, just so you know hehe. Anyways, basically the answer to your question is yes, but I'm not sure if you will be able to use ^ as a delimiter, because in batch that is also used to escape.

Try it and see what happens.

EDIT: yes, you can use ^ as a delimiter, I tested and it works fine.

Report •

Related Solutions

August 6, 2010 at 15:12:41
NVM, tested it myself and it works, should have just done that first rather than bug you more, thanks again!

Report •

August 6, 2010 at 15:14:43
No Problem, Glad I could Help!

Report •

December 7, 2010 at 17:09:35
I'm also new to programming, I've got a similar question.
I have a directory of ascii data files where the second line of each file is always in the format:

F1 1251040A0

I'd like to rename each file in the dir to the 10 characters following the "F1 ".

Since I have multilple files, would I use a nested FOR statement?
It seems like I should be able to fashion your earlier code to do this, but I've not been able to make it work. I'd appreciate any help you could give.

Report •

Ask Question