Find and replace string using batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Find and replace string using batch

#31 Post by abc0502 » 08 Apr 2013 00:21

i edited my last post check it till i check the topics

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

Re: Find and replace string using batch

#32 Post by pditty8811 » 08 Apr 2013 00:26

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:
wait :idea: i 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 problem

Code: 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.

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

Re: Find and replace string using batch

#33 Post by abc0502 » 08 Apr 2013 00:35

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

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

Re: Find and replace string using batch

#34 Post by pditty8811 » 08 Apr 2013 00:46

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.

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

Re: Find and replace string using batch

#35 Post by abc0502 » 08 Apr 2013 00:55

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

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

Re: Find and replace string using batch

#36 Post by abc0502 » 08 Apr 2013 01:01

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.

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

Re: Find and replace string using batch

#37 Post by pditty8811 » 08 Apr 2013 01:04

Here is an example and a test I made. This is an excerpt from BATCH_SCR.bat, the file we are searching in.

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.

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

Re: Find and replace string using batch

#38 Post by abc0502 » 08 Apr 2013 01:13

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.

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

Re: Find and replace string using batch

#39 Post by pditty8811 » 08 Apr 2013 01:20

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?

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

Re: Find and replace string using batch

#40 Post by abc0502 » 08 Apr 2013 01:25

in this case it will be easy,
just few minutes

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

Re: Find and replace string using batch

#41 Post by abc0502 » 08 Apr 2013 01:45

Try This :

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

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

Re: Find and replace string using batch

#42 Post by pditty8811 » 08 Apr 2013 01:54

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.

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

Re: Find and replace string using batch

#43 Post by pditty8811 » 08 Apr 2013 02:01

This is what I changed it to:

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.

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

Re: Find and replace string using batch

#44 Post by abc0502 » 08 Apr 2013 02:04

what is the error??

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.

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

Re: Find and replace string using batch

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

Code: Select all

\Ubisoft\SilentHunterIII1 was unexpected at this time

Post Reply