Find and replace string using batch
Moderator: DosItHelp
Re: Find and replace string using batch
i edited my last post check it till i check the topics
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
abc0502 wrote:if the batch is just for you you can adjust it as you want,
but as standard the program files folder or program files (x86) always on the same drive the windows exist.
so the code i posted earlier should work fine for 99.99% ( if you will distribute the batch ).
The %systemdrive% is the driver letter where the windows is installed.
Edited:
waiti think i understand what you was talking about.
if you mean that the E:\ in the sh3directory can be any other drive letter, then this should solve the problemCode: Select all
REM Identify windows bit version
IF EXIST "%SYSTEMDRIVE%\Program Files (x86)" (
SET "sh3directory=%SYSTEMDRIVE%\Program Files (x86)\Ubisoft\SilentHunterIII"
) ELSE (
SET "sh3directory=%SYSTEMDRIVE%\Program Files\Ubisoft\SilentHunterIII"
)
I would have to set like this:
Code: Select all
IF EXIST "%SYSTEMDRIVE%\Program Files ^(^x86^)^" (
SET "sh3directory=%SYSTEMDRIVE%\Program Files (x86)\Ubisoft\SilentHunterIII"
) ELSE (
SET "sh3directory=%SYSTEMDRIVE%\Program Files\Ubisoft\SilentHunterIII"
)
Right?
But isn't %systemdrive% always the OS drive letter? NOt the letter of what sh3directory actually is. SH3 is a game. And the variable !sh3directory! is derived from user input.
The user input code is here:
Code: Select all
set /p sh3directory=Where is Silent Hunter 3 installed ^?^ HINT: the directory where "sh3.exe" is located. Example: C:\Program Files (x86)\Ubisoft\SilentHunterIII :
if exist !sh3directory!\sh3.exe (
goto sh3saves
)
echo sh3.exe does not reside in that directory. Please try again.
echo Correct example: C:\Program Files (x86)\Ubisoft\SilentHunterIII
goto sh3directory
That is how sh3directory is assigned. They put in the filepath of the game silent hunter 3.
Re: Find and replace string using batch
ok, that make sense now 
I thought that you have the sh3directory always in the program files folder so it must be in the system drive.
but in this case, why you need to set the drive letter if the user will input the path

I thought that you have the sh3directory always in the program files folder so it must be in the system drive.
but in this case, why you need to set the drive letter if the user will input the path
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
right. So we know sh3directory will always be defined correctly. So how do I find a filepath and replace it with sh3directoy. My orignal delimma.
Re: Find and replace string using batch
in the file you will replace the line in, can you post a sample text of the line and the one before and the one after so i can find it
Re: Find and replace string using batch
one more thing can you describe the problem from the start cays when i review the other posts again i find things don't fit together.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
Here is an example and a test I made. This is an excerpt from BATCH_SCR.bat, the file we are searching in.
notice E:\Program Files (x86)\Ubisoft\SilentHunterIII1
We'd like to change that to sh3directory, which I set as E:\Program Files (x86)\Ubisoft\SilentHunterIII
Without the 1 at the end. This is just a little test to see if we can make code that find and replaces filepaths.
Code: Select all
:nextpart
if not exist "E:\Program Files (x86)\Ubisoft\SilentHunterIII\Dynamic Campaign\Careers\!careerdir2!\Campaign_SCR.mis" (
mkdir "E:\Program Files (x86)\Ubisoft\SilentHunterIII1\Dynamic Campaign\Careers\!careerdir2!"
copy /y "E:\Program Files (x86)\Ubisoft\SilentHunterIII\data\Campaigns\Campaign\Campaign_SCR.mis" "E:\Program Files (x86)\Ubisoft\SilentHunterIII\Dynamic Campaign\Careers\!careerdir2!\Campaign_SCR.mis"
)
notice E:\Program Files (x86)\Ubisoft\SilentHunterIII1
We'd like to change that to sh3directory, which I set as E:\Program Files (x86)\Ubisoft\SilentHunterIII
Without the 1 at the end. This is just a little test to see if we can make code that find and replaces filepaths.
Re: Find and replace string using batch
so all what is needed to be changed is "SilentHunterIII1" to "SilentHunterIII" ?
Edited:
Unless we know the exact words we will change it will be impossible.
Edited:
Unless we know the exact words we will change it will be impossible.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
abc0502 wrote:so all what is needed to be changed is "SilentHunterIII1" to "SilentHunterIII" ?
Well if sh3directory said: C:\Program Files\Ubisoft\SilentHunterIII
Then I'd have to replace E:\Program Files (x86)\Ubisoft\SilentHunterIII1
with the former (sh3directory).
The former (variable) will always change, but the latter will always be the same. In this example it is :
E:\Program Files (x86)\Ubisoft\SilentHunterIII1
So yes, we know the exact words of what we are to find, but what we are to replace it with will be a changing variable.
Does this make sense?
Re: Find and replace string using batch
in this case it will be easy,
just few minutes
just few minutes
Re: Find and replace string using batch
Try This :
The code ask for the path, then check if this is the correct path by checking if the sh3.exe exist or not.
now after check comes ok, it will take a back-up copy of the InFile, then replace the location we set at line 5 with the location we take from the user input and out put all that to the .tmp file.
after all done , delete the InFile and rename the .tmp file with the same name as the InFile was.
test it and we will continue from there
Code: Select all
@Echo OFF
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 =%
Rem Take User Input [ the sh3directory ]
:Menu
Echo.
Echo Enter Silent Hunter III Folder Path
Echo.
SET "ReplaceWith="
SET /P "ReplaceWith= > "
Rem check if the game exe file exist or not, if not it will alert the user and loop back again
IF not Exist "%ReplaceWith%\sh3.exe" (
Echo Error ... Incorrect Game Path
Pause
CLS
Goto :Menu
)
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
The code ask for the path, then check if this is the correct path by checking if the sh3.exe exist or not.
now after check comes ok, it will take a back-up copy of the InFile, then replace the location we set at line 5 with the location we take from the user input and out put all that to the .tmp file.
after all done , delete the InFile and rename the .tmp file with the same name as the InFile was.
test it and we will continue from there
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
I'm going to take out the if exist file part, as I already do that in the user input. It would be extra code.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
This is what I changed it to:
it is error.
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
it is error.
Re: Find and replace string using batch
what is the error??
change "!sh3directory!" to "%sh3directory%"
Edited, sorry forgot to escape the parentheses, i will change it up
change "!sh3directory!" to "%sh3directory%"
Edited, sorry forgot to escape the parentheses, i will change it up
Last edited by abc0502 on 08 Apr 2013 02:06, edited 1 time in total.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find and replace string using batch
Code: Select all
\Ubisoft\SilentHunterIII1 was unexpected at this time