Renaming files to add a string contained within the file
Posted: 17 Oct 2012 16:44
I have a directory containing files with meanlingless names. Every file contains a line that contains a string that I want to append to the file name.
So, An example of the line is:
2012-10-12 04:01:53| 0.4| 0.0|INFORM|Read substation name from scrapbook 3137X 3137X_TAP_3137X_64
I can find this line in each file by searching for the string "Read substation name from scrapbook "
The part of this line I want to append to the filename is the 3137X_TAP_3137X_64. This is everying from the end of the line back to the first space.
For now, I am trying to process a single file to get the string I want to append to the filename.
I am splitting out the line by the pipe, the last string being the one I'm interested in. It is always in the format: Read substation name from scrapbook xxx yyy, where yyy is variable length but contains no spaces.
I can use findstr to get the line in the file that I am interested in but I can't get it to work with the rest of the script (below) I don't use DOS much and at this point don't even know if I'm heading in the right direction.
-----------------------------------------------------------------
@echo off
set STR="2012-10-12 04:01:53| 0.4| 0.0|INFORM|Read substation name from scrapbook 3137X TAP 3137X_TAP_3137X_64"
rem I cannot get the results of findstr to work. Seems like it is an issue with special characters.
rem findstr /C:"Read substation name from scrapbook" xxx_633E4057_135002888980_9999.log > circuit.txt
rem set /p str=<circuit.txt
for /f "tokens=1,2,3,4,5 delims=|" %%a in (%str%) do (
set tmp1=%%a
set tmp2=%%b
set tmp3=%%c
set tmp4=%%d
set field=%%e
)
echo %field%
echo remove read text
set field=%field:Read substation name from scrapbook =%
echo %field%
echo replace space with pipe
set field=%field: =\%
echo %field%
set STR="%field%"
echo break out parts
for /f "tokens=1,2,3 delims=\" %%a in (%str%) do (
set part1=%%a
set part2=%%b
set part3=%%c
)
echo %part3%
So, An example of the line is:
2012-10-12 04:01:53| 0.4| 0.0|INFORM|Read substation name from scrapbook 3137X 3137X_TAP_3137X_64
I can find this line in each file by searching for the string "Read substation name from scrapbook "
The part of this line I want to append to the filename is the 3137X_TAP_3137X_64. This is everying from the end of the line back to the first space.
For now, I am trying to process a single file to get the string I want to append to the filename.
I am splitting out the line by the pipe, the last string being the one I'm interested in. It is always in the format: Read substation name from scrapbook xxx yyy, where yyy is variable length but contains no spaces.
I can use findstr to get the line in the file that I am interested in but I can't get it to work with the rest of the script (below) I don't use DOS much and at this point don't even know if I'm heading in the right direction.
-----------------------------------------------------------------
@echo off
set STR="2012-10-12 04:01:53| 0.4| 0.0|INFORM|Read substation name from scrapbook 3137X TAP 3137X_TAP_3137X_64"
rem I cannot get the results of findstr to work. Seems like it is an issue with special characters.
rem findstr /C:"Read substation name from scrapbook" xxx_633E4057_135002888980_9999.log > circuit.txt
rem set /p str=<circuit.txt
for /f "tokens=1,2,3,4,5 delims=|" %%a in (%str%) do (
set tmp1=%%a
set tmp2=%%b
set tmp3=%%c
set tmp4=%%d
set field=%%e
)
echo %field%
echo remove read text
set field=%field:Read substation name from scrapbook =%
echo %field%
echo replace space with pipe
set field=%field: =\%
echo %field%
set STR="%field%"
echo break out parts
for /f "tokens=1,2,3 delims=\" %%a in (%str%) do (
set part1=%%a
set part2=%%b
set part3=%%c
)
echo %part3%