Find and replace string using batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Find and replace string using batch

#1 Post by pditty8811 » 06 Apr 2013 19:32

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.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Find and replace string using batch

#2 Post by Endoro » 07 Apr 2013 07:52

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"


Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Find and replace string using batch

#3 Post by Squashman » 07 Apr 2013 08:42

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Find and replace string using batch

#4 Post by dbenham » 07 Apr 2013 09:58

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

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find and replace string using batch

#5 Post by pditty8811 » 07 Apr 2013 18:23

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find and replace string using batch

#6 Post by abc0502 » 07 Apr 2013 19:06

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
Last edited by abc0502 on 07 Apr 2013 19:54, edited 1 time in total.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find and replace string using batch

#7 Post by pditty8811 » 07 Apr 2013 19:38

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find and replace string using batch

#8 Post by abc0502 » 07 Apr 2013 19:54

sorry for that, i changed the error
it was in
Echo !line:%Replace%=%ReplaceWith%!

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find and replace string using batch

#9 Post by pditty8811 » 07 Apr 2013 20:56

I'm getting an error with that. Are you sure?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find and replace string using batch

#10 Post by abc0502 » 07 Apr 2013 21:03

have you set the replace and replacewith variables ?
post what u used

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find and replace string using batch

#11 Post by pditty8811 » 07 Apr 2013 21:24

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
Last edited by pditty8811 on 07 Apr 2013 21:25, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find and replace string using batch

#12 Post by abc0502 » 07 Apr 2013 21:25

you didn't set the drive letter where the "Dynamic Campaign\BATCHES\BATCH_SCR.bat"" is.
is it C:\ or D:\ .. etc,

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find and replace string using batch

#13 Post by pditty8811 » 07 Apr 2013 21:26

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Find and replace string using batch

#14 Post by abc0502 » 07 Apr 2013 21:27

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

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find and replace string using batch

#15 Post by pditty8811 » 07 Apr 2013 21:31

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.

Post Reply