How to find a replace a string using batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Murali
Posts: 4
Joined: 30 Dec 2008 01:05

How to find a replace a string using batch script

#1 Post by Murali » 30 Dec 2008 01:22

I am trying to uninstall an application using a batch script and i need to fetch the uninstaller string from reg keys, edit the uninstaller string from the regfile.

My requirement is :

I need to replace
Original string:
"UninstallString"="C:\\WINDOWS\\st6unst.exe -n \"C:\\Program Files\\Myapp\\ST6UNST.LOG\" "
with new sring:

"UninstallString"=C:\\WINDOWS\\st6unst.exe -n "C:\\Program Files\\Myapp\\ST6UNST.LOG"

that is remove \ after -n and then remove \ after ST6UNST.LOG and then the doublwe quotes.

Advance thanks.

Murali
Posts: 4
Joined: 30 Dec 2008 01:05

#2 Post by Murali » 30 Dec 2008 01:24

Also i need to store the string C:\\WINDOWS\\st6unst.exe -n "C:\\Program Files\\Myapp\\ST6UNST.LOG" in a variable

Thanks.

Gimson
Posts: 3
Joined: 29 Dec 2008 06:01

#3 Post by Gimson » 30 Dec 2008 03:08

Murali wrote:Also i need to store the string C:\\WINDOWS\\st6unst.exe -n "C:\\Program Files\\Myapp\\ST6UNST.LOG" in a variable

Thanks.


For storing a registry value in a variable I guess you can try the following approach:

Code: Select all

@ECHO OFF
set reg_branch="HKEY_LOCAL_MACHINE\SOFTWARE\Skype\Phone"
set reg_value=SkypePath

for /f "tokens=2,*" %%a in ('reg query %reg_branch% /v %reg_value% ^| findstr %reg_value% ') do ( set yourvariable=%%b )

ECHO %yourvariable%

pause


In my case the bat file returns :

Code: Select all

C:\Program Files\Skype\Phone\Skype.exe


so it seems to be working fine.

I of course used here some registry entry created by Skype upon install.
If I remember correctly, I found this on www.robvanderwoude.com - all credits go to the author.

Murali
Posts: 4
Joined: 30 Dec 2008 01:05

#4 Post by Murali » 30 Dec 2008 06:31

I used the following script to tp get the value and it works fine.

@echo off
REGEDIT /E MyAppReg.reg "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ST6UNST #1"


FOR /F "tokens=1* delims==" %%A IN ('TYPE MyappReg.reg ^| FINDSTR.EXE /I /B /R /C:"\"UninstallString\"="') DO (
SET RegKey=%%~A
SET RegVal=%%~B
)

%RegVal%
pause



But
Before executing the uninstallation command i need to edit the string as below and run the command
RegVal contains string

"C:\\WINDOWS\\st6unst.exe -n \"C:\\Program Files\\Myapp\\ST6UNST.LOG\"


i should change it to :
"C:\\WINDOWS\\st6unst.exe -n "C:\\Program Files\\Myapp\\ST6UNST.LOG"


i.e.. remove \ after -n and then remove \ after ST6UNST.LOG and then the double quotes.
then i should execute the command.

Thanks.

Gimson
Posts: 3
Joined: 29 Dec 2008 06:01

#5 Post by Gimson » 30 Dec 2008 07:44

Murali wrote:
"C:\\WINDOWS\\st6unst.exe -n "C:\\Program Files\\Myapp\\ST6UNST.LOG"



Are you sure you need to have three quote marks here ?


EDIT

well, here goes :

Code: Select all

@ECHO OFF

:: here goes ...

SET var="C:\\WINDOWS\\st6unst.exe -n "C:\\Program Files\\Myapp\\ST6UNST.LOG"

ECHO original string
ECHO.
ECHO %var%
ECHO.

SET substr1=%VAR:~1,28%
SET substr2=%VAR:~31,37%

SET your_var=%substr1%"%substr2%"

ECHO new string
ECHO.
ECHO %your_var%
ECHO.
pause

exit


I hope this is what you needed and will be able to modify it in case I misunderstood anything

works on XP, dunno about different versions of Windows

Murali
Posts: 4
Joined: 30 Dec 2008 01:05

#6 Post by Murali » 30 Dec 2008 08:40

EXCELLENT!!!!!!!!!!!!! Works exactly what i expected Many thanks for your reply and many many thanks for your fast response. I closed this issue in a single day.


Thanks,
Murali.

Post Reply