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

#16 Post by abc0502 » 07 Apr 2013 21:38

if your problem is in the path, try this:
SET "InFile=%~dp0Dynamic Campaign\BATCHES\BATCH_SCR.bat"
SET "OutFile=%~dp0Dynamic Campaign\BATCHES\BATCH_SCR.bat"

The %~dp0 will fill the rest of the path till the folder the batch exist in,
But note that it will end with backslash so don't write the path like this:
SET "InFile=%~dp0\Dynamic Campaign\BATCHES\BATCH_SCR.bat"
with the blue backslash, or it will give you errors

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

Re: Find and replace string using batch

#17 Post by pditty8811 » 07 Apr 2013 22:11

Ok, that part works, but i'm still getting error.

It is

Code: Select all

\Ubisoft\SilentHunterIII1 was unexpected at this time


All the 4 variables are as they should be.

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

Re: Find and replace string using batch

#18 Post by pditty8811 » 07 Apr 2013 22:19

perhaps its the parenthesis in E:\Program Files (x86)\Ubisoft\SilentHunterIII"
that are the problem here.

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

Re: Find and replace string using batch

#19 Post by Endoro » 07 Apr 2013 22:20

You should escape parentheses.

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

Re: Find and replace string using batch

#20 Post by pditty8811 » 07 Apr 2013 22:24

Endoro wrote:You should escape parentheses.


That is another thing. How do I escape parenthesis if its a variable? sh3directory is a variable.

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

Re: Find and replace string using batch

#21 Post by abc0502 » 07 Apr 2013 22:25

pditty8811 wrote:perhaps its the parenthesis in E:\Program Files (x86)\Ubisoft\SilentHunterIII"
that are the problem here.

yes that's right.

inside the variable before each parentheses add ^

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

Re: Find and replace string using batch

#22 Post by pditty8811 » 07 Apr 2013 22:36

abc,

I did that and I didn't get an error, but still it did not replace the text in the file either.

But how to add ^ in variable? sh3directoy is a file path and will ahv parenthesis, depending if the person using the mod is running an x64 version of windows or not.

This is another delimma.

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

Re: Find and replace string using batch

#23 Post by abc0502 » 07 Apr 2013 22:44

it's not a problem, you can make your batch check for the windows version when it starts and depending on the bit version it set the variable.

Replace This command:

Code: Select all

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

with:

Code: Select all

REM Identify windows bit version
IF EXIST "%SYSTEMDRIVE%\Program Files (x86)" (
   SET "sh3directory=E:\Program Files ^(x86^)\Ubisoft\SilentHunterIII"
) ELSE (
   SET "sh3directory=E:\Program Files\Ubisoft\SilentHunterIII"
)

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

Re: Find and replace string using batch

#24 Post by pditty8811 » 07 Apr 2013 22:48

abc0502 wrote:it's not a problem, you can make your batch check for the windows version when it starts and depending on the bit version it set the variable.

Replace This command:

Code: Select all

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

with:

Code: Select all

REM Identify windows bit version
IF EXIST "%SYSTEMDRIVE%\Program Files (x86)" (
   SET "sh3directory=E:\Program Files ^(x86^)\Ubisoft\SilentHunterIII"
) ELSE (
   SET "sh3directory=E:\Program Files\Ubisoft\SilentHunterIII"
)


I wish it was that easy. The hardrive letter might be e but it is likely to be C, or it might even be d.

Is there another way to do this?

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

Re: Find and replace string using batch

#25 Post by abc0502 » 07 Apr 2013 22:51

That's why we use "%SYSTEMDRIVE%" instead of hard-coding the drive letter, as it is almost every user has the program files folder in the same drive with windows.
unless the user change the system and put the program files on another drive and that is very very very very small possibility

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

Re: Find and replace string using batch

#26 Post by pditty8811 » 07 Apr 2013 23:45

abc0502 wrote:That's why we use "%SYSTEMDRIVE%" instead of hard-coding the drive letter, as it is almost every user has the program files folder in the same drive with windows.
unless the user change the system and put the program files on another drive and that is very very very very small possibility


I do. I have 6 hdd though.

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

Re: Find and replace string using batch

#27 Post by abc0502 » 07 Apr 2013 23:50

The program files folder location will depend on the current working system.
So do you mean that you have the system "windows" running on C for example nad it's program files folder on another partition ?

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

Re: Find and replace string using batch

#28 Post by pditty8811 » 08 Apr 2013 00:08

abc0502 wrote:The program files folder location will depend on the current working system.
So do you mean that you have the system "windows" running on C for example nad it's program files folder on another partition ?


I have my OS and documents on C, non-games on D and games installed on E. The other drives are for various types of storage. I'd assume this is common in the gaming arena.

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

Re: Find and replace string using batch

#29 Post by abc0502 » 08 Apr 2013 00:13

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"
)
Last edited by abc0502 on 08 Apr 2013 00:20, edited 1 time in total.

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

Re: Find and replace string using batch

#30 Post by pditty8811 » 08 Apr 2013 00:18

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.


Is there a way to find and replace while using parenthesis?

I've looked at a few other examples here and some I don't understand. Like this one:

viewtopic.php?f=3&t=2710

or this one:

viewtopic.php?f=3&t=3855

Not sure how to implement those....

Post Reply