Removing a registry key and its subkeys using a batch - batch-file

I need to remove a registry key (including its subkeys) from a batch file.
The examples I found led me to the following code but the key remains?
#ECHO OFF
REG DELETE "HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.0\ACAD-8001:409\Profiles\STDPROFILE" /V

Not so good at batch programming, but
reg query "HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.0\ACAD-8001:409\Profiles\STDPROFILE" /s > toDelete.txt
for /f %v in (toDelete.txt) do reg delete %v
the first line puts every key and value in a file, then the loop reads them and calls reg delete

Related

NSIS with admin access fails to create reg key via batch script

I have a NSIS script with RequestExecutionLevel admin set and within this I invoke a .bat script which adds a reg key.
When the batch-file is executed through command-prompt the reg key gets added. But when running the installer, it executes the .bat file but fails to add the reg key.
nsExec::ExecToStack '"$pluginsdir${SETUP_PATH}\UpdateNtpConfiguration.bat" $Ip1'
UpdateNtpConfiguration.bat content
set adds=%1
REM Get the list of ntp servers showing up in System Date & Time->Internet Time dropdown
set "num=0"
for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /A num=num + 1
REM Add address at the end
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "%adds%" /f
goto:eof
Just like #Anders said, there are built in functions for that.
https://nsis.sourceforge.io/Docs/Chapter4.html#registry
If an an error happens with this command, it will be a lot easier to debug as it is built-in.
If you're not doing anything else in the batch file, it is better to find out if NSIS has a command already integrated with it....
EDIT:
It also might be that the admin installer is run the bat file without admin privs.
After some debugging found out that NSIS was writing the registry values in 32 bit reg space(HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft). I had to append /reg:64 to make it add to the 64 bit reg space.
Also the reason I had the bat script was to have some logic to look for duplicates before adding the key.

Preview command output to text file before running command (Batch)

I have a batch-file which works as intended, deleting files with a specified string or extension, but I want to keep a record of what was deleted.
I want to take the output of the command and write it to a .txt file on my desktop before running the actual command:
if /I %CONFIRM%==y (
(del /F /S %FOLDER%\*%DELETE%)>"C:\Users\%username%\Desktop\deleted.txt"
del /F /S %FOLDER%\*%DELETE%
)
In this case %DELETE% is the user input variable value bak.
If I try to delete .bak files it still deletes the files but gives me the following result and wont write to text file:
Could Not Find C:\Users\USERXYZ\Desktop\*bak
Any ideas?
Perhaps
set /p "confirm=delete %FOLDER%\*%DELETE% ? "
if /I %CONFIRM%==y (
(ECHO del /F /S %FOLDER%\*%DELETE%)>"C:\Users\%username%\Desktop\deleted.txt"
(dir/S/B %FOLDER%\*%DELETE%)>>"C:\Users\%username%\Desktop\deleted.txt"
del /F /S %FOLDER%\*%DELETE%
)
may assist.
In your code, the first del command deletes the file AND should create the deleted.txt file containing the names of the deleted files. Since those files have now been deleted, then the second del command quite correctly reports that it could not delete the files since they are no longer there.
Note that > will create a NEW file, whereas >> will append to an existing file (or create a new file if none already exists)
The echo del in this code shows the command that is about to be executed - you may or may not want this - idk. However the > puts this into a NEW file.

Creating Registry Using Batch Files

I am working on creating a batch file that adds will create registry entries in order to add custom commands into the UI of our file management software.
The issue is that the location of the key varies based on the version of the software a user is on.
Is it possible to create a patch file that will search within registry keys/sub keys and create new values there?
Here is an example of the registry location, the portion surrounded by ** will change based on version:
[HKEY_CURRENT_USER\Software\Motive\M-Files\**12.0.6550.8**\Client\MFShell\Rombald\TasksBar\ShellCommands\1]
"Name"="Calendar"
"Executable"="\\\\mfiles-server\\M-Files setup\\M-Files
Calendar\\MFCalendar_v1.2.6\\MFCalendar_v1.2.6\\M-Files Calendar.exe"
"Icon"="\\\\mfiles-server\\M-Files setup\\M-Files
Calendar\\MFCalendar_v1.2.6\\MFCalendar_v1.2.6\\M-Files Calendar.exe,0"
"Arguments"="{B1438CAB-2E53-474E-AA82-3D48F787F6B7}"
Essentially I would have to look for the version of the software and insert that into the key. If more than one exists it would be the largest value.
Thanks in advance for the help!
#echo off
setlocal
for /f "delims=" %%A in (
'reg query HKCU\SOFTWARE\Motive\M-Files'
) do set "root=%%~A" & set "version=%%~nxA"
echo root: %root%
echo version: %version%
if defined root (
echo Write new reg information.
reg add "%root%\testkey" /v testvalue /d data /f
reg add "%root%\testkey" /v testversion /d "%version%" /f
)
The for loop uses the command reg query to search through
the entries in the registry key. The last entry is saved as
root and version. The variable root will have the full key path
while the variable version will be the last segment of the path
which will be just the key name. These are echoed to console.
If defined root, subkey testkey will be added with values and
data as the code shows.
So long as you only have version keys under the key of M-Files
then this may be all you may need. If not, then you may need more
code to skip the other keys.

How to use FINDSTR inside of a FOR loop to delete registry keys?

I want to delete all *.bak profile registry keys off all computers at work.
We gave everyone temporary profiles a while back, and we're trying to remove them now. The .bak registry keys are forcing temp profiles where people don't want them.
I have this command that LISTS all the *.bak registry keys:
for /f %f in (windowscomputers.txt) do reg query "\\%f.mydomain.com\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | findstr /E bak
But that just lists them. I need to delete them.
And I TRIED to delete the *.bak keys off of just one workstation as a test:
for /f %f in ('reg query "\\workstationFQDN\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" ^| findstr /e bak') do reg delete \\workstationFQDN\%f
The above tries to delete \\workstationFQDN\HKLM\SOFTWARE\Microsoft\Windows. Even though I have "" around the path, it's breaking at the space between Windows and NT.
How do I use findstr inside of a for loop, so that it executes on each of the registry keys that findstr finds?
After that, I need to do a nested for loop so that it deletes each of the .bak registry keys that findstr finds, on each of the computers in my text file windowscomputers.txt.
This is a basic question. I tried googling findstr reg query for loop, and found lots of batch script examples that frankly went over my head, using options like tokens=, delims=, and %~ that I've read about in the for /? help file, but don't understand their usage or how it might apply to my specific need (delete each .bak profile registry key on each computer at work).
I've also only used one-liners in cmd prompt. I understand that batch has some syntax differences if I need to use multiple commands, like %%f instead of %f.
Try
for /f "delims=" %f ...
(And echo the reg delete... just to be safe)
(also - quote the string being deleted)
%f is referred to as a metavariable. Metavariables require one % directly from the prompt, buttwo if used within a batch file.
From the prompt, use for /? |more for (somewhat cryptic) documentation about the delims= (and other) options. Or look through some examples here on SO.
(as a batch line) the following listed the appropriate keys for me (I can't duplicate your setup)
FOR /f "tokens=1*delims=\" %%q IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"') DO ECHO %%r
Prefixing "HKLM" above with "\machinename\" and filtering using a findstr filtered out the unwanted lines. %%r contained \software\..., so all you'd appear to need is to pop the prefix string \\machinename\HKLM\ before that and your command should be ready-to-go.

Reg command in batch file

I am trying to run a simple batch file with following line of codes in it but as soon as I trigger this batch file (either running it from command prompt or double clicking on it), it continuously runs. Like forever.
I am just trying to modify existing key in registry using batch file to some specific value.
reg add "hkcu\control panel\desktop" /v ForegroundLockTimeout /d "0x30d40" /f /t REG_DWORD
exit
In order to stop execution when I am giving interrupt (Cntrl + C) it asks whether it should terminate execution of this batch file or not..
Can someone help to understand what is wrong with this code?
I think it should stop execution once it updates registry key but that is not happening. It continuously prints line# 1 on screen.
You've called the batch file reg.bat right?
Try regfile.bat
Try the following, this works for me:
reg add "HKCU\control panel\desktop" /v ForegroundLockTimeout /t REG_DWORD /d "0x30d40" /f
It returns:
The Operation completed successfully.
Obviously try saving that to your bat file and then run the batfile as usual.

Resources