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%
Renaming files to add a string contained within the file
Moderator: DosItHelp
Re: Renaming files to add a string contained within the file
This will Append the part you pointed at to the existing name of all files in the directory with a space as a separator between existing name and the new part
Specify the file type without the dot " . "
Test It First
Specify the file type without the dot " . "
Test It First
Code: Select all
@Echo OFF
:: Put This Batch with The Files in The Same Folder
set "type=log"
set "str=Read substation name from scrapbook"
For /F "Tokens=* delims=" %%A in ('Dir /B /A:-D "*.%type%"') Do (
For /F "tokens=9* delims= " %%B in ('FINDstr /I /C:"%str%" "%%A"') Do (
Ren "%%~nA.%type%" "%%~nA %%B %%C.%type%"
)
)
pause
Re: Renaming files to add a string contained within the file
This seems to work here. It appends the text to each *.txt filename.
It differs from abc0205 in that the makeup of each line is not significant to the result.
It differs from abc0205 in that the makeup of each line is not significant to the result.
Code: Select all
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('dir *.txt /a:-d /b') do (
for /f "delims=" %%b in ('find /i "Read substation name from scrapbook" ^<"%%a"') do (
set "var=%%b"
set "var=!var:*Read substation name from scrapbook =!"
ren "%%a" "%%~na - !var!%%~xa"
)
)
Re: Renaming files to add a string contained within the file
Thank you abc0502 and foxidrive!!! This is exactly what I need. I am trying to work through your code so that I understand exactly what is going on and am learning alot. Thanks again!!