Renaming files to add a string contained within the file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
johnsd
Posts: 2
Joined: 17 Oct 2012 15:47

Renaming files to add a string contained within the file

#1 Post by johnsd » 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%

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Renaming files to add a string contained within the file

#2 Post by abc0502 » 17 Oct 2012 19:13

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

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Renaming files to add a string contained within the file

#3 Post by foxidrive » 17 Oct 2012 23:23

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.

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"
)
)

johnsd
Posts: 2
Joined: 17 Oct 2012 15:47

Re: Renaming files to add a string contained within the file

#4 Post by johnsd » 18 Oct 2012 06:44

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!!

Post Reply