Changing registry values through bat files - windows

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

Related

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

Cmd character font size

Did anybody know how to modify the character size in cmd. I already searched on internet but all what I found was the method from Proprieties. I need something like mode x,y but for characters.
You can not change (or at least i don't know how to do it) the properties of the current console from command line without some third party tool.
BUT, you can customize the creation of a new console
#echo off
setlocal enableextensions disabledelayedexpansion
set "consoleName=testing"
:: http://technet.microsoft.com/en-us/library/cc978570.aspx
( reg add "HKCU\Console\%consoleName%" /f
reg add "HKCU\Console\%consoleName%" /f /v "FaceName" /t "REG_SZ" /d "Consolas"
reg add "HKCU\Console\%consoleName%" /f /v "FontFamily" /t "REG_DWORD" /d 0x00000036
reg add "HKCU\Console\%consoleName%" /f /v "FontSize" /t "REG_DWORD" /d 0x00080004
reg add "HKCU\Console\%consoleName%" /f /v "FontWeight" /t "REG_DWORD" /d 0x00000000
reg add "HKCU\Console\%consoleName%" /f /v "QuickEdit" /t "REG_DWORD" /d 0x00000000
reg add "HKCU\Console\%consoleName%" /f /v "ScreenBufferSize" /t "REG_DWORD" /d 0x00200040
reg add "HKCU\Console\%consoleName%" /f /v "WindowSize" /t "REG_DWORD" /d 0x00200040
) > nul
start "%consoleName%" cmd.exe
The registry stores the configuration for multiple customizations of the console. This code just creates a basic customization associated to a window title and starts a new console with this title to use the indicated parameters.
For more information, the documentation includes a complete reference of the values.

Batch script if statement returns error 'unexpected at this time'

I have a batch script that writes registry keys for Win7/WinXP so an older video game can run properly, but the batch itself isn't working. The script is as follows:
echo on
setlocal ENABLEEXTENSIONS
set KEY_NAME=HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 311080
set VALUE_NAME=InstallLocation
for /F "usebackq tokens=3*" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do (
set OUTPUT_SEVEN=%%A%%B
)
setlocal ENABLEEXTENSIONS
set KEY_NAME=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 311080
set VALUE_NAME=InstallLocation
for /F "usebackq tokens=3*" %%A IN (`reg query "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul ^| find "%VALUE_NAME%"`) do (
set OUTPUT_XP=%%A%%B
)
reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 311080"
if %ERRORLEVEL% EQU 0 goto Win7
reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 311080"
if %ERRORLEVEL% EQU 1 goto WinXP
:Win7
(
echo Running Windows 7 Script
REG ADD "HKLM\SOFTWARE\Wow6432Node\Madia\Echelon" /V Path1 /T reg_sz /D "%OUTPUT_SEVEN%\\Data\\" /F
REG ADD "HKLM\SOFTWARE\Wow6432Node\Madia\Echelon" /V Path2 /T reg_sz /D "%OUTPUT_SEVEN%\\Data\\" /F
REG ADD "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Echelon" /V InstallLocation /T reg_sz /D "%OUTPUT_SEVEN%\\" /F
REG ADD "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Echelon" /V InstallSource /T reg_sz /D "%OUTPUT_SEVEN%\\" /F
set QQ=%OUTPUT_SEVEN%
goto exit
)
:WinXP
(
echo Running Windows XP Script
REG ADD "HKLM\SOFTWARE\Madia\Echelon" /V Path1 /T reg_sz /D %OUTPUT_XP%\\Data\\" /F
REG ADD "HKLM\SOFTWARE\Madia\Echelon" /V Path2 /T reg_sz /D %OUTPUT_XP%\\Data\\" /F
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Echelon" /V InstallLocation /T reg_sz /D %OUTPUT_XP%\\" /F
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Echelon" /V InstallSource /T reg_sz /D %OUTPUT_XP%\\" /F
set QQ=%OUTPUT_XP%
goto exit
)
:exit
(
start /d"%QQ%" /wait Game.exe
)
Running the script in the command prompt:
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>gamelauncher.bat
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>echo on
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>setlocal ENABLEEXTENSIONS
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>set KEY_NAME=HKLM\SOFTWARE
\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 311080
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>set VALUE_NAME=InstallLoca
tion
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>for /F "usebackq tokens=3*
" %A IN (`reg query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\
Uninstall\Steam App 311080" /v "InstallLocation" 2>nul | find "InstallLocation"`
) do (set OUTPUT_SEVEN=%A%B )
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>(set OUTPUT_SEVEN=C:\Progr
amFiles (x86)\Steam\steamapps\common\Echelon )
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>setlocal ENABLEEXTENSIONS
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>set KEY_NAME=HKLM\SOFTWARE
\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 311080
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>set VALUE_NAME=InstallLoca
tion
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>for /F "usebackq tokens=3*
" %A IN (`reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\St
eam App 311080" /v "InstallLocation" 2>nul | find "InstallLocation"`) do (set OU
TPUT_XP=%A%B )
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>reg query "HKLM\SOFTWARE\W
ow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 311080"
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninsta
ll\Steam App 311080
DisplayIcon REG_SZ C:\Program Files (x86)\Steam\steam\games\1b116bbca7
813266288d61df77b3baca0297aaab.ico
DisplayName REG_SZ Echelon
HelpLink REG_SZ http://support.steampowered.com/
InstallLocation REG_SZ C:\Program Files (x86)\Steam\steamapps\common\E
chelon
Publisher REG_SZ MADia Entertainment
UninstallString REG_SZ "C:\Program Files (x86)\Steam\steam.exe" steam:
//uninstall/311080
URLInfoAbout REG_SZ http://www.madia.ru/echelon.html
NoRepair REG_DWORD 0x1
NoModify REG_DWORD 0x1
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>if 0 EQU 0 goto Win7
\Steam\steamapps\common\Echelon was unexpected at this time.
c:\Program Files (x86)\Steam\SteamApps\common\Echelon>set QQ=C:\ProgramFiles (x8
6)\Steam\steamapps\common\Echelon
If someone smarter than me could figure out what happened here, I'd be very grateful.
The problem are the parenthesis in the value you are assigning to the variable when
set QQ=C:\ProgramFiles (x86)\Steam\steamapps\common\Echelon
is executed. Change
set QQ=%OUTPUT_SEVEN%
into
set "QQ=%OUTPUT_SEVEN%"
and do the same for the rest of set commands. This prevents problems with special characters and avoid the inclusion of unneeded spaces at the end of the stored value.

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

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

Windows batch command to make changes effective immediately

I have a batch script that lets users change their background from black to white or vice versa. The problem I'm having is that the script only makes immediate change sometimes, and other times the user has to log off and log back on for the background to change. Here is what I have so far:
#echo off
call :quiet
exit /b
:quiet
:: For comparison, using the black wallpaper registry value
set "black=C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_black.bmp"
:: Set reg query result to current
FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKCU\Control Panel\Desktop" /v Wallpaper') DO SET current=%%B
:: For debugging purpose.
ECHO current=%current%
pause
if "%current%"=="%black%" (
call :MakeDayWallpaper>nul 2>&1
:: Make changes without requiring logoff
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
) else (
call :MakeNightWallpaper>nul 2>&1
:: Make changes without requiring logoff
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
)
EXIT /b
:MakeDayWallpaper
REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "" /f
REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_white.bmp" /f
REG DELETE "hkcu\Software\Microsoft\Internet Explorer\Desktop\General" /v Wallpaper /f
REG ADD "hkcu\control panel\desktop" /v WallpaperStyle /t REG_SZ /d 2 /f
EXIT /b
:MakeNightWallpaper
REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "" /f
REG ADD "hkcu\control panel\desktop" /v wallpaper /t REG_SZ /d "C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Themes\MDCBackground_black.bmp" /f
REG DELETE "hkcu\Software\Microsoft\Internet Explorer\Desktop\General" /v Wallpaper /f
REG ADD "hkcu\control panel\desktop" /v WallpaperStyle /t REG_SZ /d 2 /f
EXIT /b
The line RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters is the command that allows the immediate change. When I look at shell I can see that the registry value is changing every time the script is executed, but despite this fact, sometimes the background does not change until the user logs off and logs on.
It might be that the registry changes aren't taking effect until the log off/log on is done (I'm not sure why it would sometimes work immediately though). Try restarting explorer afterwards and see if that helps.
taskkill /im explorer.exe /f
explorer.exe

Resources