Find and replace string using batch
Moderator: DosItHelp
Re: Find and replace string using batch
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
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
same error.
Are you testing this? Can you test it aswell?
Are you testing this? Can you test it aswell?
Re: Find and replace string using batch
please show the output of "type %infile%".
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
Using pause to find out where the error is. It is somewhere in here:
Perhaps our variables don't work with this code.
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
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
Re: Find and replace string using batch
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
also try to escape the sh3directory parentheses too
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
No changes to the original either
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
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.
We'd have to echo sh3directory to a text file then give it ^ then grab it and assign it to ReplaceWith.
Re: Find and replace string using batch
escaping it automatically, i don't know about that now, but the important thing first is to make it work when escaping it.
Re: Find and replace string using batch
I think i know where the error is.
Try this now ( the parentheses problem still there )
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
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

-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
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?
The user input is much more complicated than that. Can we keep it out of this code and just assign sh3directory to something already?
Re: Find and replace string using batch
Set !line! before echo it.
Re: Find and replace string using batch
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
Re: Find and replace string using batch
WOW
Endoro
+100 for you it really works
i was going to code more over 20 line just to fix that
here is the code, don't excape any paranthese in the replace or in the sh3directory

+100 for you it really works


i was going to code more over 20 line just to fix that


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