Delete files in context menu with bat file windows 8 - batch-file

my bat file:
#echo off
set targetfolder=%1
cd /d %1
del .
pause
my delete.reg file
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Run Batch script]
#="Delete all"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Run Batch script\command]
#=C:\delete.bat \"%V\"
The option is in the context menu but if I click Im getting this error:
This file does not have a program associated with it for performing this action. Please install a program or, if one is already installed, create an association in the Default Programs control panel.
[OK]

You don't really need a batch file, you should be able to run a command directly from the registry key.
Example:
#ECHO OFF
SETLOCAL
SET "SKEY=HKCU\Software\Classes\"
SET "EKEY=\shell\DeleteAll"
REG ADD %SKEY%Folder%EKEY% /VE /D "Delete &All Files" /F>NUL
REG ADD %SKEY%Folder%EKEY%\command /VE /T REG_EXPAND_SZ /D^
"%%COMSPEC%% /C PUSHD %%L && DEL *.*" /F>NUL
Right-click on a folder and select 'Delete All Files' to delete all normal files within that folder. (Be careful this will be catastrophic if you select the wrong folder)

Related

Run registry issue. DELETE script opening folder

I created two scripts, one to delete shortcuts from a folder whenever the system boots up using Run registry and the other to add REG key to Run registry. The problem is whenever I bootup my device it keeps opening the folder location specified in the DELETE Script. Any solution for this guys ? I will share the commands that I used below.
DELETE script :
del /s /q "%Appdata%/Microsoft\Windows\Start Menu\Programs\Startup\*.*"
ADDING REG KEY TO RUN script :
xcopy /s "%~dp0delete.bat" "C:\Program Files\perma\"
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v Delete /t REG_EXPAND_SZ /d "C:\Program Files\perma\delete.bat" /f

Batch Script to copy folder from Server to user desktop in a network

My goal is to create a batch script to copy a folder with subfolders to user desktop with overwrite option and minimized of command prompt.
I am pushing the script through Group policy in user start up screen.
However I am getting an error when running the script locally. Not sure what I am missing in the script..
#echo off
#cls
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
echo Your Source Path:
set INPUT1=\\X.X.X.X\Test\TMS\
echo Your Destination Path:
set INPUT2=C:\Users\%Username%\Desktop\
xcopy %INPUT1% %INPUT2% /y /s
:minimized
You mentioned folder, so I am writing this assuming you want to create the TMS folder with content on the desktop.
I would try something like this inside of you minimized label. This is untested as I have no Network drives to test with.
for /f "tokens=2" %i in ('net use * "\\X.X.X.X\Test\TMS\" ^| findstr /i Drive') do set "tmpDr=%%i"
mkdir "%USERPROFILE%\Desktop\TMS" >nul
xcopy "%tmpDir%\*" "%USERPROFILE%\Desktop\TMS" /s /y
net use /d %tmpDir% >nul
Some things to note in your code. You have 2 minimized labels, you need to enclose path variables with double quotes to eliminate possible whitespace, you can drop the echo's seeing as you plan on running the script minimized. Last but not least, you do not need to specify full path to the user's desktop as %USERPROFILE% variable already exists as Drive:\Users\Username

Having troubles with batch files

This issue is eating my brains of. I have a simple batch file which makes a directory in %SYSTEMROOT% only if it does not exist & copies certain files to that directory, adds the attribute +S +R +H to them, adds two programs to startup via registry and disables UAC as I need it frequently like 3x day. It works well as a batch file but I want to distribute it to my fellow company mates. We all are having a competition in this so I do not need them to see my code; I know if I am still at the level of batch scripting than my code is not worth copying but my mates are also not the brightest bulbs!
My issue is that when I convert it to exe using Quick Batch Convertor as the moment it becomes an exe it starts giving Access denied error only when It gets to copy the files in %SYSTEMROOT% even though I am running it as administrator and the disabling UAC command, which is C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f works, which, originally should require admin privileges. Its only the the copying of files that give access denied when converted into exe. They all just work fine if it is in a form of batch.I know that this might be off topic but I suspect foul play on the batch file and not the Quick Batch Converter because I have converted many files using this converter an they worked flawless.
The code for my batch is here
#echo off
echo %CD%
cd %~dp0
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Update" /t REG_SZ /d "\"C:\Windows\System32\SystemSettingsUpdate\HL~Realtime~Defense.exe\" " /f
Reg.exe add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "Antivirus-Update " /t REG_SZ /d "\"C:\Windows\System32\SystemSettingsUpdate\Configure.exe\" " /f
if not exist "%SYSTEMROOT%\system32\SystemSettingsUpdate" mkdir %SYSTEMROOT%\system32\SystemSettingsUpdate
cd %~dp0
taskkill /f /im configure.exe
copy "%~dp0HL~Realtime~Defense.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
copy "%~dp0Whatsapp,Inc.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
copy "%~dp0Configure.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate"
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\Configure.exe
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\Whatsapp,Inc.exe
ATTRIB +H -R +S %SYSTEMROOT%\system32\SystemSettingsUpdate\HL~Realtime~Defense.exe
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v ConsentPromptBehaviorAdmin /t REG_DWORD /d 0 /f
exit
Any Suggestions?
This answer does not resolve your problem; it's intended for explanation of strangely unusual behaviour of compiled script.
Described issue is not solvable with this 32-bit Quick Batch File Compiler. For proof, create a batch script battoexeTest.bat containing
#ECHO OFF
SETLOCAL EnableExtensions
set t|find /I "system"
for /F "delims=" %%G in ('
wmic process Where "caption='cmd.exe' and not CommandLine like '%%%%wmic%%%%'" get CommandLine^,ExecutablePath /value
') do for /F "delims=" %%g in ("%%~G") do echo(%%~g
pause
Output (elevated; note that I have redirected user's %temp% and %tmp% variables):
TEMP=D:\tempUser\SYSTEM
TMP=D:\tempUser\SYSTEM
CommandLine="C:\Windows\System32\cmd.exe" /C "D:\bat\battoexeTest.bat"
ExecutablePath=C:\Windows\System32\cmd.exe
If you run compiled version of above script batToExeTestY.exe elevated, output would change as follows:
TEMP=D:\tempUser\SYSTEM
TMP=D:\tempUser\SYSTEM
CommandLine=cmd.exe /c ""D:\tempUser\SYSTEM\8YQTO48H.bat" "D:\bat\batToExeTestY.exe" "
ExecutablePath=C:\Windows\SysWOW64\cmd.exe
You can see that
batToExeTestY.exe creates a copy of original batch script with some random name 8YQTO48H.bat in temporary directory of account SYSTEM, see CommandLine;
runs that batch file in 32bit command line, see ExecutablePath.
hence, 32-bitness proved.
Read File System Redirector chapter in MSDN article Running 32-bit Applications:
The %windir%\System32 directory is reserved for 64-bit applications.
… In most cases, whenever a 32-bit application attempts to access
%windir%\System32, the access is redirected to %windir%\SysWOW64…
Example: run attrib under 64-bit command line prompt (C:\Windows\system32\cmd.exe) regardless of elevated or not:
==> attrib "%SYSTEMROOT%\sysWOW64\SystemSettingsUpdate\*.*"
A R C:\Windows\sysWOW64\SystemSettingsUpdate\WOW-cliParser.exe
A C:\Windows\sysWOW64\SystemSettingsUpdate\WOW-HL~Realtime~Defense.txt
==> attrib "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"
A C:\Windows\system32\SystemSettingsUpdate\cliParser.exe
A SHR C:\Windows\system32\SystemSettingsUpdate\HL~Real~Def.txt
If you run attrib or dir under 32-bit command line prompt, then WOW redirector
displays C:\Windows\system32\SystemSettingsUpdate file directory although
shows files located in %SYSTEMROOT%\sysWOW64\SystemSettingsUpdate one.
Take a look:
==> %windir%\SysWoW64\cmd.exe /C attrib "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"
A R C:\Windows\system32\SystemSettingsUpdate\WOW-cliParser.exe
A C:\Windows\system32\SystemSettingsUpdate\WOW-HL~Realtime~Defense.txt
==> %windir%\SysWoW64\cmd.exe /C dir /A "%SYSTEMROOT%\system32\SystemSettingsUpdate\*.*"|find ":"
Directory of C:\Windows\system32\SystemSettingsUpdate
01.03.2016 12:25 <DIR> .
01.03.2016 12:25 <DIR> ..
01.03.2015 12:31 5 120 WOW-cliParser.exe
26.02.2016 08:54 84 WOW-HL~Realtime~Defense.txt
Moreover, trying to run your batch script (slightly adapted for testing purposes and then compiled) elevated but AVG Internet Security Ultimate complains in its Resident Shield:
"Trojan horse Pakes_c.BWYN, d:\bat\batToExeTest.exe";"Secured";"25. 2. 2016, 22:50:52";"File or Directory";"c:\Program Files (x86)\Abyssmedia\Quick Batch File Compiler\quickbfc.exe"
and on copy "%~dp0XYZ.exe" "%SYSTEMROOT%\system32\SystemSettingsUpdate" line and/or on attrib lines in its Identity Protection module:
"IDP.ALEXA.51, D:\tempUser\SYSTEM\8W88ULA2.bat";"Secured";"26. 2. 2016, 8:35:14";"File or Directory";""
"Unknown, D:\tempUser\SYSTEM\0G8KOWPT.bat";"Secured";"26. 2. 2016, 1:08:25";"File or Directory";""
Could be a false positive, but you definitely need to use some virus-free and 64-bit-compliant bat to exe converter…

Command Prompt Scripting

This is going to be hard to explain, I'll try my best.
I'm using a shell scripting (Test Executor) in command prompt to automate our regression testing. However i'm stuck on one thing.
Basically I have to copy this dynamically created folder in a location into another folder after the automated script is done (in CMD via scripting)
:: create an achieve directory
#SET ACHIEVE_DIR=C:\Test Result\LOG_Files
mkdir "%ACHIEVE_DIR%"
xcopy /y /s /e "%TEST_SOURCE_DIR%"\*.* "%ACHIEVE_DIR%"
This will copy all the files in the TEST_SOURCE_DIR, but i only want this dynamically created folder which changes according to the date stamp (BUT this date stamp is calculated by second - so i can't use that as a variable).
For example: After i run a test it'll create a folder under lets say C:\temp\ another folder named "TEST RUN - _2014-06-30 15_48_01_"
So if go into C:\temp i'll find a folder named "TEST RUN - _2014-06-30 15_48_01_" along with other folders. BUT i only want to copy this dynamic folder into Test Result\LOG_Files
What we are given is that the starting string will always be "TEST RUN -" but the ending string will interchange according to the time to the second (BUT we cannot go by that since it is the second of the starting test result).
I found out if i am in command prompt and i type in "cd TEST RU" and hit tab it'll finish it for me "cd TEST RUN - _2014-06-30 15_48_01_" Is there a way to script this tab hit or something?
Thanks!
This should work in a batch file to isolate the folder:
If there are a number of folders starting with test run - then tell us.
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do xcopy "%%a\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
This code looks for a test folder as discussed in the comments:
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do (
pushd "%%a"
for /d /r %%b in ("test*") do (
xcopy "%%b\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
)
popd
)
This batch file should do the job:
#echo off
rem Find the directory starting with "TEST RUN -" created last in C:\Temp.
set ACHIEVE_DIR=C:\Test Result\LOG_Files
for /F "usebackq delims=" %%D in ( `dir "C:\Temp\TEST RUN -*" /AD /B /O-D /TC` ) do (
set TEST_SOURCE_DIR=%%D
goto CopyDir
)
echo No directory starting with "TEST RUN -" found in C:\Temp.
pause
exit /b
:CopyDir
rem Copy all files and subdirectories from last test run to achieve
rem directory whereby xcopy automatically creates the achieve directory.
xcopy /E /I /Q /S /Y "%TEST_SOURCE_DIR%" "%ACHIEVE_DIR%"
set ACHIEVE_DIR=
set TEST_SOURCE_DIR=
Read the Microsoft pages about
dir
for
xcopy
for details about the used parameters of those 3 commands.
Or execute in a command prompt window the commands
dir /?
for /?
xcopy /?
one after the other to get the shorter help for those 3 commands output in the window.

executing all the .reg files from batch file

I have following script that executes all the .reg files in the current directory. the problem is i also have sub directories in that folder and i want to execute those reg files as well. I am not sure what switch i am supposed to use. i tries using /s which returns all the .reg files of the drive.
this is how my batch file looks like:
rem #echo off
cls
SET folder=%~dp0
for %%i in ("%folder%*.reg") do (regedit /s "%%i")
echo done
pause
EXIT
This is how file/directory structure looks like:
Folder1
batchfile.bat -> user double clicks or executes from command prompt
1.reg
2.reg
3.reg
Folder2
4.reg
5.reg
what am i missing here?
I think that you need a recursive for loop using the /R switch:
for /R "%folder%" %%i in (*.reg) do (regedit /s "%%i")
You didn't actually say what your problem was, so instead of fixing your script here's what I would have done:
#echo off
set folder=%~dp0
for /f "usebackq" %%i in (`dir /s /b *.reg`) do (
regedit /s "%%i"
)
And the /s script on regedit is for silent mode. It tells regedit not to ask the user for confirmation with a dialog box. The for loop is what causes the batch file to loop through all subdirectories, not the /s switch on regedit.
From Microsoft's Knowledge Base:
[/s|-s]
When a filename is specified on the command line, this switch is used to
suppress any informational dialog boxes that would normally be displayed.
This is useful when the Setup program for an application wants to execute
REGEDIT.EXE with a .REG file, but does not want the user to be confused by
any dialog boxes that are displayed.

Resources