3

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?

Community
  • 1
  • 1
James
  • 15,754
  • 12
  • 73
  • 91
  • Advice.If its not requirement to use batch - make your life easier - use a scripting language. I would prefer python, but you can choose whatever you like. Multiple file content parsing is possible, but bad thing to do in the batch. – Drako Jan 09 '17 at 14:54
  • @Drako that was my immediate thought too, however it is a requirement as it needs to go to a client and they require it as a batch file. – James Jan 09 '17 at 20:34

3 Answers3

4

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
aschipfl
  • 33,626
  • 12
  • 54
  • 99
1

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.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
1

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.

Ryan
  • 35
  • 5
  • 1
    the `all matching files` part would make this very painful, so this answer isn't helpful. – Stephan Oct 18 '20 at 07:47
  • 1
    I found this answer useful - notepad++ as of version 7.9 can replace strings within files of a directory. I had to replace NCOLS to ncols in 60 files. This worked like a charm with the ctrl+F replace in directories. – Jens Dec 10 '20 at 07:52