Dost Arora
Dost Arora

Reputation: 31

How to find the position of a character and extract a part of string using batch file

My batch file reads the value of a registry key and stores its value in the variable key. So the value happens to be "H:\KMSpico\KMSpico\Uninst.exe" /u0=KMSpico . I want to extract the part of this string that is enclosed in "". and store it in another variable, say uninstall. I could have easily done it by the following code: SET uninstall=%key:~1,30%. But the problem is that, the part before Uninst.exe, i.e. H:\KMSpico\KMSpico\ which specifies the location of Uninst.exe might change depending upon the location where the user has installed the software, therefore the length of the part that is to be extracted might change. But the above code is set to extract the string only up to 30 characters.

Therefore, I wanted to know if there is way, so that we can somehow know the position of second " , so as to know the number of characters to be extracted. Or if there is some another way, to do this.

Upvotes: 1

Views: 1371

Answers (2)

dbenham
dbenham

Reputation: 130919

The simple FOR loop preserves quoted values as a single string. You know it is the first value, so simply GOTO a label to break out of the loop after a single iteration.

for %%A in (%key%) do set "uninstall=%%A"&goto :break
:break

Upvotes: 1

npocmaka
npocmaka

Reputation: 57282

So you are using a pirated windows? :-)

try this

@echo off
rem :: I suppose you are capable to get the value from the registry and save is as variable.
set "reg_key="H:\KMSpico\KMSpico\Uninst.exe" /u0=KMSpico"
for /f "usebackq tokens=1" %%a in ('%reg_key%') do set "uninstaler=%%~a"
echo %uninstaler%

Upvotes: 0

Related Questions