I'm using TinyWeb server.
I run it using a batch file START_tiny.bat:
c:
cd\
cd tiny
cd bin
tiny c:\tiny\root 8080
exit
The problem is that after tiny.exe is executed, the console won't close so it hangs here:
I can manually close the console window, and it will continue to run as expected, but I was just wondering if there was a way in the batch file to make sure it closes after the program is invoked.
EDIT:
the solution was:
cmd /c start tiny c:\tiny\root 8080
The Windows command processor cmd.exe halts processing of the batch file as long as tiny.exe is running and waits for its termination, even if this application is a GUI application not opening any window or opening a GUI window.
The command exit is not necessary at all and is just bad for debugging the batch file.
It would be better to use a shortcut file (*.lnk) to just start TinyWeb server. Target in properties of shortcut file would be C:\tiny\bin\tiny.exe C:\tiny\root 8080 and Start in would be C:\tiny\bin or C:\tiny\root or whatever should be the current directory on starting TinyWeb. No console window is shown on tiny.exe not being a console application which I don't know as not having downloaded and installed this application.
The command start can be used in a batch file for starting TinyWeb server as separate process by cmd.exe without waiting for its termination.
One of the following command lines could be used in the batch file:
start C:\tiny\bin\tiny.exe C:\tiny\root 8080
start /DC:\tiny\bin tiny.exe C:\tiny\root 8080
start /D C:\tiny\bin tiny.exe C:\tiny\root 8080
start "TinyWeb server" C:\tiny\bin\tiny.exe C:\tiny\root 8080
start "TinyWeb server" "C:\tiny\bin\tiny.exe" "C:\tiny\root" 8080
start "TinyWeb server" /D"C:\tiny\bin" tiny.exe "C:\tiny\root" 8080
start "TinyWeb server" /D "C:\tiny\bin" tiny.exe "C:\tiny\root" 8080
Open a command prompt, run start /? and read the output help for an explanation of the above command lines.
The parameter /D specifies the start in respectively current directory for the started executable. It can be specified immediately after /D or separated with a space from the switch.
Command start interprets first string in quotes as window title. For that reason it is necessary to explicitly specify a window title in double quotes on one of the other arguments is enclosed in double quotes even if no console window is opened because of started application is a Windows GUI application. An empty title string specified with just "" is enough for GUI applications started by command start as separate process.
The command line cd /D "C:\tiny\bin" can be used to change current directory to C:\tiny\bin even on current directory being on a different drive. The help output on running in a cmd window cd /? explains the parameter /D to change also drive if necessary and not only current directory on current drive.
Related
I would expect the below code to open up in the C:\Users\zjafri\Desktop\Arthur\Runner2 directory when running this bat file, but instead it opens up to my desktop directory.
Does somebody have an idea why?
%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
CD C:\Users\zjafri\Desktop\Arthur\Runner2
The console output is:
C:\Users\zjafri\Desktop>C:\windows\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
(base) C:\Users\zjafri\Desktop>
The used command lines are wrong for the wanted behavior which is activating Anaconda environment with making a specific directory the current directory and keep the command process running for entering manually more commands to execute.
A batch file is a script. A script needs a script interpreter executable. The interpreter for Windows batch files is the Windows command processor cmd.exe which is with full qualified file name %SystemRoot%\System32\cmd.exe or %ComSpec%. SystemRoot and ComSpec are two predefined Windows Environment Variables.
Windows command processor halts the processing of a batch file on starting an executable until the started executable terminated itself.
What does happen on cmd.exe instance processing the batch file executes the following command line?
%windir%\system32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
Windows command processor starts one more command process to process the other batch file and keeps running after batch file execution finished because of option /K enclosed uselessly in double quotes. So the started second instance of cmd.exe does not terminate itself. For that reason the first instance of cmd.exe waits and waits and waits for termination of second instance of cmd.exe before it continues processing the batch file with reading the next command line.
Therefore the command line CD C:\Users\zjafri\Desktop\Arthur\Runner2 is executed by first command process only when the user typed exit in second command process to terminate second cmd.exe instance.
One solution would be the usage of following command lines in a batch file:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat" "C:\ProgramData\Anaconda3"
cd /D "%UserProfile%\Desktop\Arthur\Runner2"
This batch file results in executing the batch file activate.bat, next is executed command CD and then processing of batch file is finished.
What happens next depends on how the batch file was started. If just a double click was made on the batch file, Windows called cmd.exe with using implicit option /C to execute the batch file and then close the command process. So on double clicking the batch file, a console window opens, the two command lines are executed and the console window closes as cmd.exe terminates itself.
But if the user opened a command prompt which means starting cmd.exe with using implicit the option /K, and executes the batch file from within the command prompt window by typing its full qualified file name without or with the completion help by hitting one or more times TAB after entering just a few characters of a folder/file name, and hits RETURN or ENTER to execute the batch file, the command prompt window remains opened and Anaconda environment is activated with directory %UserProfile%\Desktop\Arthur\Runner2 being the current directory.
Well, first opening a command prompt window and manually running the batch file with full path is not really handy. The batch file could be stored with a short file name in one of the folders of which path is present in value of environment variable PATH like the Windows directory. This would make it possible to open a command prompt, type just the file name of the batch file and hit key RETURN or ENTER to execute it.
Another handy solution would be creating on user's desktop a shortcut file (*.lnk) with file name Anaconda3 on which in properties of the shortcut is configured:
Target: %SystemRoot%\System32\cmd.exe /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
Start in: C:\Users\zjafri\Desktop\Arthur\Runner2
Comment: Opens a command prompt and activates Anaconda3 in Runner2
There are multiple advantages on using a shortcut file. A shortcut key can be defined to start Windows command processor and execute the batch file to activate Anaconda environment independent on which application has currently the input focus and if the desktop is behind an application window or in front of all other windows. On the tabs Options, Font, Layout, Colors of the shortcut properties the options and the look can be defined which should be used on using Anaconda like using a console window with more lines/columns and with a larger font as by default.
Although the usage of a shortcut file would be definitely the best solution for this use case, it is also possible to run a batch file with a double click to open a command prompt window to execute the batch file to activate Anaconda environment and keep the command process running with current directory set to wanted directory.
Such a batch file would require just one command line:
#start "Anaconda3" /D"%UserProfile%\Desktop\Arthur\Runner2" %ComSpec% /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
In this case cmd.exe processing the batch file with this command line starts a new command process with option /K to keep the command process running after execution of the batch file by started cmd.exe with Anaconda3 as title for the new console window and with setting before starting cmd.exe the directory CD C:\Users\zjafri\Desktop\Arthur\Runner2 as current directory. cmd.exe does not wait for termination of the started second cmd.exe in this case and so the cmd.exe instance processing the batch file with this single command line terminates immediately.
The disadvantage of this batch file solution is that the user has no possibility to configure the options, font, layout, colors of the command process which keeps running after activating the Anaconda environment.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
cd /?
cmd /?
start /?
PS: I have installed neither Anaconda nor read its documentation. So I don't know what happens on execution of C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3 and what is the current directory after execution of this batch file. If the batch file is good coded and the argument C:\ProgramData\Anaconda3 does not mean to change the current directory to this directory, then everything should work as written above. Otherwise the provided solutions need to be adapted depending on what activate.bat changes on environment, i.e. when it changes the current directory.
perform the cd command prior to the cmd command (concatenated) and wrap it all in a code block.
(CD C:\Users\zjafri\Desktop\Arthur\Runner2 & cmd.exe /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3)
The issue
I am trying to create a batch file that will execute the following two commands:
cd /some/path
dotnet run
that produces the following output:
It starts my localhost server.
Trying To Accomplish
What I would like to do is, put those two commands in a batch file and automatically open Chrome to the server address, HOWEVER, the dotnet command takes some time to finish. So somehow, I would have to keep monitoring localhost to see if it is available.
Any help would be appreciated. Although opening a CMD window, typing those 2 commands, and waiting a minute isn't all that much of a problem, it sure would be nice to just click on a batch file.
Thank you.
You can create a batch file like this code :
#echo off
Set "ApplicationPath=%UserProfile%\source\repos\PruttPos\PruttPosSystem\"
CD /D "%ApplicationPath%"
dotnet run
Start "Chrome" Chrome.exe "http://localhost:5000"
pause
When trying to open vscode folder using a batch file, Visual Studio opens up with that folder, but also a cmd window pops up and does not go away if you use exit command.
#echo off
start code "C:\GitHub\TestApp\testapp"
exit
VSCode opens up correctly, but also this window opens
Using VSCode 1.52.1, the only way I could start it without having a cmd window open after exiting the batch script is:
explorer.exe "%userprofile%\AppData\Local\Programs\Microsoft VS Code\Code.exe"
Note: it does not involve opening a specific local directory to work with. But maybe you can find a solution such as saving the folder as a workspace or using Ctrl+R to open recent folders. Plus, if you work only within that directory / workspace, or use it right before closing VSCode, it will be opened automatically at the next launch.
That's because you are actually invoking the batch file code.cmd which is located at [VSCodePath]\bin\code.cmd. The code.cmd file in turn invokes the actual VSCode executable code.exe
When invoking a batch file (.BAT or .CMD) using the start command, a new instance of CMD process will be created to handle the execution of the batch file, But it invokes the CMD process with the /K switch rather than /C
For example start code.cmd executes cmd /k code.cmd
It is the /K switch that causes the new cmd to remain open after finishing the execution of the batch file.
To resolve, instead of supplying the batch file directly the to the start command, execute it by an explicit CMD invokation:
#echo off
start cmd /C code "C:\GitHub\TestApp\testapp"
exit
That CMD window is associated with the VSCode instance that you just opened. Attempting to close it will terminate the application you started. (in this case, VSCode)
The start xxx xxx... command opens up a new cmd terminal to perform its action. Even though a new prompt appears, which can be used as a normal terminal itself, the VSCode process is inexorably linked to it as the parent process.
If your goal is to not launch a separate cmd window, then run:
start code /b "C:\GitHub\TestApp\testapp"
which just runs the command in the same window. The VSCode window is still inexorably bound to the current cmd window and will close if the cmd window disappears, but at least another cmd window isn't launched.
Windows doesn't have the capability to launch a program in the background from the terminal.
If all described solutions did not work for you, try making an ordinary Windows shortcut to "C:\Users\username\AppData\Local\Programs\Microsoft VS Code\Code.exe" C:\path-to-project-folder-or-file.
Then call this shortcut in your .bat or .cmd script like that (assuming shortcut name is shortcut):
#echo off
start C:\path-to-shortcut-file\shortcut
START "Test Server" "%~dp0\server.exe" LAN %M%.aao log=server.log ini=server.ini
Everything after "LAN" is not being executed by server.exe I can see in the servers log file that it is trying to open lan but it should be trying to open %m%.aao which means everything after "LAN" is being ignored.
How can I fix this?
Using
START "Test Server" server.exe LAN %M%.aao log=server.log ini=server.ini
will not work as I'm trying to run the batch file from WOTGreal. I am unsure why, but the way I fixed it for the other two files/programs I open was to use %~dp0, but the server requires that the spaces not be ignored.
I'm trying to run the batch file from WOTGreal
So the batchfile will be run from a different folder. That will also mean server.exe will be run from a different folder. so local filenames like in %M%.aao log=server.log ini=server.ini will be read from the wrong directory.
You could probably fix that by also using %~dp0 in all other paths. But it is probably easier to change the current directory at the start of the batchfile. To do that, add the following line to the start of the batchfile.
cd /d "%~dp0"
I have a TFS Build Definition.
In the work flow, I need to bring up a console listener, and run some tests on this listener.
So I create a BAT file with the followings:
start cmd /k "d:\abc.exe"
If I run this BAT file, the BAT itself will terminate, but it will spawn another cmd Windows, running the listener. So all is fine.
But when this is incorporated into TFS Build Definition, the work-flow would wait for the completion of this process, and the entire flow would hang.
I've tried with various switches for both START and CMD so that the work-flow can continue with the listener running, but to no avail.
start "" cmd /c "d:\abc.exe"
in cmd, /k means start a new instance, execute the indicated command and leave the window open. /c means is the same, but when command ends, cmd exits.