Thread: cmd.exe and filepath with spaces

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    12

    cmd.exe and filepath with spaces

    Hello everyone,

    I'm writing a C program which sends a command to cmd.exe.

    I have an executable I need to run and send parameters too. I am trying to make the program more portable, so I am trying to allow spaces to be in the filepath where the executable is located.

    I have successfully solved that issue by enclosing the filepath in quotes. Below is an example of the command I send:

    cmd.exe /c "c:\File path with spaces\executable" -k unlock

    This works great but now an issue arises when I have to send a parameter to this executable which includes empty quotation marks as shown here:

    cmd.exe /c "c:\File path with spaces\executable" -k unlock ""

    This does not work, yet it works when I remove the quotations around the file path, but then the only issue is that there can't be spaces in the file path:

    cmd.exe /c c:\Filepathwithoutspaces\executable -k unlock ""

    Does anyone know why this happens? How can I resolve this?

    Thanks!

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    This would be better posted to the Windows programming forum on this site, as it is specific to the Windows O/S.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cmd.exe quoting rules - Google Search
    Basically, it's a mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filepath help please,
    By Shadow20 in forum C Programming
    Replies: 3
    Last Post: 04-03-2011, 10:31 AM
  2. open(filePath, O_RDONLY) not blocking?
    By lsad in forum Linux Programming
    Replies: 7
    Last Post: 01-20-2011, 11:42 AM
  3. How to Declare Character array with filepath?
    By cool_guy in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2010, 08:53 AM
  4. Getting filepath from string
    By SterlingM in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2009, 12:57 PM
  5. Separating filename from filepath
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2003, 12:37 PM

Tags for this Thread