Page 1 of 1

Looping replace

Posted: 30 Jul 2013 13:49
by Beach Boy
Is it possible to loop this script in a for each command to perform on each file in a given folder?

Code: Select all

@echo off
set "replace=system"
set "replaced=offline"

set "source=control.reg"
set "target=control2.reg"

setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal

Re: Looping replace

Posted: 30 Jul 2013 16:04
by Dragokas
Hello, Beach Boy. Here some script.
"sourceFolder" - is a folder with files that need replaces. (reading without subfolders)
"mask" - for some additional filtering.
Destination is the same folder, but the name of file was created with prefix _2

Also, I prefer to use "find", not "findstr" - less bugs.

Code: Select all

@echo off
setlocal enableDelayedExpansion EnableExtensions

set "replace=system"
set "replaced=offline"

set sourceFolder=c:\temp
set mask=*.reg

for /f "delims=" %%F in ('dir /b /a-d "%sourceFolder%\%mask%"') do (
  (
    for /F "tokens=1* delims=]" %%a in ('find /N ^< "%sourceFolder%\%%F"') do (
      set "line=%%b"
      if defined line set "line=!line:%replace%=%replaced%!"
      echo.!line!
    )
  ) > "%sourceFolder%\%%~nF_2%%~xF"
)

Best regards, Alex.

Re: Looping replace

Posted: 31 Jul 2013 09:26
by Beach Boy
Thank you Dragokas -
A little bit of revising but it worked out nice.