Windows start command not able to execute batch file - windows

I am executing start command from command prompt.
Command calls batch file named D:\My script.cmd and passes argument "Argument one". Here is command that I am tring to execute.
C:\Users\ABCUser>start "D:\My script.cmd" "Argument one"
but getting error mesasge as The system cannot find the file Argument one.
I don't understand why command is searching for file. Contents of file My script.cmd.
#echo off
cls
echo "Hello"
echo %1
Am I missing something or command syntax is wrong ? This command is not even working for file name without spaces.

It's the well known bug of start/cmd.exe handling a cmd and also an argument with quotes.
The cause is, that start uses cmd.exe /k to start the new task.
The help of cmd /k and cmd /c explains, that in this case the first and last quote are removed.
And additionally you used the start command wrong.
This should work, as the call works like a dummy to supress the quoting problem
start "Title" call "D:\My script.cmd" "Argument one"

You can use cmd.exe /c "D:\My script.cmd" arg1 arg2
If there is a problem you can switch the /c with /k which will leave the cmd open for you to examin the errors..
gl,
Refael

Related

How do I format the Target field of a Windows SendTo shortcut to quote the paths I'm passing to it?

OS: Windows 10
I have a Windows shortcut in my SendTo folder with the following Target:
C:\Windows\System32\cmd.exe /c "C:\Program Files\Path\To\Executable.exe"
This works just fine if I pass it a file or files without spaces in their filenames. But if I pass it files with spaces, I get the following error:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
(I changed it to /k to see the error. /c terminates right away, so I couldn't see the error message.)
If I launch it from inside a CMD session, I would do:
C:\Windows\System32\cmd.exe /c ""C:\Program Files\Path\To\Executable.exe" "path to/first" "path to/second""
But I'm not sure how to tell the Target field to do that. I tried:
C:\Windows\System32\cmd.exe /c ""C:\Program Files\Path\To\Executable.exe" "%1" "%2" "%3""
But that passed a literal %1 as the first argument, a literal %2 as the second argument, and then the file that I right-clicked and chose "Send To -> MyShortcut", prepended by a literal "%3 " as the third argument.
I need to be able to pass at most three arguments.
cmd.exe is stupid and will strip quotes if the command starts and ends with quotes.
Change the shortcuts command to C:\Windows\System32\cmd.exe /c if 1==1 "C:\Program Files\Path\To\Executable.exe"

Windows Registry Run Key

According to http://msdn.microsoft.com/en-au/library/aa376977(v=vs.85).aspx:
"Run and RunOnce registry keys cause programs to run each time that a user logs on. The data value for a key is a command line."
Should I then be able to add a key with:
Name: MyName
Data: START /MIN "Title" "cmd.exe" /c "#echo off && "C:\TestApplication.exe" -Arg1 "Arg2"
with the goal being to start my console application "C:\TestApplication.exe" minimized with arguments "-Arg1 "Arg2"" when Windows starts?
I ask because I cannot seem to get it working.
The documentation is misleading, though not strictly incorrect. The command line is passed directly to CreateProcess() rather than being passed to cmd.exe so commands internal to cmd.exe, such as start, are not valid. This means you need to add cmd /c at the start of the command line.
You're also missing a quote mark at the end, and you don't need to put quotes around cmd.exe. This works:
cmd /c START /MIN "Title" cmd /c "#echo off && "C:\TestApp.exe" -Arg1 "Arg2""
However, since the target application is an executable, not a batch file, you don't need the #echo command either:
cmd /c START /MIN "Title" cmd /c ""C:\TestApp.exe" -Arg1 "Arg2""
(Note that the command line passed to /c is never echoed.)

Batch Scripting: Command Not Running With If Statement

So for starters, I'm kind of a scrub coder and I'm enjoying playing around with batch scripting.
I don't like to have multiple command line windows open, so I'm trying to script everything into this 1 batch file, including the ability to run standard command line commands.
Here's the section of my code that I'm having issues with.
:three
echo.
echo ***************************
echo **Enhanced Command Prompt**
echo ***************************
:three.b
echo.
set /p Return=Enter Command:
if '%Return%'=='Return' goto home
cmd /c %Return%
goto three.b
NOTE: I've also removed cmd /c, and %Return% excutes with no issue, but for the sake personal preference, I've kept the cmd /c present.
The issue with this line of text, is executing a command such as "Ping Google.com" crashes the window. So does ipconfig /all, and anything else that doesn't seem to execute right away.
If I remove the "IF=Return", then I have no problems running ping google.com or anything else, but I'm ultimately trapped in this section of code without that IF statement.
The IF statement itself functions, but "Ping X" and other commands do not without crashing the batch file.
Any help would be appreciated.
Thanks!
You will need to use double quotes in if as this is the proper way of quoting/comparing strings in batch file.
:three
echo.
echo ***************************
echo **Enhanced Command Prompt**
echo ***************************
:three.b
echo.
set /p Return=Enter Command:
if "%Return%" == "Return" goto home
cmd /c %Return%
goto three.b

How to start a batch file minimized with task scheduler in Windows 8? - %comspec% method not working anymore after Windows 7

After Windows XP, I always use the trick below to start batch files minimized with Windows Task Manager.
From http://www.pcreview.co.uk/forums/running-bat-files-minimized-scheduler-t2125918.html:
"prequisite: all your batch files have an exit-command to finish the actions off. If you do not exit, you will end with a command prompt blinking.
This is what I keep using:
%comspec% /c start /min "C:\Scripts\Destination_inbound_ftp5.bat"
When you save this in the properties, you will get a follow-up dialogue asking you if you meant all this to be parameters or not. Answer NO and the task will be saved as you would expect.
I also read the Stack Overflow question “start %comspec% /c script.cmd” vs “start cmd /C second.cmd script.cmd”, which made me replace the "%comspec%" statement with "C:\Windows\system32\cmd.exe", but that did not change anything either.
The problem is that now, instead of a minimized running bat file, I end up with just a command prompt, minimized but without any of the batch commands executed. The task scheduler status remains "running" :(
How do I get this done on Windows 8 (64-bit)? Preferrable with old-school batch commands instead of PowerShell (or worse ;p)
The start command needs the leading "" quotes to disable the title feature. Try scheduling this:
%comspec% /c start "" /min "C:\Scripts\Destination_inbound_ftp5.bat" ^& exit
Assuming Windows 8 is the same as Windows 7, an "exit" is only going to exit the batch file (which it is going to do anyway).
You need to add the exit code like this:
Under "Program/Script":
CMD (or command.exe, or %comspec%)
Under "Arguments:
/c start "Title" /min "C:\Scripts\Destination_inbound_ftp5.bat" ^& exit
I didn't like seeing the command window pop up and then disappear so here is another solution from https://ss64.com/vb/run.html ...
First create invisible.vbs with this single line of text:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
Next and finally, launch your cmd or batch file via:
%SystemRoot%\system32\wscript.exe "invisible.vbs" "myscript.cmd" //nologo
Ta da! Scripting of this sort has been built into Windows for a long time. If you're curious, do a web search for "WSH" (windows scripting host). You can even write such scripts in dialect of JavaScript called JScript.
Another possibility: a small freeware program named CMDH, that simply runs the requested orders in background.
For example:
cmdh MyScript.cmd
No need to add "exit" to the script.
Tested working in Windows XP SP3, and there is no reason it should fail on Windows 8.
Here's a solution from https://ss64.com/vb/run.html that will run a batch file in a minimized window. Unlike the other solutions that use the start command with /min, this one will not flash a new window onto your screen or interrupt full-screen activities. It does, however, steal focus. I don't know how to avoid that.
First create a file named run_minimized.vbs with this single line of text:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 2, False
Next, create your Task Scheduler task with an action to start the program wscript.exe with these arguments:
"c:\path\run_minimized.vbs" "c:\path\my script.bat"
Change the paths as necessary to specify the locations of the two files.
There is no simple way to pass arguments from Task Scheduler to the batch file while also preserving spaces and quotation marks, because wscript strips quotation marks from its arguments. The simplest way to handle arguments with spaces would be to put the entire batch file command into the vbs:
CreateObject("Wscript.Shell").Run """c:\path\my script.bat"" ""arg 1"" arg2", 2, False
Note the use of quotation marks. There's one pair of quotation marks " enclosing the entire command string, and a pair of adjacent quote characters "" every place you'd use a normal quotation mark in a command line.
Maybe it's not the same as you mean but if I simply run a .bat file with the scheduler and tick this "Hidden" box on the General tab of the task properties it starts minified.
As already mentioned in foxidrive's answer, the issue is caused by the missing title parameter, which is taken from the first quoted argument string on the command line of the start command, which is the quoted batch file path in your command line. Since there is nothing left to be taken as the command/program to be started, the default cmd.exe is started.
Providing a dummy title (which may even be empty) solves that issue:
%ComSpec% /C start "" /MIN "C:\Scripts\Destination_inbound_ftp5.bat"
However, as mentioned in the help message of start (type start /? into a command prompt window), when the provided command/program is a batch file, the command processor cmd.exe is run with the /K switch, which keeps the command prompt window open:
command/program
If it 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 it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
To prevent that, let us explicitly specify the command processor with the /C switch:
%ComSpec% /C start "" /MIN %ComSpec% /C "C:\Scripts\Destination_inbound_ftp5.bat"
So there is no additional exit command necessary.

Pausing a batch file when double-clicked but not when run from a console window?

Is there a way for a batch file (in this case, running on Windows XP) to determine whether it was launched from a command line (i.e. inside a console window) or launched via the shell (e.g. by double-clicking)?
I have a script which I'd like to have pause at certain points when run via the shell, but not when run at a command line. I've seen a similar question on SO, but am unable to use the same solution for two reasons: first, whether or not it pauses needs to be dependent on multiple factors, only one of which is whether it was double-clicked. Second, I'll be distributing this script to others on my team and I can't realistically ask all of them to make registry changes which will affect all scripts.
Is this possible?
Found one :-) – After desperately thinking of what cmd might do when run interactively but not when launching a batch file directly ... I finally found one.
The pseudo-variable %cmdcmdline% contains the command line that was used to launch cmd. In case cmd was started normally this contains something akin to the following:
"C:\Windows\System32\cmd.exe"
However, when launching a batch file it looks like this:
cmd /c ""C:\Users\Me\test.cmd" "
Small demo:
#echo off
for %%x in (%cmdcmdline%) do if /i "%%~x"=="/c" set DOUBLECLICKED=1
if defined DOUBLECLICKED pause
This way of checking might not be the most robust, though, but /c should only be present as an argument if a batch file was launched directly.
Tested here on Windows 7 x64. It may or may not work, break, do something weird, eat children (might be a good thing) or bite you in the nose.
A consolidated answer, derived from much of the information found on this page (and some other stack overflow pages with similar questions). This one does not rely on detecting /c, but actually checks for the name of the script in the command line. As a result this solution will not pause if you double-clicked on another batch and then called this one; you had to double-click on this particular batch file.
:pauseIfDoubleClicked
setlocal enabledelayedexpansion
set testl=%cmdcmdline:"=%
set testr=!testl:%~nx0=!
if not "%testl%" == "%testr%" pause
The variable "testl" gets the full line of the cmd processor call, stripping out all of the pesky double quotes.
The variable "testr" takes "testl" and further strips outs the name of the current batch file name if present (which it will be if the batch file was invoked with a double-click).
The if statement sees if "testl" and "testr" are different. If yes, batch was double-clicked, so pause; if no, batch was typed in on command line (or called from another batch file), go on.
Edit: The same can be done in a single line:
echo %cmdcmdline% | findstr /i /c:"%~nx0" && set standalone=1
In plain English, this
pipes the value of %cmdcmdline% to findstr, which then searches for the current script name
%0 contains the current script name, of course only if shift has not been called beforehand
%~nx0 extracts file name and extension from %0
>NUL 2>&1 mutes findstr by redirecting any output to NUL
findstr sets a non-zero errorlevel if it can't find the substring in question
&& only executes if the preceding command returned without error
as a consequence, standalone will not be defined if the script was started from the command line
Later in the script we can do:
if defined standalone pause
One approach might be to create an autoexec.nt file in the root of c:\ that looks something like:
#set nested=%nested%Z
In your batch file, check if %nested% is "Z" - if it is "Z" then you've been double-clicked, so pause. If it's not "Z" - its going to be "ZZ" or "ZZZ" etc as CMD inherits the environment block of the parent process.
-Oisin
A little more information...
I start with a batch-file (test.cmd) that contains:
#echo %cmdcmdline%
If I double-click the "test.cmd" batch-file from within Windows Explorer, the display of echo %cmdcmdline% is:
cmd /c ""D:\Path\test.cmd" "
When executing the "test.cmd" batch-file from within a Command Prompt window, the display of
echo %cmdcmdline% depends on how the command window was started...
If I start "cmd.exe" by clicking the "Start-Orb" and "Command Prompt" or if I click "Start-Orb" and execute "cmd.exe" from the search/run box. Then I execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is:
"C:\Windows\system32\cmd.exe"
Also, for me, if I click "Command Prompt" from the desktop shortcut, then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is also:
"C:\Windows\system32\cmd.exe"
But, if I "Right-Click" inside a Windows Explorer window and select "Open Command Prompt Here", then execute the "test.cmd" batch-file, the display of echo %cmdcmdline% is:
"C:\Windows\System32\cmd.exe" /k ver
So, just be careful, if you start "cmd.exe" from a shortcut that contains a "/c" in the "Target" field (unlikely), then the test in the previous example will fail to test this case properly.

Resources