Home > front end >  Batch file and CHOICE command for multiple files
Batch file and CHOICE command for multiple files

Time:02-25

Having a Windows program that does wipe files and folders from the context menu too even with no confirmation at all then I created a batch file with the following content

@ECHO OFF
CHOICE /M "Do you want to Wipe Files"
IF ERRORLEVEL 2 GOTO QUIT
IF ERRORLEVEL 1 GOTO RUN
:RUN
START "" "C:\Program Files\MyProgram\myexec.exe" /wipe "%~1"
EXIT
:QUIT
EXIT

and a registry key with the following entry

[HKEY_CLASSES_ROOT\*\shell\wipefiles\command]
@="\"C:\\Program Files\\MyProgram\\myexec-start.cmd\" \"%1\""

just to get a safety chance before to wipe the file or the folder and it does work.

However if multiple files are selected then it prompts you to answer Y or N for the times as far as the number of files is and furthermore the cmd window remains open until you hit Y or N the times as far as the number of selected files is.

Is there a way in order to answer a single time Y or N (closing the cmd screen) regardless of the number of selected files?

CodePudding user response:

Seemingly simpler to use the following command (which does not depend on any external software)

@ECHO OFF
CHOICE /M "Do you want to Wipe Files"
IF ERRORLEVEL 2 GOTO QUIT
IF ERRORLEVEL 1 GOTO RUN

:RUN
for %%f in (%*) do del /q "%%f"
for %%d in (%*) do rd /s /q "%%d"

:QUIT

Where del is an internal command to delete files, and rd is an internal command to delete folders. The call to the batch parameter "% *" is to include all the files and folders selected for deletion.

CodePudding user response:

There seems to be no simple way to do what you request from the context menu, since a separate process is opened for each selected file. But it can be done easily from the send to menu: Create a shortcut in the SendTo folder for your script, and now you can send as many files or folders as you want. The script reads as follows:

@ECHO OFF
CHOICE / M "Do you want to Wipe Files"
IF ERRORLEVEL 2 GOTO QUIT

:: If your software enables file processing in a chain, such as myexec.exe /wipe filename1 filename2 ..., use this syntax:
START "" "C:\Program Files\MyProgram\myexec.exe" /wipe %*
:: If not, use the for loop:
for %%f in (%*) do START "" "C:\Program Files\MyProgram\myexec.exe" /wipe %%f

: QUIT

Maybe it's a bit of a deviation from the theme, but you can do it in VBScript like this:

Set Shell = CreateObject("WScript.Shell")
if MsgBox("Do you want to Wipe Files?", vbQuestion   vbYesNo, "wipefiles") = vbYes then
  for each Arg in WScript.Arguments
    Shell.Run """C:\Program Files\MyProgram\myexec.exe"" /wipe " & """" & Arg & """",0,True
  Next
end if

It will display a graphical message box asking the user to confirm the deletion, without any terminal window.

  • Related