This is a batch file in Windows.
Here is my .bat file
#echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"
"C:\ThirdParty.exe"
This works fine except the .bat file leaves the command window open the whole time the "ThirdParty" application is running.
I need the command window to close.
I would use the short-cut for the application but I must be able to run this copy command first (it actually changes which data base and server to use for the application).
The ThirdParty application does not allow the user to change the source of the db or the application server.
We're doing this to allow users to change from a test environment to the production environment.
Using start works for me:
#echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe
EDIT: Ok, looking more closely, start seems to interpret the first parameter as the new window title if quoted. So, if you need to quote the path to your ThirdParty.exe you must supply a title string as well.
Examples:
:: Title not needed:
start C:\ThirdParty.exe
:: Title needed
start "Third Party App" "C:\Program Files\Vendor\ThirdParty.exe"
Create a .vbs file with this code:
CreateObject("Wscript.Shell").Run "your_batch.bat",0,True
This .vbs will run your_batch.bat hidden.
Works fine for me.
Using start works fine, unless you are using a scripting language. Fortunately, there's a way out for Python - just use pythonw.exe instead of python.exe:
:: Title not needed:
start pythonw.exe application.py
In case you need quotes, do this:
:: Title needed
start "Great Python App" pythonw.exe "C:\Program Files\Vendor\App\application.py"
Try this:
#echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe
exit
Great tip. It works with batch files that are running a java program also.
start javaw -classpath "%CP%" main.Main
You might be interested in trying my silentbatch program, which will run a .bat/.cmd script, suppress creation of the Command Prompt window entirely (so you won't see it appear and then disappear), and optionally log the output to a specified file.
Or you can use:
Start /d "the directory of the executable" /b "the name of the executable" "parameters of the executable" %1
If %1 is a file then it is passed to your executable. For example in notepad.exe foo.txt %1 is "foo.txt".
The /b parameter of the start command does this:
Starts an application without opening a new Command Prompt window. CTRL+C handling is ignored unless the application enables CTRL+C processing. Use CTRL+BREAK to interrupt the application.
Which is exactly what we want.
I haven't really found a good way to do that natively, so I just use a utility called hstart which does it for me. If there's a neater way to do it, that would be nice.
Using Windows API we can start new process, a console application, and hide its "black" window. This can be done at process creation and avoid showing "black" window at all.
In CreateProcess function the dwCreationFlags parameter can have CREATE_NO_WINDOW flag:
The process is a console application that is being run
without a console window. Therefore, the console handle
for the application is not set. This flag is ignored if
the application is not a console application
Here is a link to hide-win32-console-window executable using this method and source code.
hide-win32-console-window is similar to Jamesdlin's silentbatch program.
There is open question: what to do with program's output when its window does not exist? What if exceptions happen? Not a good solution to throw away the output. hide-win32-console-window uses anonymous pipes to redirect program's output to file created in current directory.
Usage
batchscript_starter.exe full/path/to/application [arguments to pass on]
Example running python script
batchscript_starter.exe c:\Python27\python.exe -c "import time; print('prog start'); time.sleep(3.0); print('prog end');"
The output file is created in working directory named python.2019-05-13-13-32-39.log with output from the python command:
prog start
prog end
Example running command
batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C dir .
The output file is created in working directory named cmd.2019-05-13-13-37-28.log with output from CMD:
Volume in drive Z is Storage
Volume Serial Number is XXXX-YYYY
Directory of hide_console_project\hide-win32-console-window
2019-05-13 13:37 <DIR> .
2019-05-13 13:37 <DIR> ..
2019-05-13 04:41 17,274 batchscript_starter.cpp
2018-04-10 01:08 46,227 batchscript_starter.ico
2019-05-12 11:27 7,042 batchscript_starter.rc
2019-05-12 11:27 1,451 batchscript_starter.sln
2019-05-12 21:51 8,943 batchscript_starter.vcxproj
2019-05-12 21:51 1,664 batchscript_starter.vcxproj.filters
2019-05-13 03:38 1,736 batchscript_starter.vcxproj.user
2019-05-13 13:37 0 cmd.2019-05-13-13-37-28.log
2019-05-13 04:34 1,518 LICENSE
2019-05-13 13:32 22 python.2019-05-13-13-32-39.log
2019-05-13 04:55 82 README.md
2019-05-13 04:44 1,562 Resource.h
2018-04-10 01:08 46,227 small.ico
2019-05-13 04:44 630 targetver.h
2019-05-13 04:57 <DIR> x64
14 File(s) 134,378 bytes
3 Dir(s) ???,???,692,992 bytes free
Example shortcut for running .bat script
Target field:
C:\batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C C:\start_wiki.bat
Directory specified in Start in field will hold output files.
You can create a VBS script that will force the window to be hidden.
Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("""C:\Program Files (x86)\McKesson\HRS
Distributed\SwE.bat""", 0)
set WshShell = Nothing
Then, rather than executing the batch file, execute the script.
run it under a different user. assuming this is a windows box, create a user account for scheduled tasks. run it as that user. The command prompt will only show for the user currently logged in.
Compile the batch file to an executable using Batch2Exe http://www.f2ko.de/programs.php?lang=en&pid=b2e.
Use the "Invisible Window" option.
To make the command window of a .bat file that executes a .exe file exit out as fast as possible, use the line #start before the file you're trying to execute. Here is an example:
(insert other code here)
#start executable.exe
(insert other code here)
You don't have to use other code with #start executable.exe.
I used this to start a cmd file from C#:
Process proc = new Process();
proc.StartInfo.WorkingDirectory = "myWorkingDirectory";
proc.StartInfo.FileName = "myFileName.cmd";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
Please use this one, the above does not work. I have tested in Window server 2003.
#echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"
Start /I "" "C:\ThirdParty.exe"
exit
So below vbscript will launch the cmd/bat file in hidden mode.
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
'MsgBox(strFolder)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & strFolder & "\a.bat" & Chr(34), 0
Set WshShell = Nothing
Now, only app window will be visible, not the cmd.exe window
This is an easy work around for those who are fine with a dirty solution. Press win+tab, drag and drop the bat file to a new desktop and forget about it.
I occasionally will make a bat file that I'll rarely use, and it is kind of a drag to have to use tools to make the window hidden. This is no more complicated than it needs to be for me.
Related
I have two files in one folder, a vbs file called notify.vbs and an executable called foo.exe.
the foo.exe is a node.js pkg-bundled executable that triggers a native desktop notification with a random message from a local text file after 5 seconds from execution. Although pkg had one inconvencience which is that executing foo.exe launches a cmd interface, which doesn't suit my purpose. This is where notify.vbs come in handy as it can execute foo.exe while keeping the cmd interface hidden just like I wanted. notify.vbs looks like this:
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "foo.exe", , , "runas", 1
It works fine. But then, when I change the location of foo.exe for example in a folder called bar directly in the C drive and change the path in the script to:
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "C:\bar\foo.exe", , , "runas", 1
the script starts foo.exe and closes it immediately.
Basically notify.vbs only executes foo.exe when they're in the same folder. When I change the location of either of them it does that weird behaviour I mentioned. I verified the functionality of foo.exe in different locations. It always works. I even verified the path in script each time I tried a different location, it's always correct as it actually does execute foo.exe but it closes it immediately after.
I have no experience with VBScript as I copy pasted that script. Thanks in advance.
I need to run a program (a python script made into an exe) on start up, without the console showing up.
In some question, I found the solution, i.e to execute the program. Right now, I'm testing it out with a simple python program filewriter.py that does -
while count != 1000:
f = open('test.txt','a+')
f.write(str(count))
f.close()
sleep 1
The bat file tool.bat :
#ECHO OFF
python "<absolute_path_here>\filewriter.py"
EXIT /B
The VBS file :
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "<absolute_path_here>\tool.bat" & Chr(34), 0
Set WinScriptHost = Nothing
If I execute the VBS file (double-click it), everything works fine. The output file appears, without the console appearing. So I added it to registry under
HKCU\Software\Microsoft\CurrenVersion\Run
as WScript "path_to_the_vbs_file".
On Startup, the VBS file executes properly (verified it by adding a MsgBox which displayed the popup) but the call to the bat file is not being executed. How do I make this work?
In windows there are two python executables: python.exe, pythonw.exe. If you don't wish to see the terminal window you must use pythonw.exe.
I need to run a program (a python script made into an exe).
If you covert your script to .exe with help of py2exe it is simillar. You can assing your script to console or windows. Look to Py2exe Tutorial, the console variable can be replace forwindows.
You don't need to create EXE files from python. You can run pythonw.exe with path as argument to your script. Why do you need to create .bat which you run from vbscript ? Look here: Run on windows startup CMD with arguments
I forgot to say, that the Windows Python installer normally create following file association, so the script can run directly.
'.py' to python.exe
'.pyw' to pythonw.exe
Other way how you can run the the script on boot is to use Windows Scheduler. The big advantage is you can setup user rights or more events when start the script. You can run the script manually too and you will last status.
Create Python .exe is sometimes tricky. If you don't need to distribute your script to multiple computers I prefer don't use.
[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
If start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat
Okay I created a small script to call "net file" and pump the output to a file. There are no spaces in the filenames, and everything seems okay when I run it interactively. So I run the following and I get no results and no errors:
set oWShell = CreateObject("WScript.Shell")
owshell.run "net file > C:\openfiles.txt"
Set owShell = nothing
Now if modify this just slightly to show execute the same command (and keep my command window open) it works just as expected (except it keeps the cmd window open which I can't have)
set oWShell = CreateObject("WScript.Shell")
owshell.run "%comspec% /k net file > C:\openfiles.txt"
Set owShell = nothing
It must be something obvious that I'm just missing. I only touch vbs once in a blue moon so it isn't something that I'm that used to using.
Any help would be appreciated! Thanks in advance!
You need a shell (i.e. %comspec%) to get shell features like redirection; the persisten window is what you asked for: the /k lets the shell stay open (try /c instead) and you should use the second and third parameter of the .Run method to get a hidden window (and possibly wait for the process before you zap the owShell).
Have a look at Run. Changing
owshell.run "%comspec% /k net file > C:\openfiles.txt"
to
owshell.run "%comspec% /c net file > C:\openfiles.txt", 0, FALSE
This will hide the cmd and carry on with the rest of the script... If you want to wait for the command to finish, change FALSE to TRUE.
Also keep in mind that some machines are funny about letting you write files directly to C:\. Possibly create a test folder and write to there instead!
I've created a simple batch file that kicks off my *.msi installer within our company, creating a log file of the process, then displays the log file after the installer has completed.
installAndLog.bat:
msiexec.exe /i "\\FileServer2\setup.msi" /l*v "C:\setupLog.txt"
"C:\setupLog.txt"
It works, but there are two (2) glitches that annoy me:
The black console box shows in the background the whole time the installer is running and the log file is being displayed. Q1: How do I hide that?
and
The console box will not close until the log file is no longer being viewed (i.e. notepad.exe is closed). Q2: Can I call the text file in a new process and simply exit?
I was a DOS lover back in the day, but that was too many years ago.
I don't think you can hide the console window when running a batch file. However you can use vbscript instead which will by default not create a console window.
Take the below and put it in a file with a .vbs extension:
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "msiexec.exe /i ""\\FileServer2\setup.msi"" /l*v ""C:\setupLog.txt""", 1, true
wshShell.Run "C:\setupLog.txt"
All the double double quotes are there because the entire command must be surrounded by "'s and doubling them escapes them. The the documentation for WshShell.Run for more info.
Q1 - AFAIK you can't really hide the console window.
Q2 - Use the start command. This will launch the specified program (notepad) outside of the shell. It will also prevent the shell from waiting until the application closes to continue processing the batch script.
You might be better off changing the batch script to launch the MSI installer using the start command and having the installer launch notepad to view the log file once installation is complete.
If you really want to get these batch windows away, you'll have to switch over to something else. One simple alternative could be one of the scripting languages supported by the windows scripting host.
Or you try HTA (HTML applications) see here and here.
Run the dos script as a different user by scheduled task or as a service.