-1

Ex:

cmd /C start C:\Users\Bob Builder\Desktop\New Folder\test.exe

I'm trying to use cmd to start a file but since there are spaces in the path, cmd is throwing an error after Bob.

Error:

"Windows cannot find C:\Users\Bob. Make sure you typed the name correctly, then try again."

The system cannot find the file C:\Users\Bob.

Its simply failing to accept the spaces. It's driving me crazy because I'm spoiled with C# working out of the box. I don't know much about this, I have been spending way too much time trying to figure this out. Some help would be greatly appreciated.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 4
    how is python, C# and C++ related here? Don't do tag spam – phuclv Oct 17 '21 at 02:59
  • It's being read as separate tokens. Use quotes. You also don't have to call on cmd.exe. just run it from PowerShell. – Abraham Zinala Oct 17 '21 at 02:59
  • 2
    Where are you executing the command? From a `cmd` prompt? From a `powershell` prompt? Somewhere else? You just edited in the [tag:powershell] tag even though there's no indication in the question. – briantist Oct 17 '21 at 03:19
  • What is your definition of "working out of the box"? c# requires quotes around strings/paths. PowerShell is even more forgiving when it come to which type of quotes you use. – Daniel Oct 17 '21 at 03:22
  • My definition is string apppath = @"mydamnnormalpathherewithnobullshit" – SharpFarter Oct 17 '21 at 03:26
  • For some reason I'm starting to think that its your "test.exe" app that might be where the actual problem is at. Does it load okay if you run it directly? Is it console or GUI? – Daniel Oct 17 '21 at 03:28
  • its a console app that is basically automating some file management for me. It's not the problem because if there is no spaces in it, if i throw my test.exe in programdata directory it runs just fine. Its the spaces in the directory. – SharpFarter Oct 17 '21 at 03:34

3 Answers3

3
  • In order for a path that contains spaces to be recognized as a single path (argument), it must be quoted.

  • In order for an executable to execute in the current console window, synchronously, with its streams connected to the calling shell, it must be invoked directly, not via start.

Direct invocation from cmd.exe (only "..." quoting supported):

"C:\Users\Bob Builder\Desktop\New Folder\test.exe"

From PowerShell:

& 'C:\Users\Bob Builder\Desktop\New Folder\test.exe'

Note:

  • PowerShell also supports '...' strings (single-quoted), which are verbatim strings that are preferable to "..." (double-quoted) ones if you do not require expansion of variables (string interpolation) - see the conceptual about_Quoting_Rules help topic.

  • For syntactic reasons, PowerShell requires the use of &, the call operator to invoke commands that are quoted and/or contain variable references - see this answer for details.


By contrast, use start in cmd.exe / Start-Process in PowerShell (whose built-in alias is also start) to launch an executable in a new window (on Windows), asynchronously, with no (direct) ability to capture the launched executable's output:

From cmd.exe:

start "title" "C:\Users\Bob Builder\Desktop\New Folder\test.exe"

Note:

  • Specifying "title" - i.e. a self-chosen (console) window title - is required for syntactic reasons in this case: without it, the double-quoted path itself would be interpreted as the window title, and the - implied - executable to launch would be another cmd.exe instance.

  • Note that if you launch a GUI application this way, the title argument is irrelevant, because no new console window is created.
    Conversely, if you launch a console application specified by double-quoted path and therefore must use a title argument, note that "" will result in the new window having no title.

From PowerShell (parameter -FilePath is positionally implied):

Start-Process 'C:\Users\Bob Builder\Desktop\New Folder\test.exe'

Note:

  • Start-Process does not support specifying a window title, so you may want to call cmd.exe's internal start command for that (or other features not supported by Start-Process, such as specifying the process priority).

  • To work around quoting problems, invoke cmd.exe's start from PowerShell by passing the entire start command as a single string to cmd /c:

    cmd /c 'start "title" "C:\Users\Bob Builder\Desktop\New Folder\test.exe"'
    
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

cmd /C start "C:\Users\Bob Builder\Desktop\New Folder\test.exe"

Quotes are your friend. Sometimes even double quotes are too!

Collin Bell
  • 334
  • 2
  • 10
  • That is loading up the program inside a cmd window the file is not opening but a cmd with its icon opened. How do i get the exe to execute normally? – SharpFarter Oct 17 '21 at 02:58
  • Try it without start - `cmd /C "C:\Users\Bob Builder\Desktop\New Folder\test.exe"` – Daniel Oct 17 '21 at 03:41
  • 2
    While the need for quotes is a good pointer, your `start` command is broken - the double-quoted path is interpreted as a _window title_, and the program that is launched is `cmd.exe` by default. I know you just built on the OP's own attempt, but it's also worth pointing out that you need the `cmd /c` neither from `cmd.exe` nor from PowerShell. Though in PowerShell, where `start` is an alias for `Start-Process`, `start "C:\Users\Bob Builder\Desktop\New Folder\test.exe"` would work as-is. All of this assumes that the intent is to launch in a _new window_, _asynchronously_. – mklement0 Oct 17 '21 at 15:49
-2

Seems like cmd won't work for me. Powershell worked with this script:

$env:Path += ";C:\Users\Bob Builder\Desktop\New Folder\"
test.exe
  • 2
    From `cmd.exe`, use `"C:\Users\Bob Builder\Desktop\New Folder\test.exe"`. While your PowerShell workaround is effective, you don't need a workaround; use `& 'C:\Users\Bob Builder\Desktop\New Folder\test.exe'` – mklement0 Oct 17 '21 at 15:44