Run a batch file silentily in NSIS - cmd

I have a batch file that needs to vbe excecuted, I am currently executing it using
Exec "$PLUGINSDIR\IISHelper.bat"
But while executing a black window is appearing which is not what is desired. I would like to run that batch file silently.
So I tried
Exec "start $PLUGINSDIR\IISHelper.bat /B"
But while this runs silently this doesn't have the elevated permission as it does for the installer.

The start would not be silent if it worked but it does not because start is not a program, it is a internal command in cmd.exe.
Use the nsExec plug-in (part of the default NSIS installation) to hide the console window.
nsExec::Exec '"$sysdir\cmd.exe" /c if 1==1 "$PLUGINSDIR\IISHelper.bat"'
Pop $0

Related

CMD does not return to prompt when calling program at time of cmd call

I want to have a desktop shortcut/icon for a cmd prompt which runs a command when it is opened, but then returns to the prompt
I have something like this
C:\Windows\System32\cmd.exe /k "E:\python-gui.pyw"
When I run this the GUI launches as expected, but the command prompt does not return until the GUI closes.
If instead I open a
cmd
and then run the command
E:\python-gui.pyw
then the command prompt is available even while the gui remains on the screen. i.e. I can execute further commands.
Is there a way I can get this behavior from the first call?
As #KJ writes in the comments, a program which would lock the command window can be launched instead with the run command. Launching with the run command launches the program in its own command window, which prevents the original command window from waiting for the program to complete.
waits for program to exit before returning control:
C:\Windows\System32\cmd.exe /k "E:\my_script.script"
does NOT wait for program to exit before returning control:
C:\Windows\System32\cmd.exe /k start "E:\my_script.script"
Create a new file for example I will name it a.bat
put this in it
E:\python-gui.pyw
Go to the original file and add this command
start a.bat

Continue With CMD Commands After Batch Script

I am trying to write a batch script which, once complete, would allow the user to continue using the Windows command prompt as they normally would had no script been run. Is this possible? Thank you in advance for any help.
If you manually open CMD (the Command Prompt) and invoke the batch file by name, CMD will remain open for additional commands after the batch file completes. You cannot do this by double-clicking on the batch file, but if you create a shortcut to the batch file that runs CMD.EXE with the /K switch, you will run the batch file and then leave CMD running for additional commands. See CMD at SS64.

How start a program via command line after the previous process terminated itself?

I run a program like notepad.exe via command line.
After the process terminated itself after completion of task of the started application, I need to run another program via command line, for example winword.exe.
So I would like a behavior like a scheduler, waiting in background up to completion of first process and then initiate start of another process.
Can I achieve this in Windows? And if yes, how?
Batch file solutions
For the example Notepad.exe and Winword.exe the solution is quite simple with following batch file:
#echo off
%SystemRoot%\Notepad.exe
start Winword.exe
Windows command interpreter cmd.exe first starts Windows Notepad and halts execution of the batch file until Notepad.exe terminated itself which means the user
pressed Alt+F4 on keyboard, or
clicked on X symbol on right side of title bar of Notepad window, or
clicked in menu File on menu item Exit, or
double clicked on application symbol on left side of title bar of Notepad window, or
clicked once on application symbol on left side of title bar of Notepad window and clicked next on last application context menu item Close.
Then internal command START of cmd.exe is used to start Microsoft Word in a separate process parallel to running command process. For that reason cmd.exe continues the batch file processing immediately after executing START while Microsoft Word is running parallel and exits because there is no more command line.
The application to start must be specified usually with full path if the directory containing the executable is not included in environment variable PATH, enclosed in double quotes if the path contains a space or one of these characters &()[]{}^=;!'+,`~. Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? for details on how Windows command interpreter finds executables and scripts specified on command line or in a batch file without path (and without file extension). So best would be as second line something like:
start "" "%ProgramFiles(x86)%\Microsoft Office\Office14\Winword.exe"
This command line starts 32-bit Microsoft Word 2010 installed in standard installation directory on a computer running 64-bit Windows. The additional empty argument string "" is necessary because command START would interpret otherwise the full qualified name of application to start enclosed in double quotes as title for a new console window. So the command START would start a new command process with the title C:\Program Files (x86)\Microsoft Office\Office14\Winword.exe for the console window without specifying explicitly an empty title with "" as first argument.
Why is using just start Winword.exe working?
The directory containing Winword.exe is not included in environment variable PATH. But as long as Winword.exe is installed at all, this command line nevertheless results in starting Microsoft Word. The reason is correct registration of application Winword.exe in Windows registry during installation. For that reason the command START is capable to find out where Winword.exe is installed and execute it. For details on how this works see answer on Where is “START” searching for executables?
The three lines in batch file can be also optimized to a single line with multiple commands:
#%SystemRoot%\Notepad.exe & start Winword.exe
But this single command line can't be used directly in a command prompt window because of cmd.exe executes in this case Windows Notepad and Microsoft Word parallel.
Command line solutions
The command line solution for usage directly from within a command prompt window is:
start /wait Notepad.exe & start Winword.exe
This starts Windows Notepad in a separate process using command START with explicitly waiting for termination of Notepad.exe because of using additionally the START parameter /wait before one more START is executed to start Microsoft Word. There is no need of "" as empty title string here because of no argument string in this command line is enclosed in double quotes.
But this command line solution has one disadvantage: the command prompt window can't be used further as long as Windows Notepad is running.
So better would be starting from within current command prompt window a new command process with minimized window which first executes Windows Notepad, halts command line execution until Notepad terminates itself, then starts Microsoft Word and exits immediately after starting Winword.exe. This can be done with following command line:
start "Notepad & Winword" /min cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
This command line results in starting cmd.exe as separate process, with a console window being minimized because of option /min of command START, with a console window title Notepad & Winword, which closes itself because of option /C of CMD, after first starting Notepad.exe and waiting for Notepad termination before starting Winword.exe not waiting for termination.
Well, the additional, minimized console window with title Notepad & Winword is in real of no usage for the user. Therefore better would be the command line:
start "" /B cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
The additional command process is started in this case with no window (in background) because of usage of option /B of command START. An empty title is specified here as no console window is shown at all.
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.
cmd /?
echo /?
start /?

Run command on Command Prompt start

Is there a way to run any command (eg: cd D:\test) on Command Prompt start and to have this instance of Command Prompt as a different shortcut/instance.
You can create a shortcut that has a target of e.g.:
%comspec% /k ""C:\Full_Path_to\VsDevCmd.bat""
Where the .bat file is the command(s) that you want to run on startup. This will then leave you with an open command prompt after those commands have executed.
(In case it's not obvious, this is just shamelessly ripped from one of the Developer Command Prompt shortcuts that Visual Studio installs in the Start Menu)
%comspec% is a nice way of getting cmd to execute.
Cmd:
/k : Carries out the command specified by string and continues.

cmd.exe doesn't terminate when using a .bat file

[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

Resources