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

Re: Find and replace string using batch

#61 Post by pditty8811 » 08 Apr 2013 03:16

IT WORKS!!!!!!!!!!

Endoro with the missing code.

ABC, THANK YOU MUCH FOR YOUR LONGTIME PATIENCE.

What is set line?

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

Re: Find and replace string using batch

#62 Post by pditty8811 » 08 Apr 2013 03:17

ahhh, I see.

Here:

Code: Select all

    set "FinalLine=!line:%Replace%=%ReplaceWith%!"
      Echo !FinalLine!)

line wasn't assigned.

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

Re: Find and replace string using batch

#63 Post by abc0502 » 08 Apr 2013 03:19

instead of directly echo the line like this:
echo !line:%Replace%=%ReplaceWith%!

we set the whole variable to another variable and then echo the new one

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

Re: Find and replace string using batch

#64 Post by pditty8811 » 08 Apr 2013 03:23

abc0502 wrote:instead of directly echo the line like this:
echo !line:%Replace%=%ReplaceWith%!

we set the whole variable to another variable and then echo the new one


Why was that the issue you think, if !line! had a value already?

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

Re: Find and replace string using batch

#65 Post by abc0502 » 08 Apr 2013 03:43

i don't know exactly, but if the cmd put the value it self it can escape it but if you give a value from out to the cmd you have to escape it.

like a problem i had before in Unicode names , i can't pass Unicode characters to the batch or set it inside the batch, but after i used a for command and a dir command to get the names from the cmd and pass it to my code it worked.
i don't know the reason for sure but that what is close enough for me any way. :?

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

Re: Find and replace string using batch

#66 Post by pditty8811 » 10 Apr 2013 18:56

Ooops. It appears the code needs a bit more tweaking.

It finds and replaces but now it is adding unnecessary spaces at the end of the replaced string.

For example:
I want to replace this:
"E:\Program Files (x86)\Ubisoft\SilentHunterIII1"

with this:
"E:\Program Files (x86)\Ubisoft\SilentHunterIII"

in this:
"E:\Program Files (x86)\Ubisoft\SilentHunterIII1\files\boats\files"


Now, the output is this:
"E:\Program Files (x86)\Ubisoft\SilentHunterIII \files\boats\files"

A whole bunch of spaces are after the replaced string.

But I'd like it to be this:
"E:\Program Files (x86)\Ubisoft\SilentHunterIII\files\boats\files"

No spaces after the replaced string.

What is the issue?

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

Re: Find and replace string using batch

#67 Post by pditty8811 » 11 Apr 2013 00:26

This is the code I have the issue with:

Code: Select all

@Echo OFF

REM Set These Variables
SET "FileName=BATCH_SCR.bat"
SET "InFile=%~dp0Dynamic Campaign\BATCHES\BATCH_SCR.bat"         %= File That needs to be changed =%
SET "Replace=E:\Program Files (x86)\Ubisoft\SilentHunterIII1"      %= The path in the file that need changing =%

Rem Take User Input [ the sh3directory ]
:Menu
Echo.
Echo Enter Silent Hunter III Folder Path
Echo.
SET "ReplaceWith="
SET /P "ReplaceWith=> "

REM Back up file
Copy /y "%InFile%" "%InFile%.bak" >NUL

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 (
      set "FinalLine=!line:%Replace%=%ReplaceWith%!"
      Echo !FinalLine!)
  )
)>>"%InFile%.tmp"
ENDLOCAL

REM Delete Original and Rename the InFile.tmp with the InFile original Name
Del /F /Q "%InFile%" >NUL
Ren "%InFile%.tmp" "%FileName%"

PAUSE

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

Re: Find and replace string using batch

#68 Post by abc0502 » 11 Apr 2013 06:23

The only space i can find is in line 14

Code: Select all

SET /P "ReplaceWith=> "

make that line like this and test

Code: Select all

SET /P "ReplaceWith=>"


Edited:
Or f it happens in all your tests we can remove the last character by changing the "!FinalLine!" to "!FinalLine:~0,-1!".
that will remove the space too, but it can cause problems if the space was because of a user input

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

Re: Find and replace string using batch

#69 Post by pditty8811 » 11 Apr 2013 17:23

Thank you for the response. I will test this out later tonight and give feedback.

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

Re: Find and replace string using batch

#70 Post by pditty8811 » 13 Apr 2013 02:20

Hi,

It actually wasn't your code. It was when I adjusted your code with the assignment of ReplaceWith and a variable. I forgot ot put quotes around both of them.

Thanks all!

Post Reply