START where both path and filename have spaces [duplicate] - batch-file

This question already has answers here:
How to create batch file in Windows using "start" with a path and command with spaces
(7 answers)
Closed 2 years ago.
START /high /d "./files" "Program Name.exe"
This doesn't work, it works if "Program Name" wasn't in quotation marks, but in this scenario i don't have control over the file name and where the batch file gets executed. How would i solve this using Batch?

Though it's not explicitly stated in the help text from start /?:
START ["title"] [/D path] ...
the first quoted option is treated as the title for the window. In your case, that's the (supposed) executable name.
You can see that if you create an x y.cmd file containing only the command #dir, then run:
start /d "\program files" "\path\to\x y.cmd"
The directory \path\to above is the directory where you created it, needed because you'll actually be in the program files directory when you try to run it.
What you'll see is not a directory listing (because the script isn't running), but a new cmd window titled \path\to\x y.cmd.
If you instead use:
start "xyzzy" /d "\program files" "\path\to\x y.cmd"
you'll find that it does run the script, giving you a directory in a window titled xyzzy.
In other words, if you want to quote your program name, you should provide an actual title so there's no confusion.

Related

"set path" command preventing "xcopy" command to work [duplicate]

This question already has answers here:
What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
(4 answers)
Closed 2 years ago.
When I need to extract a .RAR file using a BAT script I use the following command:
set path="path\to\installed\winrar\directoy"
WinRAR x "name of file.RAR"
In the next line if I try to give a xcopy command it says:
'xcopy' is not recognized as an internal or external command, operable program, or batch file.
Whereas if I run the xcopy command before issuing the set path command, it works without any issue.
How do I use the xcopy command AFTER giving the set path command?
To better explain this in more detail.
Windows has environment variables for both System and User. one of these variables is path.
Path is used as a default indexing variable which tells the system where to find files and executables. So for you setting path, your cmd prompt no longer knows where to find the executables, unless you supply the full path to the executable BUT it should still never be done.
To understand it better:
open cmd and type set path or echo %path% and see the result.
Then do set "path=some string" then do set path or echo %path% again and notice how it changed.
So get to know your variables by running set from cmd to list all default variables, and ensure you do not use any of them when creating variables in batch-files. It can become VERY dangerous.
So, to wrap it up, to fix your issue, change path to something unique like mypath:
set "mypath=path\to\installed\winrar\directoy"
"%mypath%\WinRAR.exe" x "name of file.RAR"

Error: "is not recognized as an internal or external command, operable program or batch file"

Whenever I try and run mycommand.exe from my Windows cmd.exe terminal, I get this error:
''mycommand.exe' is not recognized as an internal or external command, operable program or batch file'
Secondly
I've also experienced a similar error when I tried to run C:\Program Files\My-App\Mobile.exe
''C:\Program' is not recognized as an internal or external command, operable program or batch file'
This is a very common question seen on Stackoverflow.
The important part here is not the command displayed in the error, but what the actual error tells you instead.
a Quick breakdown on why this error is received.
cmd.exe Being a terminal window relies on input and system Environment variables, in order to perform what you request it to do. it does NOT know the location of everything and it also does not know when to distinguish between commands or executable names which are separated by whitespace like space and tab or commands with whitespace as switch variables.
How do I fix this:
When Actual Command/executable fails
First we make sure, is the executable actually installed? If yes, continue with the rest, if not, install it first.
If you have any executable which you are attempting to run from cmd.exe then you need to tell cmd.exe where this file is located. There are 2 ways of doing this.
specify the full path to the file.
"C:\My_Files\mycommand.exe"
Add the location of the file to your environment Variables.
Goto:
------> Control Panel-> System-> Advanced System Settings->Environment Variables
In the System Variables Window, locate path and select edit
Now simply add your path to the end of the string, seperated by a semicolon ; as:
;C:\My_Files\
Save the changes and exit. You need to make sure that ANY cmd.exe windows you had open are then closed and re-opened to allow it to re-import the environment variables.
Now you should be able to run mycommand.exe from any path, within cmd.exe as the environment is aware of the path to it.
When C:\Program or Similar fails
This is a very simple error. Each string after a white space is seen as a different command in cmd.exe terminal, you simply have to enclose the entire path in double quotes in order for cmd.exe to see it as a single string, and not separate commands.
So to execute C:\Program Files\My-App\Mobile.exe simply run as:
"C:\Program Files\My-App\Mobile.exe"
When you want to run an executable file from the Command prompt, (cmd.exe), or a batch file, it will:
Search the current working directory for the executable file.
Search all locations specified in the %PATH% environment variable for the executable file.
If the file isn't found in either of those options you will need to either:
Specify the location of your executable.
Change the working directory to that which holds the executable.
Add the location to %PATH% by apending it, (recommended only with extreme caution).
You can see which locations are specified in %PATH% from the Command prompt, Echo %Path%.
Because of your reported error we can assume that Mobile.exe is not in the current directory or in a location specified within the %Path% variable, so you need to use 1., 2. or 3..
Examples for 1.
C:\directory_path_without_spaces\My-App\Mobile.exe
or:
"C:\directory path with spaces\My-App\Mobile.exe"
Alternatively you may try:
Start C:\directory_path_without_spaces\My-App\Mobile.exe
or
Start "" "C:\directory path with spaces\My-App\Mobile.exe"
Where "" is an empty title, (you can optionally add a string between those doublequotes).
Examples for 2.
CD /D C:\directory_path_without_spaces\My-App
Mobile.exe
or
CD /D "C:\directory path with spaces\My-App"
Mobile.exe
You could also use the /D option with Start to change the working directory for the executable to be run by the start command
Start /D C:\directory_path_without_spaces\My-App Mobile.exe
or
Start "" /D "C:\directory path with spaces\My-App" Mobile.exe
when you have this problem
search for Environment variables
press on Environment variables
Under "System varibles" you search for PATH and press edit
press on new and add the path of your program and save it

quoting for cmd.exe with argument within argument

I've searched in many posts that already deal with this kind of issue but I'm still not able to make it work in my case.
I'm trying to create a link file to Launch a bat file with cmd.exe (because the bat file launch a service in spy/console mode so the console has to be shown).
The bat file is created before the install process of the service.
The link file is created in Windows Start Menu while installing process, so it can embed the path where the exe file of the service is installed.
As the root path will be linked to cmd.exe, I need to pass the path of the exe of the service as an argument of the bat file, itself being an argument of the cmd.exe file...
I've tried the syntax given as a solution in this threat :
correct quoting for cmd.exe for multiple arguments
Spaces in Program Path + parameters with spaces :
cmd /k ""c:\batch files\demo.cmd" "Parameter 1 with space" "Parameter2 with space""
So in my case, I've writen:
cmd /k ""C:\Program Files (x86)\Company\Licenses Server\Debug Mode.bat" "C:\Program Files (x86)\Company\Licenses Server\""
Here is the content of the "Debug Mode.bat" file :
#ECHO off
net stop company.service
START %~f1ServiceFile.exe /console spymode
net start company.service
#ECHO On
But when I launch the link file, I get the message:
Windows cannot find C:\Program...
There must be a "quote" problem but I've tried a lot of different syntaxes and I cannot figure out what goes wrong here...

Batch How to start a program

I want to create a batch file to launch my executable file after it has made some changes to itself.
My batch file is:
START /D "C:\Users\me\AppData\Roaming\Test\Test.exe"
When I run it though I just get a brief console flash and Test.exe doesn't start up.
I've verified the EXE is there in the directory.
I've launched the exe manually to verify it is working as well.
My batch file resides in
C:\Users\admin\AppData\Roaming\run.bat"
There are two issues:
The /D option solely defines the starting or working directory, but not the program to execute.
The start command considers the first quoted argument as the title of the new window. To avoid confusion with other arguments, always provide a window title (that may also be empty).
There are two solutions, which are actually not exactly equivalent:
Remove the /D option, so the current working directory is used:
start "" "C:\Users\me\AppData\Roaming\Test\Test.exe"
Keep the /D option and explicitly provide the new working directory to be used:
start "" /D "C:\Users\me\AppData\Roaming\Test" "Test.exe"
try changing to this
start /d "C:\Users\me\AppData\Roaming\Test" Test.exe
You will see the console flash and your program should startup.
Update
Thanks for #SomethingDark 's suggestion to use the following code.
start "" C:\Users\me\AppData\Roaming\Test\Test.exe
However, the above code will not work if your filename contains space.
Try with the following command.Add it to your batch script.Notice that you have to add double quotes after start keyword if there is/are whitespaces in the path string.
start "" "C:\Users\me\AppData\Roaming\Test\Test.exe"
Enclose any directory names which are longer than one-word into quotation marks. So the following path:
start C:\Program Files\MySQL\MySQL Workbench 8.0 CE\MySQL.exe
Should become something like this:
start C:\"Program Files"\MySQL\"MySQL Workbench 8.0 CE"\MySQL.exe

Batch file which executes an exe from a specific path

I am trying to create a batch file which runs an exe from a specific path.
Eg: I have my exe in E drive. Exact path is E:\kk.exe. I want to run this kk.exe from D:\bin folder.
I use the following command in my batch file:
start "D:\bin" "E:\kk.exe"
So far no luck. Any help would be appreciated.
start "" /d "d:\bin" "e:\kk.exe"
start command has a peculiar behaviour: the first quoted argument is the title of the window. That is the reason for the initial "" (you can include the title you want). The rest of the line is the starting folder (/d, what will be the current active folder for the started process) and the command to execute.
cd /d "D:\bin"
start "window name" "E:\kk.exe"
If I've decoded your meaning correctly, you wish to run kk.exe while your current directory is d:\bin. This will create an independent process to run that program.
Note: the syntax of "start" is such that it's advisable to assign a window title (the first quoted parameter) - if you don't ant a title, leave the text out and use en empty quoted string.
However, if you just want to execute e:\kk.exe then
cd /d "D:\bin"
"E:\kk.exe"

Resources