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

#46 Post by pditty8811 » 08 Apr 2013 02:05

same.

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

Re: Find and replace string using batch

#47 Post by abc0502 » 08 Apr 2013 02:07

ok, here it is:

Code: Select all

set "sh3directory=E:\Program Files (x86)\Ubisoft\SilentHunterIII"

REM Set These Variables
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 =%

set ReplaceWith=!sh3directory!



REM Take a Back-up copy of the InFile
Copy "%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 ( Echo !line:%Replace%=%ReplaceWith%!)
  )
)>>"%InFile%.tmp"
ENDLOCAL



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

PAUSE

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

Re: Find and replace string using batch

#48 Post by pditty8811 » 08 Apr 2013 02:12

same error.

Are you testing this? Can you test it aswell?

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

Re: Find and replace string using batch

#49 Post by Endoro » 08 Apr 2013 02:14

please show the output of "type %infile%".

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

Re: Find and replace string using batch

#50 Post by pditty8811 » 08 Apr 2013 02:15

Using pause to find out where the error is. It is somewhere in here:

Code: Select all

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%!)
  )
)>>"%InFile%.tmp"


Perhaps our variables don't work with this code.
Last edited by pditty8811 on 08 Apr 2013 02:19, edited 1 time in total.

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

Re: Find and replace string using batch

#51 Post by pditty8811 » 08 Apr 2013 02:17

It creates the .bak file but other than that the output does not change. Nothing changes in the .bak file and the error occurs before the .bak file is deleted or changed, or even renamed to the oringal BATCH_SCR.bat

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

Re: Find and replace string using batch

#52 Post by abc0502 » 08 Apr 2013 02:23

no, the .bak file is the original , the changes will be in the .bat file, check it.
also try to escape the sh3directory parentheses too

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

Re: Find and replace string using batch

#53 Post by pditty8811 » 08 Apr 2013 02:24

No changes to the original either

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

Re: Find and replace string using batch

#54 Post by pditty8811 » 08 Apr 2013 02:26

That is the issue again, escaping the parenthesis. It will be a variable made from user input. How do we escape that?

We'd have to echo sh3directory to a text file then give it ^ then grab it and assign it to ReplaceWith.

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

Re: Find and replace string using batch

#55 Post by abc0502 » 08 Apr 2013 02:31

escaping it automatically, i don't know about that now, but the important thing first is to make it work when escaping it.

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

Re: Find and replace string using batch

#56 Post by abc0502 » 08 Apr 2013 02:41

I think i know where the error is.
Try this now ( the parentheses problem still there )

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 "%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 ( Echo !line:%Replace%=%ReplaceWith%!)
  )
)>>"%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

we add a set command at the start and put the batch name in it and in the last command "Ren" replace "%InFile%" with "%FileName%" this will work if the sh3directory without parentheses

One step forward :)

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

Re: Find and replace string using batch

#57 Post by pditty8811 » 08 Apr 2013 02:56

Again with the user input?

The user input is much more complicated than that. Can we keep it out of this code and just assign sh3directory to something already?

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

Re: Find and replace string using batch

#58 Post by Endoro » 08 Apr 2013 03:02

Set !line! before echo it.

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

Re: Find and replace string using batch

#59 Post by abc0502 » 08 Apr 2013 03:04

pditty8811 wrote:Again with the user input?

The user input is much more complicated than that. Can we keep it out of this code and just assign sh3directory to something already?

what is the problem, the code take input and replace that input if it finds it in the file.

the only problem now if the input contain characters that needs to be escaped, and that is likely to be the program files.
i have an idea working on it not, we just use the short file names, that will truncate the program files name and remove the parentheses but I'm stuck on how, trying to make a function

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

Re: Find and replace string using batch

#60 Post by abc0502 » 08 Apr 2013 03:07

WOW :o Endoro
+100 for you it really works :lol: :lol:
i was going to code more over 20 line just to fix that :o :o

here is the code, don't excape any paranthese in the replace or in the sh3directory

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

Post Reply