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

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"

Related

cmd /c and the & symbol in file path

I ran into a problem, where I have a batch file located in a directory that has the and symbol in it.
When I try to run the cmd /c on the batch file the and symbol messes up the cmd program even though the path is double quoted.
C:\>cmd /c "C:\This & That\batch.bat"
'C:\This' is not recognized as an internal or external command,
operable program or batch file.
The system cannot find the path specified.
I'm not sure what to do. I tried escaping the & , using ^& but that doesn't seem to work as I'm all-ready in double quotes.
If I run the same command line from a directory that does not have an & symbol it works fine.
C:\>cmd /c "C:\This and That\batch.bat"
C:\>echo "Hi There"
"Hi There"
Thanks for any help.
so this would be the best way.
You also need to escape the & with ^ and run double double quotes.
cmd /c ""C:\This ^& That\batch.bat""
or you can set a location variable and use && to run the set and cmd /c after each other.
set "location=C:\This ^& That" && cmd /c ""%location%\batch.bat""

startup batch file hangs up on second command

I created a startup bat file that looks like this
taskkill /im RemoteDesktopManager.exe
C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
"C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
"C:\Program Files\Mozilla Firefox\firefox.exe"
it does the first and second, but won't go any further, they all exist
how can I fix this?
update : I tried suggestion provided by #phd443322 and wrote this:
taskkill /im RemoteDesktopManager.exe
start "" /w C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
start "" /w "C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
start "" /w "C:\Program Files\Mozilla Firefox\firefox.exe"
intrestingly each command still waits for that program to be closed to continue to the next.
so why still not working?
Below there is a working Batch file, as first advised by phd443322:
taskkill /im RemoteDesktopManager.exe
start "" C:\Users\kheradmand\AppData\Local\Google\Chrome\Application\chrome.exe
start "" "C:\Program Files (x86)\JetBrains\PhpStorm 7.1.2\bin\PhpStorm.exe"
start "" "C:\Program Files\Mozilla Firefox\firefox.exe"
Batch files wait for programs to exit unlike interactive. These are the rules documented in the Start command.
If Command Extensions are enabled, external command invocation
through the command line or the START command changes as follows:
non-executable files may be invoked through their file association just
by typing the name of the file as a command. (e.g. WORD.DOC would
launch the application associated with the .DOC file extension).
See the ASSOC and FTYPE commands for how to create these
associations from within a command script.
When executing an application that is a 32-bit GUI application, CMD.EXE
does not wait for the application to terminate before returning to
the command prompt. This new behavior does NOT occur if executing
within a command script.
When executing a command line whose first token is the string "CMD "
without an extension or path qualifier, then "CMD" is replaced with
the value of the COMSPEC variable. This prevents picking up CMD.EXE
from the current directory.
When executing a command line whose first token does NOT contain an
extension, then CMD.EXE uses the value of the PATHEXT
environment variable to determine which extensions to look for
and in what order. The default value for the PATHEXT variable
is:
.COM;.EXE;.BAT;.CMD
Notice the syntax is the same as the PATH variable, with
semicolons separating the different elements.
When searching for an executable, if there is no match on any extension,
then looks to see if the name matches a directory name. If it does, the
START command launches the Explorer on that path. If done from the
command line, it is the equivalent to doing a CD /D to that path.

Windows start command not able to execute batch file

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

Correct quoting for cmd.exe for multiple arguments

I want to call
cmd /c "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com" mysolution.sln /build "release|win32"
Unfortunately this does not work, because I get the error:
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
As I understand, I need quoting for the spaces and quotes for the |, but I am only allowed to use the quotes once.
Any ideas how to quote this command line call correctly?
Note the "" at the beginning and at the end!
Run a program and pass a Long Filename
cmd /c write.exe "c:\sample documents\sample.txt"
Spaces in Program Path
cmd /c ""c:\Program Files\Microsoft Office\Office\Winword.exe""
Spaces in Program Path + parameters
cmd /c ""c:\Program Files\demo.cmd"" Parameter1 Param2
Spaces in Program Path + parameters with spaces
cmd /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
Launch Demo1 and then Launch Demo2
cmd /c ""c:\Program Files\demo1.cmd" & "c:\Program Files\demo2.cmd""
CMD.exe (Command Shell)
Spaces are used for separating Arguments. In your case C:\Program becomes argument. If your file path contains spaces then add Double quotation marks. Then cmd will recognize it as single argument.
Spaces are horrible in filenames or directory names.
The correct syntax for this is to include every directory name that includes spaces, in double quotes
cmd /c C:\"Program Files"\"Microsoft Visual Studio 9.0"\Common7\IDE\devenv.com mysolution.sln /build "release|win32"

open command prompt window and change current working directory

I'm terribly new to scripting on windows. Using windows 7 64.
I'm trying to make a .bat file that I can double click, and have it open a command prompt and automatically cd me to a certain directory.
I tried making a .bat file with
#ECHO OFF
cmd "cd C:\my\destination"
Which opens what looks like a command prompt, but doesn't seem to let me type any commands.
I then tried:
#ECHO OFF
start cmd "cd C:\my\destination"
But this just sent me into a loop opening tons and tons of prompts until my computer crashed :) The .bat file was located in the destination directory if that matters.
This works for me:
#ECHO OFF
cmd.exe /K "cd C:\my\destination && C:"
The quoted string is actually two commands (separated by a double ampersand): The first command is to change to the specified directory, the second command is to change to the specified drive letter.
Put this in a batch (.BAT) file and when you execute it you should see a Command Prompt window at the specified directory.
Use the /K switch:
#ECHO OFF
start cmd.exe /K "cd C:\my\destination"
But IMHO, the most useful switch is /?.
Starts a new instance of the Windows XP command interpreter
CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
[[/S] [/C | /K] string]
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
/S Modifies the treatment of string after /C or /K (see below)
/Q Turns echo off
...
And only if it does not work, then Google it, as #Neeraj suggested :D
This could be done like that:
#ECHO OFF
cd /D "C:\my\destination"
cmd.exe
If you need to execute a file or command after you open the cmd you can just replace the last line with:
cmd.exe /k myCommand
#ECHO OFF
%comspec% /K "cd /D d:\somefolder"
The /D will change folder and drive and works on 2000+ (Not sure about NT4)
If you take a look at Vista's open command here, it uses cmd.exe /s /k pushd \"%V\" but I don't think %V is documented. Using pushd is a good idea if your path is UNC (\\server\share\folder) To get UNC current directory working, you might have to set the DisableUNCCheck registry entry...
Why so complicated? Just create an alias to cmd.exe, right click on the alias and navigate to its settings. Change the "execute in" to the path you want to have as standard path. It will always start in this path.
just open a text editor and type
start cmd.exe
cd C:\desired path
Then save it as a .bat file. Works for me.
You can create a batch file "go-to-folder.bat" with the following statements:
rem changes the current directory
cd "C:\my\destination"
rem changes the drive if necessary
c:
rem runs CMD
cmd

Resources