Looping replace

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Beach Boy
Posts: 9
Joined: 27 Jul 2013 10:40

Looping replace

#1 Post by Beach Boy » 30 Jul 2013 13:49

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

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: Looping replace

#2 Post by Dragokas » 30 Jul 2013 16:04

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.

Beach Boy
Posts: 9
Joined: 27 Jul 2013 10:40

Re: Looping replace

#3 Post by Beach Boy » 31 Jul 2013 09:26

Thank you Dragokas -
A little bit of revising but it worked out nice.

Post Reply