Adding a new registry file through batch commands - windows

So I've been trying to add a new registry key and it's been working fine, noting the fact that this addition is not going to harm my computer it's just for me to understand a few concepts. The problem that's been happening is as follows, when I run my batch file, it's displaying the following message in cmd:
off REG ADD HKLM\Software\Hello - 24919 /v Test /t REG_BINARY /d fe340ead
the "off" in bold is a little weird, I'm not sure what happened for it to appear and not add the key, like I said I'm using batch commands and basically writing to another batch file through my initial batch file and this is the code I used:
#echo off
set new=new2.bat
echo #echo off REG ADD HKLM\Software\Hello - %random% /v Test /t REG_BINARY /d fe340ead >>%new%
Help is appreciated please! and thank you!
PS: I've referred to the following question (adding a random key to the registry through a batch file) , and did exactly the same, it worked once but then I don't know what happened for the above to show and not add the key.

#echo off
set new=new2.bat
(
echo #echo off
echo ^>nul REG ADD "HKLM\Software\Hello - %random%" /v Test /t REG_BINARY /d fe340ead
) > %new%
You need to place the #echo off and the REG ADD in separate lines

Related

Well-tried and tested Windows batch file stops at for loop since today - what could be the cause? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 18 days ago.
Improve this question
I have written and used a windows batch file that has been working without a problem for quite a while.
This is what it does:
The program loops through the *.pdf files of the current folder, and then performs a few tasks like extracting the text layer of the pdf file, searching for a couple of search terms and in the end it renames the pdf file according to which search term was found.
The batch file worked fine until today.
Now it just stops working at the for loop and I have no idea why.
Stripped down to the essence, the batch looks like this:
setlocal enabledelayedexpansion
for /f "delims=" %%i in ('dir /b /a-d /o-d *.pdf') do (
echo %%i
echo %%~ti
echo %%~ni
)
The echos would just be examples, but as the batch files never reaches what's after the for loop, I simplified it.
Since today, the program would just stop when it reaches the for loop.
This is what I tried to solve the problem:
examined the syntax of the for loop, found no mistakes
tried simplified variations of the for /f loop including leaving out the delims option, changing it against a usebackq option, changing the name of the loop variable, stripping down the options of the dir command, changing the file filter to *.txt
turned the for loop into a one-liner with just an echo %%i command etc.
looped through the lines of a given text file
changed the location of the batch file to another folder
tried the batch on a Windows 10 and a Windows 7 system
try again after rebooting the system
checked folder access rights, I had all of them, no restrictions
None of these helped, the batch file would always stop and close its window right at the for loop.
I also inserted the dir command before the for loop just to check the syntax - this worked with no problem, so it can't be the syntax of the dir command either.
I remember that I had this problem a few times years ago and it either disappeared by itself or I had to retype the batch word for word in a new text file copying whatever was in the original file as copy and paste wouldn't work.
Has anyone out there observed this strange behaviour and maybe found the cause and at best a solution for it? I'd be so thrilled.
EDIT: When I run the batch in the cmd window, I get a simple SYNTAX ERROR when it reaches the line with the for loop.
Try this at the command prompt:
reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun
reg query "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun
Both results should be empty, but probably in your case they are set.
The problem of the AutoRun file, it's started when a new cmd instance is started, but also for the FOR/F command.
If this is the problem, you should delete these entries by
reg DELETE "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /f
reg DELETE "HKEY_LOCAL_MACHINE\\Software\Microsoft\Command Processor" /v AutoRun /f
Or if you really need an AutoRun batch, then it should start with a guard.
#echo off
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the originial!!!
set "_ccl_=!cmdcmdline!"
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** Do your stuff ***
)
endlocal
exit /b

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

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

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.

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 open file with default application in cmd?

I'm trying to open a file in it's default editor after the user has created the file. So far my script is:
#echo off
#echo --- Create A New File ---
#echo -
#echo Where should we put the new file?
set /p fileLocation=# %UserProfile%\
#echo -
#echo What do you want to call your new file?
set /p fileName=#
#echo -
#echo Almost Done! What is the files extension?
set /p extension=# .
#echo -
copy NUL "%UserProfile%\%fileLocation%\%fileName%.%extension%"
(ignore the extra echos and '#' those are just for fun)
After I click the file, it does the command: Choose Location > Choose File Name > Choose File extension. I'm almost done on what I want but theres one last thing. How can I get the file name that I created and then open in its default text-editor?
You can use start to open the file with the associated application.
Resources :
Open a File in the Default Application using the Windows Command Line (without JDIC) (waybackmachine capture from Oct 30, 2010)
In windows you can use start (http://ss64.com/nt/start.html).
start "" "%UserProfile%\%fileLocation%\%fileName%.%extension%"
You can also use explorer.exe/explorer to open the file (e.g. explorer file.txt). This also works nicely if you use WSL, especially with an alias like alias open="explorer.exe" so you can just call it like, e.g., open file.txt.
I achieved the correct way of FILE ASSOCIATION using these cmd commands.
this is just an example:
REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v # /t REG_SZ /d "\"C:\\Program Files\\Noteepad++\\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f
assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"
(it's better to put them in .bat file)

Resources