Reputation: 16339
I don't have much experience with batch scripts, and my employer has asked me write a batch script that can be run to find and replace some text inside all matching files in the directory.
I've tried searching this and there's a tonne of resource, which I've used to get me this far:
@echo off
setlocal enableextensions disabledelayedexpansion
set "search=<employeeloginid>0"
set "replace=<employeeloginid>"
set "textFile=TimeTEQ20170103T085714L.XML"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
This will find all occurrences of <employeeloginid>0
and replace it with <employeeloginid>
inside a set file - in this case TimeTEQ20170103T085714L.XML
.
I now need to tweak this to run on all files that start with TimeTEQ
and end with .xml
I found this answer which shows how to do all files in a directory, but I don't know how I would tweak it to suit my needs here.
Can anyone help me please?
Upvotes: 5
Views: 29120
Reputation: 35
for people still searching this on google, the easy way is to open all files containing the text needed to be replaced in notepad++, press ctrl+F, click replace, type in what you want to find and replace and click replace all, then save all.
Upvotes: 1
Reputation: 34899
Simply wrap around a standard for
loop, like this:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "search=<employeeloginid>0"
set "replace=<employeeloginid>"
set "textFile=TimeTEQ*.xml"
set "rootDir=."
for %%j in ("%rootDir%\%textFile%") do (
for /f "delims=" %%i in ('type "%%~j" ^& break ^> "%%~j"') do (
set "line=%%i"
setlocal EnableDelayedExpansion
set "line=!line:%search%=%replace%!"
>>"%%~j" echo(!line!
endlocal
)
)
endlocal
If you want to process matching files also in the sub-folders, use a for /R
loop:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "search=<employeeloginid>0"
set "replace=<employeeloginid>"
set "textFile=TimeTEQ*.xml"
set "rootDir=."
for /R "%rootDir%" %%j in ("%textFile%") do (
for /f "delims=" %%i in ('type "%%~j" ^& break ^> "%%~j"') do (
set "line=%%i"
setlocal EnableDelayedExpansion
set "line=!line:%search%=%replace%!"
>>"%%~j" echo(!line!
endlocal
)
)
endlocal
Upvotes: 6
Reputation: 5874
You can surround the loop you got currently with another one going through all files in the directory that match your pattern like this:
for /f "delims=" %%n in ('dir /b ^| findstr /r "TIMETEQ.*\.xml') do (
and change the header of your current loop to:
for /f "delims=" %%i in ('type "%%~n" ^& break ^> "%%~n" ') do (
and >>"%textFile%" echo(!line!
to
>>"%%~n" echo(!line!
So in total the new script should look like this:
@echo off
setlocal enableextensions disabledelayedexpansion
set "search=<employeeloginid>0"
set "replace=<employeeloginid>"
set "textFile=TimeTEQ20170103T085714L.XML"
for /f "delims=" %%n in ('dir /a-d /b ^| findstr /r "TIMETEQ.*\.xml') do (
for /f "delims=" %%i in ('type "%%~n" ^& break ^> "%%~n" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%%~n" echo(!line!
endlocal
)
)
Explanation:
The new added outer loop goes through the output of the command dir /b ^| findstr /r "TIMETEQ.*\.xml
which itself contains the result of a regex search over the output of the dir
command with /b
switch to show only the filenames and /a-d
to execlude directories from the search.
Then I changed %filename%
to the parameter of the loop -> %%~n
with ~
to remove potential surrounding double quotes.
Upvotes: 1