# Solved: Batch file to rename files based on a substring in the file



## dhj (Oct 31, 2012)

I have a script that almost does what I need, but I can't seem to get the last piece working. Here is the task description:
I have a directory which contains files I want to rename. The files in the directory that I want to rename can be identified by a search pattern: FFF_*.log. I need to find a substring that exists on a line in each of the file that contains the following substring: --SCRAPBOOK_SET. The string I want to use in the rename is the string immediately following the --SCRAPBOOK_SET, but without the leading backquote and the ending single quote. That string that follows (in this case, 338X2_Tap_41_338X2) varies from file to file. An example line containing the search string is: 
2012-07-25 06:11:13| 0.3| 0.0|INFORM|FFF Configuration: Command line arguments are `fff' `XXX\DV\Export.fff' `--SERVICE' `jdbc:cracle:thinxxxxxxx.xx.com:1521:xxxxxx' `--LOG_FILE' `' `--USER' `xxxxx' `--NETWORK_MODEL_FILENAME' `\\XX.com\Data\DepartmentData\Energy~1\Distri~1\AspenDV\GISgen~1\LOCATION\$(SCRAPBOOK_SET).DVT' `--FFF_SECURITY_USER' `yyyyy' `-LOG_FILENAME' `C:/FFFServer//Logs/serverjobsubmitter\FFF_16633E40_134321107322_2600.log' `--FFF_SERVER_ROOT' `C:/FFFServer/' `--PROTECTED_DEVICE_FILENAME' `\\nu.com\Data\DepartmentData\PSNH-ED-CO\Deptdata\Energy~1\Distri~1\AspenDV\GISgen~1\TILTON\$(SCRAPBOOK_SET).RYT' `--FFF_MF_NAME' `DV_Circuit_Export.fff' `--SCRAPBOOK_SET' `338X2_Tap_41_338X2' `--PASSWORD' `***' `--FFF_SERVER_NAME' `Engine1' `-FFF_ENGINE_MEMORY_REDLINE' `0.5' `--FFF_DATA_REPOSITORY' `C:/FFFServer//Server/Upload/' `--FEATURE_TYPES' `' `--FFF_SECURITY_ROLES' `fffxxxx'.
I am almost there, but can't seem to find a way to remove the leading backquote and the trailing single quote.
Example: Original file name is FFF_16633E40_1340382808516_4464.log. 
Result I want: 338X2_Tap_41_338X2 - FFE_16633E40_134321107322_2600.log
Result the following script produces: `338X2_Tap_41_338X2' - FFF_16633E40_134321107322_2600.log

echo off
set "str=--SCRAPBOOK_SET"
For /F "Tokens=* delims=" %%A in ('Dir C:\testfile\FFF_*.log /B /A:-D ') Do (
For /F "tokens=31* delims= " %%B in ('FINDstr /I /C:"%str%" "%%A"') Do (
REM ECHO %%B - %%~nA%%~xA
ren "%%A" "%%B - %%~nA%%~xA" 
)
)

Any help would be greatly appreciated. Thanks!


----------



## foxidrive (Oct 20, 2012)

Try this - it echos the ren command. Any ! characters in the line will be a problem.


```
@echo off
setlocal enabledelayedexpansion
For /F "delims=" %%A in ('Dir C:\testfile\FFF_*.log /B /A:-D ') Do (
for /f "delims=" %%B in ('findstr /c:"--SCRAPBOOK_SET" "%%A"') do (
set "var=%%B"
set "var=!var:*--SCRAPBOOK_SET' `=!"
for /f "delims='" %%C in ("!var!") do echo ren "%%A" "%%C%%~xA"
))
pause
```


----------



## dhj (Oct 31, 2012)

Thanks, Foxidrive. This does exactly what I need. If you have a moment, can you explain the syntax of the last line? I don't quite get it.
I really appreciate your help.


----------



## foxidrive (Oct 20, 2012)

set "var=!var:*--SCRAPBOOK_SET' `=!"

In the line above it removes the leading part of the line until it gets to 
--SCRAPBOOK_SET' `
including the last backtick.
The next several characters are the filename, followed by an apostrophe.

This following line sets the delims to the apostrophe, and takes the first (default) token, and that is used in %%C.
That is the text from the beginning of the %var% to the backtick - which is the new filename.

for /f "delims='" %%C in ("!var!") do echo ren "%%A" "%%C%%~xA"

The %%~xA refers to the extension, you can read that in the last page of the FOR /? help.


----------

