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
Related
I would like to display the information regarding the ProfileImagePath value of one windows user who don't have the "_" character in his username :
#echo off
cls
for /f %%I in ('dir /a:d-h /b C:\Users\ ^| %SystemRoot%\System32\findstr.exe /b /l /v "_"') do (
FOR /F "delims=" %%k IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"^|findstr.exe /R "S-1-5-21-[0-9]*-[0-9]*-[0-9]*-[0-9]*$" 2^>nul') do (
reg query "%%k" /v "ProfileImagePath"|findstr /i /e /c:"%%~I"
)
)
But the findstr command also takes into account users with the "_" character, while in the first FOR command, I have excluded this character :
ProfileImagePath REG_EXPAND_SZ C:\Users\user1
ProfileImagePath REG_EXPAND_SZ C:\Users\_user1
How is it possible ? How to take this into account in the 2nd FOR command ?
findstr /i /e /c:"user1" searches for strings ending with user1. However _user1 also ends with user1 :(
Changing it to findstr /i /e /c:"\%%~I" is one way to solve your problem :)
Screenshot
I am trying to fetch all registry entries under
HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList and Delete if REG_EXPAND_SZ key contains value C:\Users\sas-
Ideal Steps Script should do :
1) Get all Registry entries for users Using :
reg query "HKEY_Local_Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
>> xyz.txt
2)If users name is sas-### (which we can get in "REG_EXPAND_SZ") then delete this entry :
for /f "skip=10 tokens=* " %%a in (xyz.txt) do (
for /f "eol=; skip=2 tokens=4 delims=\t" %k in ('reg query "%%a" /t REG_EXPAND_SZ') do (
if %k == "sas-" (reg delete "%%a")
)
)
Try this:
for /f "tokens=*" %%a in ('
reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"') do (
for /f "tokens=*" %%b in ('
reg query "%%a" /v "ProfileImagePath" 2^>nul ^| find "sas-"') do
rem reg delete "%%a"
echo "%%a" >>C:\names.txt
)
I've based this on you querying the ProfileImagePath value, and deleting any of the keys that contain sas-* in the value. I've also put in a few line breaks you will have to remove (just for easy viewing on SO).
Please test this first (I've echo'd them to a text file at the moment), if you are happy with it then remove rem from the reg delete line.
You should be able to do it from a single For loop; however, I do not particularly like the idea of removing profiles using this method.
#Echo Off
For /F "Tokens=1-2*" %%A In ('Reg Query^
"HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /S^
/F ProfileImagePath /V 2^>Nul') Do (
If Not "%%~A%%B"=="ProfileImagePathREG_EXPAND_SZ" (
Set "_C=Reg Delete "%%A%%B"") Else (Set "_P=%%~C"
SetLocal EnableDelayedExpansion
If Not "!_P:C:\Users\sas-=!"=="!_P!" Echo=!_C!
EndLocal))
Timeout -1
If you're happy with the console output then remove Echo= from line 8 and delete line 10.
I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)
I want to find and delete every desktop.ini and Recycle Bin.BIN file on a network drive, H:, using a windows batch file. I currently have this:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
Which works but for any file in a sub-folder with a space in the name it fails with a "cannot find file" error. Any suggestions how to fix that?
solution that worked for me was
create bat file "delete_all_desktop_ini.bat"
with
del /s /q /f /a ".\desktop.ini"
put it in a folder and run it
it will delete all desktop inis in current directory and sub directories of this file.
i put it in a project folder that is in google drive
Give this a test:
I've altered the recycle bin name to what I see here in Windows 8.
The name changes with different versions of Windows.
#echo off
del /s /q /f /a "h:\desktop.ini"
del /s /q /f /a "h:\$Recycle.Bin\*.*"
The problem occurs because by default space is a delimiter for the for command, but you can change this using the delims option. If you pick a character that won't ever appear in a file path then it should work fine:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
for /r "H:\" %%a in (desktop.ini $Recycle.Bin) do if exist "%%~fa" echo del /f "%%~fa"
Try it, to make it working remove echo from the script.
del /s /q /f /a ".\desktop.ini"
it should works as charm
save file .bat
put it in any folder
it will delete ini files in folders and sub folders
I have almost no experience in batch, but now I need a script to delete all files that contain certain characters in their names from a folder and its subfolders on Windows 64. I only did simple things in batch like
del "C:\TEST\TEST2\*.TXT"
But I have no idea how to perform the filtering I need.
Could someone help me to achieve this using batch?
EDIT more exactly, the question is "how to tell batch to include subfolders?"
The /s switch exists on quite a few commands (including del)
del /s "C:\TEST\TEST2\*.TXT"
The help for del /? says:
/S Delete specified files from all subdirectories.
Try this:
#echo off & setlocal
set "MySearchString=X"
for /f "delims=" %%i in ('dir /b /s /a-d ^| findstr /i "%MySearchString%"') do echo del "%%~i"
Set the variable MySearchString to the character or string to search and remove the echo command, if the output is OK.
You can also specify the MySearchString for the file name only:
#echo off & setlocal
set "MySearchString=T"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"