What I'm trying to do is have a batch script return the uninstall link for a program. So basically I want something like this:
Select UninstallString from HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall where DisplayName='Sublime Text 1.0"
I'm using
reg query HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall /S^|find " DisplayName"
to initially get a list of programs, which then get put in a menu, then I select the program to uninstall and it's supposed to go to that program's registry in HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall and get it's UninstallString value
Try this:
#echo off&setlocal enabledelayedexpansion
set "regroot=HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
set "file=%~dpn0.txt"
set /a fcount=0
for /f "delims==" %%i in ('set $ 2^>nul') do set "%%i="
if exist "%file%" (
for /f "usebackqtokens=1*delims=|" %%i in ("%file%") do (
set /a fcount+=1
set "$d%%j=%%j"
set "$u%%j=%%i"
)
goto:menu
)
echo(building "%file%", please wait
for /f "delims=" %%i in ('reg query "%regroot%"') do (
set "DN="& set "US="
for /f "tokens=2*" %%j in ('reg query "%regroot%\%%~ni" /v DisplayName 2^>nul^|find /i "DisplayName"') do set "DN=%%~k"
for /f "tokens=2*" %%j in ('reg query "%regroot%\%%~ni" /v UninstallString 2^>nul^|find /i "UninstallString"') do set "US=%%~k"
if not "!DN!"=="" if not "!US!"=="" if not defined $d!DN! (
>>"%file%" echo(!US!^|!DN!
set /a fcount+=1
set "$d!DN!=!DN!"
set "$u!DN!=!US!"
<nul set/p"=."
)
)
echo(
:menu
echo(%fcount% programs with uninstall strings found.
:loop
set /a pcount=0
set "program="
set /p "program=type a program name (q=quit): "
if not defined program goto:loop
if "%program%"=="q" goto:eof
echo(
for /f "tokens=2delims==" %%i in ('set $d ^|findstr !program! 2^>nul') do (
echo(%%i
echo(!$u%%i!
echo(
set /a pcount+=1
)
if %pcount% equ 0 (echo(!program! not found.) else echo(%pcount% program(s^) found.
goto:loop
You can use some of findstr's REGEX capabilities (eg. /i for case insensitive search). Please note: to search for all programs starting with "M" you can use "^$dM" or /b $dM. Searchable strings have always a leading $d.
Try this "two-liner":
for /f "tokens=7 delims=\" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /S /F "MySoftware" ^| find "{"') do set ProgramUninstallRegKey=%%a
for /f "skip=1 tokens=3*" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%ProgramUninstallRegKey% /V "UninstallString"') do set ProgramUninstallString=%%a %%b
Related
#echo off
cd /d D:\
setlocal enabledelayedexpansion
set /p "var=v3 "
set "file=Rev.bat"
for /F "tokens=1,* delims==" %%i in ('findstr "%var%" Rev.bat') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set /a sequence=%%~j+1
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
I tried this code it doesn't find the string
ok, so if I understand you correctly, this is what you're looking for:
#echo off
cd /d D:\
setlocal enabledelayedexpansion
set "file=rev.bat"
for /F "tokens=1,* delims==" %%i in ('findstr "%~1" rev.bat') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set /a sequence=%%~j+1
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
Explanation. We get the file content, using find to assign row id's Then we do search and replace and simply remove the row id's. Then write back everything to the file including the updated version.
note, you need to run the script with a parameter, i.e. script.cmd v3
Edit. to do more than one variable at a time by request, do:
#echo off
cd /d D:\
setlocal enabledelayedexpansion
:Begin
set /p "var=enter variable to replace [.ie v1, v2, v3]: "
set "file=rev.bat"
for /F "tokens=1,* delims==" %%i in ('findstr "%var%" rev.bat') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set /a sequence=%%~j+1
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
I'm writing a script that needs to find a numerical values or digits in a text file and replace it with input from my keyboard.
I have searched on the internet and just can't find the correct way to do it.
The text file contains the following:
C:\Progra~1\Java\userapp\fundays\jv10_80cl1\test
i need to change the "80cl1" part to new values eg: 80cl1 changes to 84cl3
:begin1
setlocal ENABLEEXTENSIONS
setlocal ENABLEDELAYEDEXPANSION
SET /P E= new value 1:
FOR /F "tokens=1 delims=:" %%a in (install.txt) do findstr /r "^[1-9][0-9]*$"
echo "%%a"
SET /P R= New value 2:
FOR /F "tokens=1 delims=:" %%a in (install.txt) do findstr /r "^[1-9][0-9]*$"
echo "%%a"
So, the script needs to find those values and change it via keyboard input.
Something like this should help. You can change the set /p to not promp and just give it a value, either way will work:
#echo off
setlocal enabledelayedexpansion
set inputfile=install.txt
set /p "_strfind=Enter value to search?"
set /p "_strinsert=Replacement value?"
for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do (
set "str=%%a"
set "str=!str:%_strfind%=%_strinsert%!"
set "str=!str:*]=!"
>>%inputfile% echo(!str!
)
If you do not want to be pompted, just set the values before hand:
#echo off
setlocal enabledelayedexpansion
set inputfile=install.txt
set "_strfind=80cl1"
set "_strinsert=84cl3"
for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do (
set "str=%%a"
set "str=!str:%_strfind%=%_strinsert%!"
set "str=!str:*]=!"
>>%inputfile% echo(!str!
)
If by any chance your file might contain ! it will not replicate them to file, then simply use this:
#echo off
set "inputfile=ip.txt"
set /p "_strfind=string to replace?"
set /p "_strinsert=replacement string?"
for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do (
set "str=%%a"
setlocal enabledelayedexpansion
set "str=!str:%_strfind%=%_strinsert%!"
call set "str=%%str:*]=%%"
>>%inputfile% echo(!str!
endlocal
)
Edit, as per your comment:
#echo off
set "inputfile=install.txt"
set /p "_strfind=string to replace? "
set /p "_strinsert=replacement string? "
set /p "_strfind2=enter Executable numeric value to replace? "
set /p "_strinsert2=enter the replacement value for executable? "
set "_strfind2=%_strfind2%.exe"
set "_strinsert2=%_strinsert2%.exe"
for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do (
set "str=%%a"
setlocal enabledelayedexpansion
set "str=!str:%_strfind%=%_strinsert%!"
set "str=!str:%_strfind2%=%_strinsert2%!"
call set "str=%%str:*]=%%"
>>%inputfile% echo(!str!
endlocal
)
I am currently trying to use reg query commands to find the data value "teststring" in a value with a random name in HKCU\Software\random characters. I want the script to find the teststring value and then delete the parent key HKCU\Software\random characters. I have tried various ways with scripting and this is what I have so far, however I keep getting syntax errors and can't get reg query to find what I need it to:
#echo off
setlocal enabledelayedexpansion
set Key="HKCU\Software"
set STRING="teststring"
for /f "delims=" %%a in ('reg.exe query "%Key%" /f "%STRING%" /d /s') do (
call :GetValueName Value "%%a"
ECHO reg.exe delete "%Key%" /v "!Value!" /f
)
goto :eof
:GetValueName
set Return=
for %%a in (%~2) do (
if "%%a"=="REG_SZ" (
set %1=!Return:~1!&goto :eof
) else (
set Return=!Return! %%a
)
)
:eof
Here's a simpler way to do it.
Remove the ECHO (not the echo) AFTER you are satisfied with what is displayed.
#echo off
set "Key=HKCU\Software"
set "STRING=teststring"
for /f "delims=" %%a in ('reg query "%Key%" /v test /s /d /f "%STRING%" /t REG_SZ') do (
echo %%a | findstr /v "REG_SZ" | findstr /v /c:"End of search:" >nul && ECHO reg.exe delete "%%a" /f
)
pause
How can i search a string and replace it with a variable.
i would like to search version="1.37.0" but the version number could be anything. And there are two "version=" string in package.xml but i would like to replace second one.
how can i search version="x.x.x" and replace it with version="$variable"?
is there any one liner?
i did try to use something like this to search:
findstr "version="[0-9].[0-9].[0-9]" package.xml
and also same thing for desrciption="$variable1"
package.xml
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest>
<Package name="audio"
description="something. . .."
version="1.37.0"
comment="">
</Package>
</PackageManifest>
ad hoc solution, but ...
Edited to adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
rem Check input parameters. Needed the input file, the version and the description
if "%~3"=="" goto :eof
set "file=%~1"
set "newVersion=%~2"
set "newDescription=%~3"
rem Read the file into memory
for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "%file%"') do (
set /a "line=1000+%%a*10"
setlocal enabledelayedexpansion
for %%c in (!line!) do endlocal & set "l_%%c=|%%b"
)
rem %%a will search the required lines
rem %%c remove blanks at the start of the line
rem %%d get the key name
for /f "tokens=2,* delims=_=|" %%a in (
'set l_1 ^| findstr /i /r /c:"^[^<]*version=" /c:"description=" /c:"^[^<]*<Package"'
) do for /f %%c in ("%%b") do for /f "delims==" %%d in ("%%c") do (
if /i "%%d"=="description" ( set "value=%newDescription%" & set "newDescription="
) else if /i "%%d"=="version" ( set "value=%newVersion%" & set "newVersion="
) else if /i "%%d"=="<Package" ( set "packageLine=%%a" & set "value="
) else set "value="
if defined value ( setlocal enabledelayedexpansion
for /f "delims=" %%z in ("!value!") do ( endlocal
for /f tokens^=1^,2^,^*^ delims^=^" %%e in ("%%b") do set "l_%%a=|%%e"%%z"%%g"
)
)
)
rem Include the missing values
set /a "packageLine+=1"
if defined newDescription set "l_%packageLine%=| description="%newDescription%""
set /a "packageLine+=1"
if defined newVersion set "l_%packageLine%=| version="%newVersion%""
rem Output the changed information to console
for /f "tokens=1,* delims=|" %%a in ('set l_1') do echo(%%b
rem Save to file
>"%file%" (for /f "tokens=1,* delims=|" %%a in ('set l_1') do echo(%%b)
Try this :
#echo off
set $FindStr=Version="x.x.x"
set $ReplString=Version="y.y.y"
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type test.xml') do (
set $Ver=%%a
set $Ver=!$Ver: =!
If /i !$Ver!==%$FindStr% set $Ver=%$ReplString%
echo !$Ver! ) >> Output.xml
Very simplist but a good base
Edit :
This will ask for the version value of the second matched version=
#echo off
set "$FindStr=Version="
set $c=1
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type test.xml') do (
set $Ver=%%a
set $Ver=!$Ver: =!
If /i "!$Ver:~0,8!"=="%$FindStr%" (
if !$c! GTR 1 (
set /p "$NewVer=Enter New version : "
set $Ver=%$FindStr%!$NewVer!)
set /a $c+=1)
echo !$Ver! >> Output.xml)
The input file is text.xml and the ouputFile Output.xml
#ECHO OFF
#setlocal enabledelayedexpansion
SET InFile=test.txt
FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
:FindString
SET String=%~1
SET String=%String:*wordA =%
SET String=%String: wordB=`%
FOR /F "tokens=1 delims=`" %%A IN ('%String%') DO ECHO %%A
Here I just written a code to replace the word with my desired character
When I am running these code I am getting the error saying
. was unexpected at this time
You were close. See mods to your code below. If you really want the first word after wordB... this will do it.
Don't enable delayed expansion if you don't need it because that just messes you up if there are ! in the text.
#echo off
SET InFile=test.txt
FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
goto :eof
:FindString
SET String=%~1
SET String=%String:*wordA =%
SET String=%String:wordB =%
FOR /F "tokens=1" %%A IN ('echo.%String%') DO ECHO.%%A
:goto :eof
I'm not sure, what you want, but you can try this:
#echo off &setlocal
SET "InFile=test.txt"
echo(wordA wordB>"%InFile%"
FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
goto:eof
:FindString
SET "String=%~1"
SET "String=%String:*wordA =%"
SET "String=%String: wordB=`%"
FOR /F "tokens=1 delims=`" %%A IN ("%String%") DO ECHO %%A