Batch file, check if string contains another string with special characters - windows

I am trying to write a batch file that will append some items to the proxy exceptions list.
I've currently got this:-
echo off
setlocal
for /f "tokens=2*" %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride') do set "var=%%b"
set "devlocal=;*.dev.local;"
set "local=*.local;"
set "exceptions=%var%%devlocal%%local%"
echo "%exceptions%"
pause
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /t REG_SZ /d "%exceptions%" /f
pause
This runs okay. But what I want to do is firstly check to see if *.dev.local already exists in the exceptions and if so do NOT add it in. The same for *.local.
I cant seem to use FINDSTR as it doesent support special characters such as *.

Here's one way to add terms if they don't exist.
It's not efficient but for two terms it will work fine.
echo "%var%"|findstr /L /i "*.dev.local" >nul || set "var=%var%;*.dev.local;"
echo "%var%"|findstr /L /i "*.local" >nul || set "var=%var%;*.local"

you can replace the strings with an empty string:
echo off
setlocal
for /f "tokens=2*" %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride') do set "var=%%b"
set "devlocal=;*.dev.local;"
set "local=*.local;"
REM SET %v:str1=str2% returns the value of variable v in which all occurrences of str1 are replaced by str2
REM Note that we use an empty string in place of str2
SET var=%var:;*.dev.local;=%
SET var=%var:*.local;=%
set "exceptions=%var%%devlocal%%local%"
echo "%exceptions%"
pause
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyOverride /t REG_SZ /d "%exceptions%" /f
pause

Related

Have a batch file check the value of a registry key, echo if it is set how I want and change it if it is not

I want to have a script that will check the following Registry Key for
HKLM\SYSTEM\CurrentControlSet\Control\DeviceGuard\Scenarios\HypervisorEnforcedCodeIntegrity
Enabled Reg_DWORD 0x0
If the value is set to 0x0, I want the script to echo Hypervisor Enforced Code Integrity is Disabled
If the value is set to anything else, I want to script to echo a statement, and then edit the registry value.
This is what I have so far, my issue is that the for /f command keeps erroring out. Any help would be really appreciated.
Set regkey="HKLM\SYSTEM\CurrentControlSet\Control\DeviceGuard\Scenarios\HypervisorEnforcedCodeIntegrity"
Set Value=Enabled
for /f "tokens=2* delims= "
%%A IN (reg query %regkey% /v %Value%) DO SET base=%%B
if %base%=0x0 && goto Disabled else goto Enabled
:Disabled
echo Hypervisor Enforced Code Integrity is Disabled
pause
exit
:Enabled
echo Hypervisor Enforced Code Integrity is Enabled
echo
echo Continuing will Disable Hypervisor Enforced Code Integrity and then your computer will reboot
echo
echo ****Save your work before continuting****
pause
reg add %regkey% /v %value% /t REG_DWORD /d 0x0
shutdown /r /f /t 005 /c "Restarting to Disable Hypervisor Enforced Code Integrity"
Your main issue, is a failure to read or understand the basic command usage information. If you open a Command Prompt window, type for /?, and press the ENTER key, you should see that you are using:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
Instead of:
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
However, there are several things you could do to improve your script, for this, and future tasks, so I have included a rewritten example for you too. It includes sections which are reusable for other registry checks, a user modifiable registry key and value pair to use with those, and parts specific only to the task laid out in the question.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Rem The next two lines are user editable.
Set "RegKey=HKLM\SYSTEM\CurrentControlSet\Control\DeviceGuard\Scenarios\HypervisorEnforcedCodeIntegrity"
Set "RegVal=Enabled"
Rem please do not modify the next twenty three lines.
Set "Elv=0"
For %%G In (RegKey RegVal) Do If Not Defined %%G (
%%G has not been defined.
Set "Elv="
)
If Not Defined Elv GoTo EndIt
Set "RegData="
For /F Delims^=^ EOL^= %%G In (
'%SystemRoot%\System32\reg.exe Query "%RegKey%" /V "%RegVal%" 2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"\<%RegVal%\>"'
) Do (
Set "RegData=%%G"
SetLocal EnableDelayedExpansion
For /F "Tokens=1,*" %%H In ("!RegData:*%RegVal%=!") Do (
EndLocal
Set "RegData=%%I"
)
)
If Not Defined RegData (
Echo The registry information does not exist.
GoTo EndIt
)
Rem The following five lines are specific to your task.
Set "Enabled=Disabled"
For %%G In (1 0x1 0x00000001) Do If "%RegData%" == "%%G" Set "Enabled=Enabled"
Echo Hypervisor Enforced Code Integrity is %Enabled%
If "%Enabled%" == "Disabled" GoTo EndIt
Rem Please do not modify the next seven lines.
Set "RegRoot=%RegKey:\="&:"%"
(Set RegRoot) 2>NUL | %SystemRoot%\System32\findstr.exe /XLI "HKLM HKEY_LOCAL_MACHINE" 1>NUL && Set "Elv=1"
%SystemRoot%\System32\reg.exe Query HKU\S-1-5-19 1>NUL 2>&1 || (
Echo To modify the registry, this script must be run elevated.
GoTo EndIt
)
Rem The following nine lines are specific to your task.
Echo(
Echo Your computer needs to reboot to disable Hypervisor Enforced Code Integrity.
Echo(
Echo ****Please save your work before continuting.****
Pause
%SystemRoot%\System32\reg.exe Add "%RegKey%" /V "%RegVal%" /T REG_DWORD /D 0 /F 1>NUL
%SystemRoot%\System32\shutdown.exe /R /T 5 /C "Restarting to Disable Hypervisor Enforced Code Integrity." /D P:2:4
Rem Please do not modify anything below.
:EndIt
Echo Press any key to exit . . .
Pause 1>NUL
Exit /B

How to find registry folder by match in it`s value

I am writing a batch file to search the registry.
I need to find the folder inside HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products which ProductName key equals to "MyProduct".
I need to find this folder and delete it.
This folder I want to delete will look like this: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\1A0614C849C672CF0A680DCFA3921735
In this example, change MyProduct to your actual ProductName string leaving the closing doublequote untouched, on line 4:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do Echo="%SystemRoot%\System32\reg.exe" Delete "%%G" /F ^>NUL
Pause
The above will only print the deletion line that you intend to run. Once you are satisfied it is correct, to actually delete it, change the code to the following, (remembering to change your ProductName string again). Please note, that as you're deleting a key from HKEY_LOCAL_MACHINE, you will most likely need to run this script elevated, or as a user having sufficient permissions for doing so.:
#Echo Off
SetLocal EnableExtensions
Set "App=MyProduct"
Set "Key=HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products"
For /F "Delims=" %%G In ('^""%SystemRoot%\System32\reg.exe" Query "%Key%" /S /F
"%App%" /D /E 2^>NUL ^| "%SystemRoot%\System32\findstr.exe" /I /R /X
"%Key:\=\\%\\[^^^\\]*"^"'
) Do "%SystemRoot%\System32\reg.exe" Delete "%%G" /F >NUL

Changing registry values through bat files

I'm trying to change some registry entries through a batch file.
All seems to work properly except these two:
reg add "HKEY_CURRENT_USER\Control Panel\International" /t REG_SZ /v sCountry /d "België" /f
reg add "HKEY_CURRENT_USER\Control Panel\International" /t REG_SZ /v sCurrency /d "€" /f
These give me some odd values instead of the ones I want entered I get the values in the image provided here:
Image
Any hints as to why/how to fix it?
Here's a quick example based upon my comment, (using codepage 1252):
#Echo Off
SetLocal EnableExtensions
Set "RegKey=HKCU\Control Panel\International"
Rem This line will undefine any variable with a name beginning _cp
For /F Delims^= %%G In ('Set _cp 2^>NUL')Do Set "%%G="
Rem This line will save your current codepage to the variable %_cp%
For /F Tokens^=* %%G In ('"%__AppDir__%chcp.com"')Do For %%H In (%%G)Do Set "_cp=%%~nH"
Rem This line will change the codepage to 1252 if it is not already 1252
If Not %_cp% Equ 1252 (Set "_cpc=TRUE"
"%__AppDir__%chcp.com" 1252 >NUL)
"%__AppDir__%reg.exe" Add "%RegKey%" /V "sCountry" /D "België" /F >NUL
"%__AppDir__%reg.exe" Add "%RegKey%" /V "sCurrency" /D "€" /F >NUL
Rem This line will change the codepage back if it was not already 1252
If Defined _cpc "%__AppDir__%chcp.com" %_cp% >NUL

Querying a registry key value via batch file

I'm trying to create a simple batch file that uses the reg query command to check for the existence of a value in a registry key, specifically
HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings ProxyEnable key.
If I run this command:
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /f 0x1 /d
It returns the following:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
ProxyEnable REG_DWORD 0x0
It's as if it's not accepting my /f search field, as I would expect the command as entered to return no results since my value for that key is 0x0.
I've tried using quotes around the "0x1", and other combinations, but wondering if I'm doing something wrong.
Thanks in advance!
As you only appear to be wanting to know if enabling proxy is toggled on, you can use this instead:
#Echo Off
Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"
For /F "Skip=1 Tokens=2*" %%A In ('Reg Query "%RR%\%RK%" /V "%RV%" 2^>Nul'
) Do If %%~B Gtr 0 Echo(%%B
Pause
If you wanted to for instance to toggle it on if it is off then change it to something like this:
#Echo Off
Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"
For /F "Skip=1 Tokens=2*" %%A In ('Reg Query "%RR%\%RK%" /V "%RV%" 2^>Nul'
) Do If %%~B Equ 0 Reg Add "%RR%\%RK%" /V "%RV%" /T REG_DWORD /D 0x1 /F>Nul
Although technically you could just run this to do that:
#Echo Off
Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"
Reg Add "%RR%\%RK%" /V "%RV%" /T REG_DWORD /D 0x1 /F>Nul
Edit
To check it first using the method suggested by Squashman and improved by zett42, because the following EnableHttp1_1 REG_DWORD 0x0 would have been sent to Find:
#Echo Off
Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"
Reg Query "%RR%\%RK%" /F 1 /E /T REG_DWORD|Find /I "%RV%">Nul 2>&1||(
Reg Add "%RR%\%RK%" /V "%RV%" /T REG_DWORD /D 0x1 /F>Nul)
Reg query is for read registry data not for set or change the registry data
to change the Registry data replace query with Add
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /d 0x1 /f

Batch reg query key to variable

When I run the command REG Query HKLM /k /F "Command Processor" /s /e /c on cmd, I get this result:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Command Processor
End results: 2 match(s) found(s).
But in batch:
#echo off & setlocal ENABLEEXTENSIONS
for /f "tokens=*" %%a in ('REG Query HKLM /k /F "Command Processor" /s /e /c') do set "MyPath=%%a"
echo The path string value is "%MyPath%"
pause
When I execute this, I only get the last line:
The path string value is "End results: 2 match(s) found(s)."
What is wrong? I would like to get the path keys on variables.
The problem is obvious: you are overwriting the value of MyPath in the for /F loop, then you are printing (echo) the final value/line.
To get all lines (any arbitrary number) you could do the following:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem storing the path strings in `MyPath1`, `MyPath2`, etc.:
set /A count=0
for /F "delims=" %%A in (
'REG Query HKLM /K /F "Command Processor" /S /E /C ^
^| findstr /L /B /V /C:"End of search: "'
) do (
set /A count+=1
set "MyPath!count!=%%A"
)
rem extracting the previously stored path strings:
echo Total number of path strings: %count%
for /L %%B in (1,1,%count%) do (
echo The !count!. path string value is "!MyPath%%B!"
)
pause
endlocal
This constitutes a sort of array MaPath1, MyPath2, and so on, containing all matching path strings.
The findstr command is used to filter out the summary line End of search: (this might be adapted according to your system locale/language).
Note that the array is no longer available after the endlocal command.

Resources