Batch file and CHOICE command for multiple files - batch-file

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?

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

Kodi - open .strm files by running a .bat / VBScript

I have legal .strm files of different TV Shows in a folder named TV Shows. Each strm file of each episode is stored in different subfolders.
I would like to run a certain VBScript before these strm files are played.
Then I have a different folder named MOVIES. Again, I would like to run a VBScript before these strm files are played. But this VBScript is a different one.
How would I do that?
Platform is Windows.
Thanks!
If you're looking to open or do something to each file based on extension, An easy way to do this is to use an for loop with /R to search all sub-directories for an *.strm file.
#ECHO OFF
SET "LOC=C:\Folder"
CD %LOC%
FOR /R %%A IN (*.strm) DO (
Rem | Do something with each file.
Start cmd.exe /C "notepad.exe "%%A""
)
Echo No More Items Found!
pause
goto :EOF
%%A will return the file path.
Start cmd.exe /C "" will start a CMD command. (Replace with your code)
notepad.exe "" will open the file (Used as an example)
EDIT:
So you're looking to check if the file exists then if true run the script? Bellow should solve that.
#ECHO OFF
If exist (C:/Path/file.strm) (
Rem | File exists, run script
Echo Do something
)
GOTO :EOF

How to move a file without waiting to complete in Batch (this should be working)

Details:
Using a program called MCEBuddy to process video files and when completed MCEBuddy will run a users batch file for custom processing. In this batch file, I fix up a few things and simply move the proccessed video to it's final location.
Goal:
To allow the batch file to return to MCEBuddy and not wait until the move has completed. Currently MCEBuddy will not resume before the entire file (video) has been moved, I don't want that.
I have 2 batch files, the first simply passes the parameters to the second batch file.
I have search and search, and even asked the developer over at MCEBuddy and is seems that everything I have tried just doesn't work.
The first Batch file:
#ECHO OFF
setlocal EnableDelayedExpansion
SET BatFile="E:\VideoCaptures\Cleaned Files\TVTransfer.bat"
SET OutputFileName="%~n1"
SET InputFileName="%~n2"
SET FolderOutput="%~3"
SET WideoWidth="%~4"
SET OutputExtension="%~5"
SET OutputFileSize="%~z1"
SET OADMonth="%~7"
SET OADDay="%~8"
SET OADYear="%~9"
START "MCE Move" /b cmd /c Call %BatFile% %OutputFileName% %InputFileName% %FolderOutput% %WideoWidth% %OutputExtension% %OutputFileSize% %OADMonth% %OADDay% %OADYear%
The second Batch file (the part that moves the file)
START "MCE Move" /b cmd /c move /y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%
I have also tried:
START "MCE Move" /MIN cmd /c move /Y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%
and:
START "MCE Move" /MIN /b cmd /c move /Y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%
and many other iterations, but none will allow MCEBuddy to resume before the file has completed it's move.
What am I missing?
I'm on my phone right now. But have you tried
START "" "MCE Move" /b cmd /c move /y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%

Batch file echoing duplicate lines

I have a handful of batch files running at users' login via GPOs and a couple of them that create text/batch files with various info are exhibiting odd behavior. Specifically, these batches run at login are echoing the same value(s) multiple times into the target files. As an example:
ECHO #echo off > \\server\share$\%username%.bat
ECHO set minimized=true >> \\server\share$\%username%.bat
ECHO start /min cmd /C "path-to-program" %computername% >> \\server\share$\%username%.bat
Seems pretty straightforward, right? Yet this batch is producing a file that contains:
#echo off
set minimized=true
start /min cmd /C "path-to-program" computer
start /min cmd /C "path-to-program" computer
This isn't my only .bat doing this, but it's all the same concept - echoing a bunch of info into a file and somewhere along the way it's as if parts of it are getting run multiple times.
Has anyone seen this before and/or have any suggestions as to what could be going on?
Try like this :
(ECHO #echo off
ECHO set minimized=true
ECHO start /min cmd /C "path-to-program" %computername%)>"\\server\share$\%username%.bat"

WshShell.run and major problems in Windows 7

This is a VBScript that I would like to improve. I would like four things :
1) Add a line that would rename the extension cleanup.dll to cleanup.exe, so as it can be called by the WshShell.run and executed (hidden).
2) The way it is written just below, the script opens two screens : the screen of the cleanup.exe and a blank screen, which should be hidden for the user and it is not what is happening ! How to hide the second screen ? I want to run it invisibly (the user cannot close or manipulate the second screen. It will be closed via code that is inside the cleanup.exe).**NOTE : The code below works perfectly in Windows XP, but not on Windows 7. How to make it work in all Windows platforms ?
VBSCRIPT "Second.vbs"
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
WshShell.Run "c:\cleanup.exe", 0, TRUE
Set WshShell = Nothing
BATCH "Master.bat"
#echo off
wscript Second.vbs
exit /b
3) Is there a good and reliable software to convert from VBS to EXE ?
4) The other problem I am having is that the command line below does not yield results.Must I use the second pard of the code below instead ? Why ??
Suppose that my Batch file is in located in drive f:\
If I double click on it, my screen should be then populated with information extracted from the TXT file, which actually resides in drive c:\
#echo off
set DRV=C:\August\MyProgram
cd\
cd %DRV%
type test.txt & pause>nul
#echo off
set DRV=C:\August\MyProgram
cd\
c:
cd %DRV%
type test.txt & pause>nul
Thank you in advance for the explanations and solutions
Why run with batch, vbscript is more powerfull and offers more controll.
about the visible console window
WshShell.Run "c:\cleanup.exe", 0, TRUE should hide the console while running and waits before continuing
Make sure you start your script with wscript.exe, not cscript.exe and don't use any wscript.echo
Renaming a file
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "cleanup.dll" , "cleanup.exe"
about the batch cd, practice this in a console window
cd never changes to a drive, only to another map, drive: changes the active drive
d: => d:\>
c: => c:\> (so now if you are on c:\)
cd d:\test =>c:\ (changes your active map on d: to d:\test but since your c: drive is still the active drive you see nothing happening)
d: => d:\test (change drive to d:, you do see that the active map on drive d: is d:\test (at least with the default prompt)

Run commands in a batch file which is inside another batch file

How to run commands in a batch file which is inside another batch file......
I am trying to run commands in different console other than command prompt in a batch file but not able to do so.I am able to start the other console in batch file but not able to pass commands on to it.
My first interpretation of the question led me to believe that Sampath wanted one batch script that has two sets of commands. Calling it would run the 1st set of commands in the parent window, and a second window would open that would run the same script with thd 2nd set of commands.
"%~f0" will give the full path to the currently executing batch script. A simple command line argument serves as a switch to determine which code to run.
#echo off
if "%~1"==":PART2" goto %~1
::use this line if 2nd window is to remain open upon completion
::start "%~f0" :PART2
::use this line if 2nd window is to close upon completion
start cmd /c "%~f0" :PART2
echo Test parent output
pause
exit /b
:PART2
echo Test child output
pause
exit /b
Andriy M suggests Sampath wants to be able to dynamically send commands to the 2nd window. This can be done with 2 scripts that I will call master.bat and slave.bat.
The slave.bat simply reads commands from stdin and executes them. The master.bat launches the slave with input redirected to a command file and then appends commands to the command file.
Here is an example of master.bat that demonstrates dymamically sending commands to the slave. Note that the master prompts for a command, but the slave window will have the focus. Make sure you click on the master so you can enter the command of your choice.
#echo off
:: create an empty command file
type nul >cmds.txt
:: start the slave with input redirected to the command file
start slave.bat ^<cmds.txt
:: issue some commands by appending them to the command file
>>cmds.txt echo echo command 1
>>cmds.txt echo echo command 2
>>cmds.txt echo echo(
>>cmds.txt echo rem /?
:: ask for a command to send to the slave
set /p "cmd=Enter a command to be sent to the slave: "
:: send the command
>>cmds.txt echo %cmd%
::pause so we can see the results in the slave window
for /l %%n in (1 1 1000000) do rem
::tell the slave to exit
>>cmds.txt echo exit
And here is the slave.bat
#echo off
:top
set "cmd="
set /p "cmd="
%cmd%
goto :top
You could try a call statement:
call batchname.bat
this will run the specified batch file in the current open prompt
It almost sounds like what you want is a file that holds commands that you want to run, and to use a batch script to call on those commands when you want?
I've implemented this by creating a batch file that holds all the commands (code snippets) that I find useful, and then using my other batch scripts to call on that "master" file for my snippets.
For example, in my MASTER_BAT.BAT file, an example of a snippet to create dates in different format for usage look like this:
GOTO:%~1
:GET_CURRENT_DATE
:: Created: 1/19/2012
:: Creates variables for the date format in different forms.
:: No additional arguments required
SET DISABLED=0
IF [%DISABLED%] == [1] GOTO:EOF
:: Created: 11/30/11
:: Creates date formats.
Set mdy=%date:~4,2%-%date:~7,2%-%date:~12,4%
Set mdY=%date:~4,2%-%date:~7,2%-%date:~10,4%
Set Dmdy=%date:~0,4%%date:~4,2%-%date:~7,2%-%date:~12,4%
Set DmdY=%date:~0,4%%date:~4,2%-%date:~7,2%-%date:~10,4%
Set ymd=%date:~12,4%-%date:~4,2%-%date:~7,2%
Set ymd=%date:~10,4%-%date:~4,2%-%date:~7,2%
GOTO:EOF
And in my CHILD_BAT.BAT, I want to use that snippet to create the date formats... lets say I want to make it so that I can call the date by the current date in mm/dd/yy format:
CALL MASTER_BAT.BAT "GET_CURRENT_DATE"
ECHO %mdy%
PAUSE
Your output for CHILD_BAT.BAT would be:
1-23-12
Press any key to continue...
Also, any variables created in your CHILD_BAT.BAT prior to the CALL command will be passed to the MASTER_BAT.BAT script as well. However, for loop interation that includes a CALL will not pass the for loop temporary variable.
Hope this is helpful.
EDIT: Note that my snippet is usable for the U.S. date format.

Resources