Page 1 of 5
Find and replace string using batch
Posted: 06 Apr 2013 19:32
by pditty8811
I'm a bit embarrassed I'm asking this but here goes:
I'm having issues writing code to find and replace strings in text files. I'm in the middle of doing my taxes so I'd figure I'd put this up so someone can take a look at it in the mean time.
Here is what I have:
rename "Dynamic Campaign\BATCHES\BATCH_SCR.bat" BATCH_SCR.tmp
set /p this=E:\Program Files (x86)\Ubisoft\SilentHunterIII
for /f "tokens=*" %%a in ("Dynamic Campaign\BATCHES\BATCH_SCR.tmp") do (
set foo=%%a
if %%a==!this! set foo=!sh3directory!
echo !foo!>> "Dynamic Campaign\BATCHES\BATCH_SCR.bat"
)
del "Dynamic Campaign\BATCHES\BATCH_SCR.tmp" /q
pause
!this! is the variable that I am trying to find (string trying to find)
!sh3directory! is the string I want to replace it with.
Yes, sh3directory is assigned correctly.
Notice both variables are filepaths.
Thank you.
Re: Find and replace string using batch
Posted: 07 Apr 2013 07:52
by Endoro
You can try this for loop:
Code: Select all
(for /f "usebackqtokens=*" %%a in ("Dynamic Campaign\BATCHES\BATCH_SCR.tmp") do (
set "foo=%%a"
setlocal enabledelayedexpansion
set "fii=!foo:%this%=%sh3directory%!"
echo(!fii!
endlocal
))>"Dynamic Campaign\BATCHES\BATCH_SCR.bat"
Re: Find and replace string using batch
Posted: 07 Apr 2013 08:42
by Squashman
There is a big library of functions on the main DosTips website. You should look thru it one of these days.
http://www.dostips.com/DtCodeCmdLib.php ... substitute
Re: Find and replace string using batch
Posted: 07 Apr 2013 09:58
by dbenham
Most of the batch text file search/replace functions have various limitations. (always case insensitive, can't replace =, etc.) And they are SLOOOOOW. They work fine for small files, but large files are painful to edit.
I've posted a robust pure batch search/replace utility whose only limitation is a max line length of 1021 characters:
The "ultimate" file search and replace batch utility. Performance is better than most pure batch solutions as long as the number of replacements is relatively small.
But my favorite text file search/replace utility is a hybrid JScript/batch script that supports regex search and replace:
regex search and replace for batch - Easily edit files!. It is amazingly powerful for such a small amount of code, and performance is
much better than any pure batch solution.
Dave Benham
Re: Find and replace string using batch
Posted: 07 Apr 2013 18:23
by pditty8811
Endoro wrote:You can try this for loop:
Code: Select all
(for /f "usebackqtokens=*" %%a in ("Dynamic Campaign\BATCHES\BATCH_SCR.tmp") do (
set "foo=%%a"
setlocal enabledelayedexpansion
set "fii=!foo:%this%=%sh3directory%!"
echo(!fii!
endlocal
))>"Dynamic Campaign\BATCHES\BATCH_SCR.bat"
Hi Endoro,
I decided to go with your example first because it seemed to be the least complicated. It works, but it removes all the blank lines from the output file.
Is there a way to prevent this?
Thanks.
Re: Find and replace string using batch
Posted: 07 Apr 2013 19:06
by abc0502
I have this snippets, it will preserve the empty lines as it is.
Code: Select all
@Echo OFF
REM Set These Variables
SET "InFile=1.txt"
SET "OutFile=out.txt"
SET "Replace=Line"
SET "ReplaceWith=Two Lines"
REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"
REM Create The OutFile with changes
SETLOCAL EnableDelayedExpansion
<"!InFile!" (
FOR /L %%a IN (1 1 0) DO SET /p "="
FOR /L %%A IN (1 1 %Till%) DO (
SET "line="
SET /P "line="
IF "!line!x" == "x" ( Echo.
) ELSE ( Echo !line:%Replace%=%ReplaceWith%!)
)
)>>"%OutFile%"
ENDLOCAL
PAUSE
Replace and
ReplaceWith Should be
this and
sh3directory
Re: Find and replace string using batch
Posted: 07 Apr 2013 19:38
by pditty8811
abc0502 wrote:I have this snippets, it will preserve the empty lines as it is.
Code: Select all
@Echo OFF
REM Set These Variables
SET "InFile=1.txt"
SET "OutFile=out.txt"
SET "Replace=Line"
SET "ReplaceWith=Two Lines"
REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"
REM Create The OutFile with changes
SETLOCAL EnableDelayedExpansion
<"!InFile!" (
FOR /L %%a IN (1 1 0) DO SET /p "="
FOR /L %%A IN (1 1 %Till%) DO (
SET "line="
SET /P "line="
IF "!line!x" == "x" ( Echo.
) ELSE ( Echo !line:!Replace!=!ReplaceWith!!)
)
)>>"%OutFile%"
ENDLOCAL
PAUSE
Replace and
ReplaceWith Should be
this and
sh3directory
I tried yours. The output preserves the blank lines but all the lines are:
Code: Select all
ReplaceReplaceWith
ReplaceReplaceWith
Everything says ReplaceReplaceWith.
Did you test it? I think there is an error in the assignment.
Re: Find and replace string using batch
Posted: 07 Apr 2013 19:54
by abc0502
sorry for that, i changed the error
it was in
Echo !line:%Replace%=%ReplaceWith%!
Re: Find and replace string using batch
Posted: 07 Apr 2013 20:56
by pditty8811
I'm getting an error with that. Are you sure?
Re: Find and replace string using batch
Posted: 07 Apr 2013 21:03
by abc0502
have you set the replace and replacewith variables ?
post what u used
Re: Find and replace string using batch
Posted: 07 Apr 2013 21:24
by pditty8811
Code: Select all
set "sh3directory=E:\Program Files (x86)\Ubisoft\SilentHunterIII"
REM Set These Variables
SET "InFile=Dynamic Campaign\BATCHES\BATCH_SCR.bat"
SET "OutFile=Dynamic Campaign\BATCHES\BATCH_SCR.bat"
SET "Replace=E:\Program Files (x86)\Ubisoft\SilentHunterIII1"
SET "ReplaceWith=!sh3directory!"
echo !Replace!
echo !ReplaceWith!
pause
REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"
REM Create The OutFile with changes
SETLOCAL ENABLEDELAYEDEXPANSION
<"!InFile!" (
FOR /L %%a IN (1 1 0) DO SET /p "="
FOR /L %%A IN (1 1 %Till%) DO (
SET "line="
SET /P "line="
IF "!line!x" == "x" ( Echo.
) ELSE (Echo !line:%Replace%=%ReplaceWith%!)
)
)>>"%OutFile%"
ENDLOCAL
pause
Re: Find and replace string using batch
Posted: 07 Apr 2013 21:25
by abc0502
you didn't set the drive letter where the "Dynamic Campaign\BATCHES\BATCH_SCR.bat"" is.
is it C:\ or D:\ .. etc,
Re: Find and replace string using batch
Posted: 07 Apr 2013 21:26
by pditty8811
abc0502 wrote:you didn't set the drive letter where the "Dynamic Campaign\BATCHES\BATCH_SCR.bat"" is.
is it C:\ or D:\ .. etc,
That is becauese Dynamic Campaign folder is within the folder of the executed batch.
Re: Find and replace string using batch
Posted: 07 Apr 2013 21:27
by abc0502
no, that does work here in this batch, as the path is used in the for command with the type command and the redirect sign < and >>
you must set the drive path
Edited:
by the way, make sure you change the OutFile name, or it will overwrite the original, and if something goes wrong you will loose your data
Re: Find and replace string using batch
Posted: 07 Apr 2013 21:31
by pditty8811
abc0502 wrote:no, that does work here in this batch, as the path is used in the for command with the type command and the redirect sign < and >>
you must set the drive path
Edited:
by the way, make sure you change the OutFile name, or it will overwrite the original, and if something goes wrong you will loose your data
If it has to be the exact location of the output and input then I don't think I can use it. Because this will be an install.bat for a mod and its location could be anywhere, depending where people unzip the mod files to.
This is my delimma.