How to Launch Command in Batch File With Space

Trouble running a batch file with a space in the path and space in the argument

Try changing the startup directory with the /d argument to start like so:

cmd /c start "" /d"C:\Test Batch\" "TestBatch.bat" "C:\Users\User Name\Desktop\Company Downloads\downloadedDoc.doc"

The start command has some oddities parsing quotes.

Opening file with spaces in Windows via Command Prompt

Just leave off start, and surround the full filename (including any path) with double-quotes. This works fine on my system:

C:\>"test space.avi"

Unable to launch command in batch file with space

When your path contains spaces, you have to wrap it in quotation marks. But, you only have to wrap the path itself, not the parameters sent (otherwise Windows will treat the parameters as part of the path.)

start "test" "D:\ab cd\jre\bin\javaw" -jar path_to_my_jar

How to Open Files in Directories with Spaces using CMD as Admin?

%0 should already be doublequoted, but is the drive path name and extension of the running batch-file. You really want to use this:

"%~dp0…\program.exe"

represents an actual directory sequence, not a relative path

To get a better idea of what is going on open a Command Prompt window, enter Call /? and read its output.

Note: the p in %~dp0 already has a trailing backslash, so you don't need to append one yourself.

Batch: How to store command output with spaces in variable?

I would first advise you not to pass an filename without an extension. Doing so relies upon each of the extensions listed under %PATHEXT%, and you could have files named python.com, python.bat, python.cmd, python.vbs, python.vbe, python.js, python.jse, python.wsf, python.wsh, and python.msc, as well as python.exe. I'm sure you wouldn't want to unknowingly create firewall rules for all of those.

I would also advise that you do not use the where command for this. The reason being that it could return more than a single item. For example, try this, where notepad.exe and if memory serves, you'll probably see, C:\WINDOWS\System32\notepad.exe, followed by C:\WINDOWS\notepad.exe. That is because where.exe searches each location in %PATH% in order of their listing, and returns each, as the first two entries under %PATH% should by default be C:\WINDOWS\system32;C:\WINDOWS;.

My suggestion therefore would be to use another method, which returns the first item found under %PATH% as opposed to all, and this should, if you've used the variable properly, return the default, (most used) item. The method is the %~$PATH variable expansion, and is documented under the help information for the for command, i.e. entering for /? at the Command Prompt.

Example Script:

@Echo Off
SetLocal EnableExtensions
Rem ~ Open ports. Run with elevated administrator rights.
"%__AppDir__%reg.exe" Query HKU\S-1-5-19 1>NUL 2>&1 || (
Echo This script must be run elevated.
Echo Please use 'Run as administrator'
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1>NUL
GoTo :EOF)
Call :OpenPorts "python.exe"
Pause
GoTo :EOF

:OpenPorts
For %%G In ("%~1") Do For %%H In (TCP UDP) Do (
"%__AppDir__%netsh.exe" AdvFirewall Firewall Add Rule^
Name="%~n1" Description="%~1" Dir=in Action=allow^
Edge=deferuser Profile=private Program="%%~$Path:G"^
Protocol=%%H)
Exit /B

I have removed all of the unnecessary Call commands, (using just the one), and deliberately used carets to split up your super long line, for readability; (which also allows you to include the localport= and/or remoteport= option, which I'd assume by your 'Open Ports' name you're going to want to include as a modification to the above later). I also took the liberty of including some code, to determine if the script was being run elevated, and display a message before closing, if it isn't.

Problem with Space in Variable on Batch-Script

The solution is to quote the filename.

del "%orginaldatei%"

That's because del accepts a list of files to delete, delimited by spaces.

Quotes help to make clear, if a space is a delimiter or part of a filename.

del my file     --- Tries to delete "my" and "file"
del "my file" --- Tries to delete "my file"


Related Topics



Leave a reply



Submit