Registry Edit From Batch - windows

I would like to change the registry path's value from 0 to 1:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background\OEMBackground
Is there any possible way of doing this within a batch file using the "reg" command? You can grab the syntax here. I searched it, but can not find a way to modify it. Any help would be greatly appreciated. Thanks!

I would stay away from reg.exe, it can be blocked by a policy. I suggest to use wmic.exe.
In your case it should works like this:
setlocal
:: $KEY broken for better readability
set "$KEY=SOFTWARE\Microsoft\Windows\CurrentVersion"
set "$KEY=%$KEY%\Authentication\LogonUI\Background"
set "$VALNAME=OEMBackground"
set "VAL=1"
set "HIVE=&H80000001" &:: "&H80000001 = HKEY_LOCAL_MACHINE"
wmic.exe /NAMESPACE:\\root\default Class StdRegProv Call^
SetDWORDValue^
hDefKey="%HIVE%"^
sSubKeyName="%$KEY%"^
sValueName="%$VALNAME%"^
uValue="%VAL%"^
&& echo Success.||echo failed.
endlocal
To select another registry-hive, the value of hDefKey must be changed accordingt to this MSDN-Article.
Other Methods for different value types which can be used instead of SetDWORDValue can be found there as well.

Related

How to set value to multiple variables in one line in windows batch

I tried to do something like this (see build:dev):
also tried to do this:
set var1=a && set var2=b
still don't work.
Tell please how can I do it?
I would set each variable on a separate line, all following the same format.
#echo off
set names=Hirsty
set source=StackOverflow
echo Hello %names% - possible answer from %source%
This would give an output of:
Hope this helps.

Windows batch for loop sub routines and local variables

At work we use Windows batch files to run jobs off a scheduling tool. It's an old tool and approach but it works. I have to make new jobs fit the existing technology.
For this particular job I need pick up a number of files from a folder and load them into a SQL server database table. For each one I load I then need to run a bit of SQL to update the recently populated table.
So I have a for loop that looks for files in the folder. If one exists I use SQL server's BCP to load the data into a table. I then use SQL server's SQLCMD to perform the update.
The policy is when using BCP or SQLCMD to write any issues out to files. Post call we then check these files to ensure that nothing went wrong.
When dealing with a single file this is fairly straightforward stuff. Using a FOR loop is proving to be impossible.
I envisaged calling a sub routine which would set a variable that code in the for loop could check and if appropriate exit gracefully.
The first thing we do in the batch file is set a global variable to 0. When the batch job returns control to the 'scheduler' if this is not equal to 0 the job shows as failed.
So paraphrasing the code it looks something like the following.
SET g_status=0
SET LOCAL EnableDelayedExpansion
FOR n = 1 to 12
IF FILE(n) EXISTS (
Execute BCP....
Execute SQLCMD....
CALL check_sql_status
IF !l_error_status! NEQ 0 ENDLOCAL & SET g_status=!l_error_status!
IF g_status NEQ 0 GOTO exit_gracefully
)
NEXT
.
.
GOTO END
REM ========
:check_sql_status
REM======
IF something is wrong (
SET l_error_status=123
GOTO EOF
REM ========
:exit_gracefully
REM======
ECHO g_status
:End
.
.
Don't get hung up on the 'code' above. It's only the bit where I am attempting pass the local variable back to the global variable that I am interested in. No matter what I try I cannot get the global variable to accept the value set in the sub routine.
I will try to find this question and post some of the actual code when I get back to work although BCP and SQLCMD use lots of parameters that may just confuse rather than help.

How to change the output file name in a bat file?

I'm trying to change the output file name, fOut, in a bat file, but have no luck so far.
I'm developing on Windows 7 and will deploy the code to Windows 2003 server.
The code looks like this:
set fName=%1
set fExt=%fName:~-5,-1%
set fOut=%fName:~0,-5%_PAD%fName:~-5%
Examples of fOut:
abcdc2evv_PAD.dat
abcdefgh33ij_3737_PAD.dat
How can I change fOut to get the following file names?
A. Adding FMT_ at the beginning of the file name:
FMT_abcdc2evv_PAD.dat
FMT_abcdefgh33ij_3737_PAD.dat
B. Adding FMT_ at the beginning of the file name and remove _PAD before .dat:
FMT_abcdc2evv.dat
FMT_abcdefgh33ij_3737.dat
Addendum:
Just one argument is passed to the bat file: path + file name.
x.bat "C\test\xxx.dat"
In the bat file:
#echo ^-input file name = ^%1
set fName=%1
set fExt=%fName:~-5,-1%
set fOut==%fName:~0,-5%_PAD%fName:~-5%
I don't know if I'm missing something obvious - it's not clear what the input to this script is.
However adding FMT_ before should just be a case of changing:
set fOut=%fName:~0,-5%_PAD%fName:~-5%
to:
set fOut=FMT_%fName:~0,-5%_PAD%fName:~-5%
or if you want to put the FMT_ version into another variable, then:
set bob=FMT_%fOut%
As for removing _PAD, can you not just repeat the SET fOut line without the _PAD? This would seem to be the simplest way to do it. In fact, removing _PAD and prefixing FMT_ would seem to simply be this:
set bob=FMT_%1
if you want to remove pad just take it out of your assignment statement
you have:
set fOut=%fName:~0,-5%_PAD%fName:~-5%
you want:
set fOut=%fName:~0,-5%fName:~-5%
to add FMT_ just add it at the beginning of the file name:
set fOut=%FMT_%fName:~0,-5%_PAD%fName:~-5%
If you want to separate the filename from the extension, don't mess around counting chars; there is a built-in method (described in for /?):
echo Filename=%~n1
echo Extension=%~x1
echo resulting file="FMT_%~1"
REM without _PAD, following with _PAD
set filename="FMT_%~n1_PAD%~x1"
If there is really need to remove _PAD (as Chris already noted, you are explicitely adding it with your code), just replace _PAD. with . only:
set filename=%filename:_PAD.=.%

Windows batch file If/Else Start syntax?

I need to make a Windows batch file that will:
1.- Check a directory name and "If" it exists,
a.- Run a specified *.exe from a different directory.
2.- Or "Else"
a.- Rename a directory and also
b.- Rename another directory - then
c.- Run a *.exe from the created directory.
Question is: I'm stuck on the correct syntax ( I suppose) on creating this batch file. This is what I have, (maybe a nested if/Else would be better?) someone please enlighten me... Thanks.
#echo off
IF EXIST "C:\Test\Dir1" (
START "C:\Test\Dir\Test.exe"
) ELSE ( ren "C:\Test\Dir" "C:\Test\Dir1"
ren "C:\Test\Dir2" "C:\Test\Dir"
START "C:\Test\Dir\Test.exe"
)
You can't ren with a target name including a path or drive.
try
ren "C:\Test\Dir2" "Dir"
start has odd syntax - 'though it seems to not be the problem in this case. The first "quoted argument" string becomes the window title and can be routinely disregarded by the executable. Try
routinely using
start "window title that can be empty if you like" "executablename" argument list
Even if this arguably doesn't comply exactly with the documented behaviour.
(and simplifying problems to a generality can obscure the real cause, too)
Thanks dbenham and Magoo. Your responses made a world of difference on something that seems quite simple. It is now working as all the changes you both mentioned help. The final "nail in the coffin" was understanding the syntax for the START parameters. Understanding that the first parameter has double quotes and it uses that as the optional TITLE for the new window, ssoooo giving it an empty double quote ( "" ) gives it an empty title before the name of the program to fake it out. And Voila...
Thank you guys sssooo much. Final Working batch file.
#echo off
IF EXIST "C:\Test\Dir1" (
START "" "C:\Test\Dir\Test.exe"
) ELSE ( ren "C:\Test\Dir" "Dir1"
ren "C:\Test\Dir2" "Dir"
START "" "C:\Test\Dir\Test.exe"
)

Using a passed in parameter value to set PATH variables in a batch file

Right now I have a batch file that sets the PATH variable to all of the required directories.(there is actually a bunch more required directories, i just took them out so the code snippet would not be too long)
#echo off
set PATH=D:/src/trunk/build/bin;D:/src/trunk/build/bin/CoreTools;D:/src/trunk/build/bin/Plugins/Extensions;D:/src/trunk/build/bin/Plugins/CustomUI
set DEBRIEF_INSTALL_DIR=D:/src/trunk/DebriefSuite/D3D_Installation
set READERS=D:/src/trunk/build/bin/CoreTools/Readers
set BINARY_DIR=D:/src/trunk/build
cd D:/src/trunk/build/bin
start PROGRAM.exe --ConfigFile="D:/src/trunk/DebriefSuite/Installation/config/Projects/config.xml" ^
--Mode-File="D:/src/trunk/DebriefSuite/Installation/config/Projects/Common/anotherconfig.xml" ^
--Env:Bin="D:/src/trunk/build/bin"
cd D:/src/trunk
It works fine, but all of the directories are hard-coded. This needs to be able to work for other computers that might have their root directory in a different location. I need to be able to pass in a root directory (something like "D:\different_root_location") and substitute it in to each place in this code that currently says "D:\src\trunk". The problem is, i am not sure what the syntax would be for something like this. I am new to writing batch files. I tried doing something like
SET ROOTDIR=%1 .....
And then
set PATH=%ROOTDIR%/build/bin;%ROOTDIR%/build/bin/CoreTools;%ROOTDIR%/build/bin/Plugins/Extensions;%ROOTDIR%/build/bin/Plugins/CustomUI ..........
start PROGRAM.exe --ConfigFile="%ROOTDIR%/DebriefSuite/Installation/config/Projects/config.xml" ^
but it did not work. I'm not really sure how to make this work! Also, any links to good sources of information about writing batch files in general would be extremely helpful since i am starting out!
Change your line so it includes the original path as well.
From this:
set PATH=D:/src/trunk/build/bin...
to this: (and Windows uses \ and not / even though it works in some cases)
set PATH=%path%;D:\src\trunk\build\bin....

Resources