REG command after SET is "unknown" in DOS/Windows batch file (.BAT) - windows

In my batch file I started to use variables and suddenly the following commands do not work anymore.
Here is the part of my code with the problem
SET "path=MyPath"
REG ADD "HKCU\Software\ETC\ETC" /f /v "MyRegNameA" /t REG_SZ /d "%path%\ETC\"
REG ADD "HKCU\Software\ETC\ETC" /f /v "MyRegNameB" /t REG_SZ /d "%path%"
PAUSE
START "" "%path%\MyProgram.exe"
This code works without the SET... and of course with MyPath instead of %path%. Error Message is:
The command "REG" is either spelled wrong or couldn't be found
I previously found how to use Variables here: stackEx.SetVariables
To my knowledge I am doing it exactly as supposed, and I couldn't find specific help so far.

path is a logical name, but it's not a good name to use as it is assigned by Windows.
path is a semicolon-separated list of the directories that Windows uses to find programs. When you change it, Windows can no longer find reg.exe since reg.exe is not in mypath.
Simply choose another name - don't use path. If you enter set at the prompt, you will see a list of many of the variables that are established by Windows. Simple rule - don't use any of them for user-variables.

Related

Start a command and if it doesn't work start another

I want to start acrobat from my software.
At this moment I'm using the following command:
cmd /c start AcroRd32.exe /t filename
But now acrobat sometimes updates to 64 bit version and then AcroRd32.exe doesn't exists anymore.
So we have to start acrobat.exe instead:
cmd /c start acrobat.exe /t filename
But we are working with a lot of clients with different computers and maybe different versions of acrobat. That is also why we don't specify an install path.
So is there a way to say in one command line entry (not a script) to start AcroRd32.exe and if this doesn't work start immediately acrobat.exe instead?
So something like:
cmd /c start AcroRd32.exe /t filename | if not 1 is ok then cmd /c start acrobat.exe /t filename
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%b IN ('where autoruns64.exe autorunsc64.exe 2^>nul') DO CMD /c START "" "%%b" &GOTO done
ECHO NOT found!
:done
GOTO :eof
Note: START command : extra pair of quotes. This sets the title of the STARTed session, otherwise the first "quoted string" is used.
"%%b" is used in case the path to the executable (I used autoruns) contains a space.
The first argument to where is the preferred target.
where examines the path for filenames matching its arguments and lists them. The for /f reads the list and on the first match found, executes the START. If no match is found,an error message is produced by WHERE, which is suppressed by the 2^>nul.
Edit: one-line version
FOR /f "delims=" %b IN ('where autoruns64.exe autorunsc64.exe 2^>nul') DO CMD /c START "" "%b" &exit
Posted as a batch file because it's easier to re-run during testing.
WHERE locates files on the PATH and lists them
START locates files on the PATH and executes the first one found.
The PATH is the list of directories that will be examined by START, in the order in which they will be searched for the required executable, so WHERE examines that list of directories for the target executable, it does not search all of the drives in the hope of finding it.
Traditionally even on 64bit it was still Acrord32 for the free reader to avoid such dual name problems / complication and the 32bit or 64bit full product was start Acrobat.exe
However Adobe did not help by using version specifics within the registry so a query there would generally produce differing results ! Microsoft start adding enhancements as to where or how simple launchers may be deployed so the location now gets cloudier (pun :-)
However the traditional ask the registry works best except for my portable
version (where /r h:\ acrord32.exe is very slow on a TB drive and it was not on that one anyway, it was on a network drive!).
Simplest way I can think of via cmd to find an installed copy is:-
cmd /v:on /c "where acro*.exe>temp.txt&&set /p acroexe=<temp.txt&&if exist !acroexe! (!acroexe! /t %filename%)"
there is little use of errorlevel since the fail will simply report
> INFO: Could not find files for the given pattern(s).
Priority will be the first match within %path%
You also need to consider where temp.txt will be written perhaps "%tmp%\delme.txt"
Potential "Gotcha" is if first match finds something like AcrobatUpdater.exe
To avoid random acro's best use
cmd /v:on /c "where /f acrord32.exe acrobat.exe>temp.txt&&set /p acroexe=<temp.txt&&if exist !acroexe! (!acroexe! /t %filename%)"
actually since all you desire is start one or the other then
start "Reader" acrord32.exe /t "filename.pdf" "printer name"||start "Editor" acrobat.exe /t "filename.pdf" "printer name"
The downside to this last method is Windows will raise a GUI error if acrord32 cannot "start" Thus gui warning needs to be dismissed before the second option can be run and start. In that case START cannot be told to be silent on error.
Finally make your choice from above or use a hybrid approach
where /f acrord32.exe... || start acrobat.exe.. will not warn user on first fail but only on second if both missing.

Append to System PATH variable value using a batch file

I want to create a batch file to add the current directory to my System variable PATH, (not my User variable).
When I use:
SETX /M PATH "%CD%;%PATH%"
it does the needed thing. However, I get an error message:
data being saved is truncated to 1024 characters.
When I check the System variable using the GUI, I saw that User Path is getting added to the System Path. As a result, the System Path has duplicated entries.
I tried assigning the %PATH% variable to a temporary variable and echoing but I saw the duplications there as well.
I saw in some stack answer that the %PATH% variable we use in the batch file is actually a concatenation of both User Path and System Path.
At the Command Prompt, I tried:
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH"
but I don't have idea much about whether we can use it to take that PATH value and assign to another variable.
So I need to find a way to assign ONLY the SYSTEM PATH to a temporary variable, let's say SYS_PATHS. How can I do that?
Is there a better way to overcome this scenario?
I found the answer to the question I asked in a link provided by #Mofi. This is how you can take the system path only, and append a directory to it.
set "env=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
for /f "tokens=2*" %%I in (
'reg query "%env%" /v Path ^| findstr /i "\<Path\>"'
) do setx /m PATH "%%J;%CD%"

REG add Doesn't Run

I've written a batch file that tests for a user's Microsoft Office version, copy an Excel Add-In to their device from a shared drive, and add a registry key to their device.
Each individual action in the batch works as designed. But when I combine them all together, the file doesn't like the REG add command and completely closes the command window (even if I put Pause after the REG add line).
To troubleshoot, I created a new batch file and added pieces of my code to it, one section at a time, and tested the file each time a new section was added. Every section ran fine until I got to this section:
CHDIR "C:\Windows\System32"
IF NOT %ALREADY_ENABLED%=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
)
To see if the problem was my IF statement, I commented out the REG add line and put ECHO Hello World inside the IF statement. The file ran just fine and gave the Hello World output as expected.
I know the REG add command works because I have a batch file that only includes that piece of code and it works just fine:
#Echo off
setlocal enableDelayedExpansion ENABLEEXTENSIONS
chdir "C:\Windows\System32"
SET PATH=\"C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam\"
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f
I think the problem has something to do with the PATH variable, but I'm dumbfounded as to why this works in one file but not the other. Is it possible that the value of PATH is somehow changing during runtime after it's been set?
I'm not sure how to trap the error to even see what error is being thrown here. Everything I've tried to handle the error with doesn't work and the command window closes. Any ideas on what I'm doing wrong here?
Maybe this code snippet could help:
SET "myPATH=C:\Program Files (x86)\Microsoft Office\Office15\Library\Cerner_AddIn.xlam"
IF NOT "%ALREADY_ENABLED%"=="TRUE" (
REM Add the new key value if it doesn't exist already.
REG add HKCU\Software\Microsoft\Office\15.0\Excel\Options /v OPEN /t REG_SZ /d "%myPATH%" /f
)
Note:
do not change system environment variable PATH; use another variable name (myPATH);
quoting in set "variablename=variable value";
quoting in if statement;
to trap any error in a batch file, use echo ON while debugging.
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax

Using Batch to change Computer Name and UserName based off of their current Computer Name?

I'm writing a batch that I'll remotely place in the all users startup folder using Desktop Central 9 /PsExec. We are going to start using a naming convention as part of an initiative to overhaul computer management. This batch needs to find what the current PC Name is and then change it based off of what I will have manually defined in the batch itself. It would also be nice to be able to then also change the users name based off of what computer name is initially found. All computers use Windows 7 and above. I wrote a quick batch of what I think it should look like, minus the user name modification, but there are nuances here and there I must be getting wrong. The "If" statement is my weakness with batch, but I know it will be extremely efficient in this case. Can anyone correct what I'm doing wrong here? Much appreciated, this will help us A LOT!
SET WMIC GET COMPUTERNAME %%CURRENT%
If %%CURRENT% = THOMAS-PC then REG ADD
HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v
ComputerName /t REG_SZ /d TGZ-DELL-001 /f
If %%CURRENT% = CHARLES-PC then REG ADD
HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v
ComputerName /t REG_SZ /d TGZ-DELL-002 /f
If %%CURRENT% = MATHEW-PC then REG ADD
HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v
ComputerName /t REG_SZ /d TGZ-DELL-003 /f
%%CURRENT% is incorrect. Use %CURRENT%
IF statements do not have a "then". Use parens if you have multiple lines in IF statement.
Use = to SET and == to compare
Use quotes as shown in my example so that the spaces, etc don't cause problems
No need for WMIC in this case.
How about something like this?
I added /I to make the compare case insensitive. Leave that out if you wish... but then your compare would miss Thomas-PC, etc.
**** CAUTION **** Untested! Of course you will test test on a test machine before messing up the registry on a working machine :)
#echo off
set "RegCmd=REG ADD HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName /v ComputerName /t REG_SZ /d"
echo(this computer is %ComputerName%
if /I "%ComputerName%"=="THOMAS-PC" %RegCmd% TGZ-DELL-001 /f
if /I "%ComputerName%"=="CHARLES-PC" %RegCmd% TGZ-DELL-002 /f
if /I "%ComputerName%"=="MATHEW-PC" %RegCmd% TGZ-DELL-003 /f

How to use REG_EXPAND_SZ from the commandline?

I was reading the Windows Commandline Documentation (Win+F1) about the commands that modify the Windows registry, particularly the the "reg add" command.
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "%systemroot%\system32"
Now, I don't know how this was designed to work.
When I invoke the command above, the variable %systemroot% gets expanded to C:\Windows. I've tried the following not to make the variable to expand, but there is no way I could force it not to:
escaping the `%%`'s with an `%, ^, \` - doesn't work even if I use double quotes around
using the single quotes '' around the whole /d string
use `setlocal setdelayedexpansion`? sth like:
# (setlocal enabledelayedexpansion) && (reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "!systemroot!\system32") && (setlocal disabledelayedexpansion)
The variable 'data' (/d) field is either like ^%systemroot^% or like !systemroot! or just expands to C:\windows .
I could probably use the .reg file to accomplish my task, but I simply don't want to do it.
I thought that maybe there is something wrong with the program that I use to display the variable contents (regedit / regedt32 / reg query (commandline)), but after checking this is probably not the case.
Any ideas? I'm mainly interested how the variable value should look like in the regedit window, should it be like :"%systemroot%\system32" or "C:\windows\system32" to be properly expanded by other programs.
Regards.
From a command line, this worked for me:
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot^%\system32
The syntax suggested by aphoria is correct, without quotes as you discovered. But you run into trouble when you need spaces in the value.
However, it is possible to mix-in quotes in the value. It looks weird, but it works:
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d ^%SystemRoot^%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ
You can use quotes where needed, just not around your variables. And not right after a backslash.
Also, beware that the syntax for use in a batch file is not the same as on the command line. If you put that line into a batch file, you need to replace the "^" with another "%":
REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d %%SystemRoot%%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ
(cmd.exe always reminds me of Henry Spencer's quote "Those who do not understand Unix are condemned to reinvent it, poorly")
I recognize that this is an old thread, but since there doesn't seem to be an answer and I too was driven crazy by this problem, I'll share what I've come up with. It's convoluted, it's ugly, and it looks like this:
SET VAR=% SET
SET VALUE=%VAR%SystemRoot%VAR%\system32...
REG ADD HKLM... /v Test /t
REG_EXPAND_SZ /d %VALUE%
I end up with Test containing the string %SystemRoot%\system32
Edit: Should have finished testing before posting. While this worked from the command line, it failed in my command scripts. What DID work was SET VALUE=%%SystemRoot%%\system32...
We have a use in an autounattend.xml. I was first following aphoria's answer, then I had to use mivk's enhanced answer. I had it working. Then I made a small change and it stopped working. I consider this insight worth reporting, to save others lengthy frustration.
Building on previous answers, here is how to make this work in autounattend.xml:
<CommandLine>cmd.exe /c reg.exe add HKLM\ourkey /v ourvalue /t REG_EXPAND_SZ /d "our data with spaces"^%USERNAME^%"and more spaces" /f</CommandLine>
What matters is the cmd.exe /c. If you don't use cmd.exe then reg.exe still runs, but it puts literally ^% into the registry, not the desired %.
Maybe the answer by ASTX813 would work, but this seems easier to read.
There's no magic here, you must escape every % in command-line with/become: %%.
In batch file, you again must escape them, become %%%%SystemRoot%%%%. Yes, four ampersands. It's ugly, but it's the way it is, and it's pretty consistent though.
note: Using ^ to escape it as literal character might help/cleanup in one step/degree only (as long as it's not interpreted). The advantage using ^ is that you can write in command-line or batch file with identical syntax: eg. "^%SytemRoot^%", since in both environments, % treated equally as literal.
The answer is very simple. Only quote the spaces and escape the %'s.
In command line, escape the %'s with the ^.
reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun /t REG_SZ /d title" "^%username^% /f
In batch file, escape the %'s with another %.
reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun /t REG_SZ /d title" "%%username%% /f
This took me a while to figure out. In my mind, I should quote the entire string, but could not get the variable to be passed without expanding it. I broke my way of thinking by quoting spaces instead.
Please try the following:
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot%^\system32
From a batch file you end up with problems with quotes, which you can then escape with a backslash, for example:
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%name%" /v "UninstallString" /t REG_EXPAND_SZ /d "\"%%comspec%%\" /d /c \"%dest%\uninstall.cmd\""
without quotes
reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "%%%^%systemroot%%%^%\system32"
with quotes
reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "\"^%%systemroot^%%\system32\""
I finally got sick of the banner/logo on CScript and decided to prefix VBS associations with //nologo in my 'new build' batch script. This required modifying a REG_EXPAND_SZ registry entry with an environment variable and this question and its various answers were the best inspiration I could find. Here's the final script.
SET "thecmd=\""%%SystemRoot%%\System32\CScript.exe\"" //nologo \""%%1\"" %%*"
ECHO.%thecmd%
REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell\Open2\Command" /ve /t REG_EXPAND_SZ /d "%thecmd%" /f
SET thecmd=
Storing the command in a variable and using \"" for " and %% for % are what allows it to work.
If you prefer a one-liner (again, from batch) you can use:
REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell\Open2\Command" /ve /t REG_EXPAND_SZ /d "\""%%SystemRoot%%\System32\CScript.exe\"" //nologo \""%%1\"" %%*" /f
If you want the same functionality, don't forget to set the verb for VBSFile to Open2 so you can just double-click VBS files to run them with CScript:
REG.exe ADD "HKEY_CLASSES_ROOT\VBSFile\Shell" /ve /t REG_SZ /d "Open2" /f

Resources