Find and replace string using batch
Moderator: DosItHelp
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
IT WORKS!!!!!!!!!!
Endoro with the missing code.
ABC, THANK YOU MUCH FOR YOUR LONGTIME PATIENCE.
What is set line?
Endoro with the missing code.
ABC, THANK YOU MUCH FOR YOUR LONGTIME PATIENCE.
What is set line?
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
ahhh, I see.
Here:
line wasn't assigned.
Here:
Code: Select all
set "FinalLine=!line:%Replace%=%ReplaceWith%!"
Echo !FinalLine!)
line wasn't assigned.
Re: Find and replace string using batch
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
echo !line:%Replace%=%ReplaceWith%!
we set the whole variable to another variable and then echo the new one
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
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?
Re: Find and replace string using batch
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.
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
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?
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?
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
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
Re: Find and replace string using batch
The only space i can find is in line 14
make that line like this and test
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
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
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
Thank you for the response. I will test this out later tonight and give feedback.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
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!
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!