I want to run a script if i press the close(x) button in the batch file. Suppose batch file is running, if i press the close(x) button i want to run some script before closing the batch file incomplete run.
Use start from a different batch file and add the /WAIT argument.
As such:
#echo off
start %cd%/yourbatchfilehere.bat /WAIT
REM additional post-close code here.
Additional info here
This is, as far as I can see, the only appropriate way to get even close to solving your problem using only the system you asked us to use. Since windows' batch implementation provides no way to detect when a batch window is closing and stop it.
You might want to look into something like this. To avoid a user closing the window - just don't show them the window.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "mk123456.bat" & Chr(34), 0
Set WshShell = Nothing
You can use the following vbs script, which will run two files ('Hidden.bat' and 'Visible.bat') so that when 'Visible' is closed, 'Hidden' will run without being shown. This is based on unclemeat's answer and this post.
set WshShell=createobject("wscript.shell")
WshShell.run "Visible.bat", 1, true
WshShell.Run chr(34) & "Hidden.bat" & Chr(34), 0
However I don't know how to pass arguments (especially multi-word ones) to the batch script.
Related
I have a .vbs file, which I have set to run at startup through regedit. Basically what the vbs does, is execute another program in the same directory (I will paste the vbs script below). Normally, the vbs script works great and everything is good. However, whenever the vbs script run at startup (i.e auto running right after the computer is booted), I always get a error message, telling me that Windows cannot find my file (i.e hello.exe), even though the exe file is right there.
I have tried setting a delay to the script, but that resulted in the same problem. I am extremely confused because everytime I run the vbs manually (like double click it), everything works fine, no problem.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "hello.exe" & Chr(34), 0
Set WshShell = Nothing
The expected result is that the vbs script will just run normally, like how it runs everytime i manually launch it. The error message is "Line 2: File cannot be found", or something along the lines of that.
As Hackoo is getting to, use the full path to the EXE you are running:
WshShell.Run chr(34) & "C:\My Hello App\hello.exe" & Chr(34), 0
This will run the exe file if its in the same folder as your script.
strPath = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName,"\"))
WshShell.Run chr(34) & strPath & "hello.exe" & Chr(34), 0
I have a shortcut running this command when clicked: cmd /c "full path to my batch file". When I use it, it does what it is supposed to do, but in the process the ugly console window pops up. Is there any way to make this command start a hidden or at least minimized window?
Use the command start with switch /min to start cmd in minimized window:
start /min cmd /c "full path to my batch file"
powershell "start <path of batch file> -Args \"<batch file args>\" -WindowStyle Hidden"
This can be placed in a separate batch file which, when called, will terminate immediately while your batch file executes in the background.
From ' Args ' to ' \" ' can be excluded if your batch file has no arguments.
' -v runAs' can be added before the end quote to run your batch file as an administrator.
I found this solution :
Create a launch.vbs file and add
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Replace "C:\Batch Files\syncfiles.bat" by your absolute or relative path file name.
Source : https://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/
Source MSDN : https://msdn.microsoft.com/en-us/library/d5fk67ky(VS.85).aspx
Right-click on the shortcut icon and choose "Properties."
On the "Shortcut" tab, choose the "Run" type you desire from the dropdown menu.
The START command has a /B switch to start without creating a window. Use START /? to read all about it.
This will create a separate process (without a window), and not block parent window so it can continue your main work:
start /b cmd /c "full path to my batch file"
Use AutoHotKey file. Download and install AutoHotKey first.
suppose you have an 1.bat
you'll create a C:\2.ahk, whose content is
Run C:\1.bat,,Hide
return
and you'll create a 3.lnk, and right click it, click property, then set the Target to
"C:\Program Files\AutoHotkey\AutoHotkey.exe" C:\2.ahk
Then you'll get what you want.
This way, you can attach the 3.lnk to your taskbar or start menu, and also change its icon.
The start method can only be used in a bat, which can't be added to taskbar or changed icon.
Create a VBScript file as a shell to start it.
' Launcher.vbs
If WScript.Arguments.Count = 0 Then
WScript.Quit 1
End If
Dim WSH
Set WSH = CreateObject("WScript.Shell")
WSH.Run "cmd /c " & WScript.Arguments(0), 0, False
You may want to embed this as a Here Document in your batch file. See heredoc for Windows batch?
The title says it all: i want to start a cmd window with a command, but i want the window hidden
start cmd.exe /k "my command"
This does what I want, but the cmd window remains open, and upon closing i end the command too. I want to run the cmd.exe in the background. Is it possible?
This question was answered here: How to run a command on the background on Windows?
Basically, you just need the /b option from the start command.
If that does not help, go the VB way, creating a .vbs like this:
Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\Scheduled Jobs\mybat.bat" & Chr(34), 0
Set WinScriptHost = Nothing
Here's my situation:
I have a BAT file that takes a long time to run (1minute to 70 minutes)
I schedule it with Windows scheduler to run every 10 minutes
If it schedules again while it is still running, nothing happens (this is good)
My problem is that I need my BAT to run silently, and it doesn't. So, I want to launch it with a Windows script like the following:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Unfortunately, when I schedule this script, it does the job but returns instantly, making Windows scheduler think the task is finished when in reality the BAT is just running off by itself somewhere.
Because of this, Windows will reschedule the job again 10 minutes later and make multiple run.
What I need:
Is there a way to tell the Windows script file to wait for the target of the .Run command to complete before progressing/exiting? Basically, I want it to act like I launched another thread and then called join on it, so it does the same thing the BAT would have, but without displaying the console window.
Other Solutions
Tell me any other way to get this BAT to execute silently (powershell commands, whatever) and I'll accept it as a solution as well. Just don't tell me to write a full on C++/C# appliation around it, that's overkill :)
Running: Windows Server 2008 R2
I think all you need is TRUE for the optional 3rd argument
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0, TRUE
Here goes the madness...
Create a file invisible.vbs:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, True
Then in the task scheduler, call your script like this:
wscript.exe "C:\Batch Files\invisible.vbs" "C:\Batch Files\syncfiles.bat"
I tested it and this does really work: No command window, and the task is considered running until the batch script ends.
Credit goes to https://superuser.com/a/62646/40362.
For 2008 R2 make invisible.vbs have this content, and just execute it directly. It will run the BAT silently and wait for completion.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "D:\IUpdateScript.bat", 0, true
I've searched this and some pages came which weren't really useful or were too complicated (I am not a skilled batch file programmer!)! What I need is to run a batch file in hidden form (no console window). The batch file will not be called from external application or code. It will be clicked on by the client and then I want no console pages to be shown (only pages which are called by call command should be shown)! The batch file is exactly as follows:
#echo off
call setup.exe
IF EXIST "C:/caillog" goto tracking
IF NOT EXIST "C:/caillog" goto end
:tracking
call dotnet4.exe
call ClientService.msi
goto end
:end
I use VBScripts to open it hidden, like this:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%batchfile%"), 0, True
for e.g the bat file I want to run is run.bat then I'll do like this
objShell.Run("run.bat"), 0, True
Instead of running the batch file run the vb file.
Write it in notepad and save it as *.vbs
If your Windows system supports powershell you can place this infront of "#echo off":
cmd /c powershell -Nop -NonI -Nologo -WindowStyle Hidden "Write-Host"
As others have said, use VBS.
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\FilePath" & Chr(34), 0
Set WinScriptHost = Nothing
This is what I use.