Problem with Path names containing spaces ?

  • Hi,

    I have a program (my own) that uses the windows ShellExecute function with the “open” option like:

    ShellExecute( Dlg, “open”, szEditor, szFile, NULL, SW_SHOWNORMAL );

    This works fine for other editors, but not for Notepad++.

    szEditor is the path to Notepad++ (C:\Program Files (x86)\Notepad++\notepad++.exe)

    szFile is the path (with spaces) (C:\Users<username>\Documents\My Files\Doc1.txt)

    First I get a dialog titled 'Create new file" saying the file does not exist, with the filename truncated like:


    If I answer No then I get the “Cannot open file” dialog with:

    “C:\Users<username>\Documents\My Files\Files\Doc1.txt” cannot be opened:
    Folder “C:\Users<username>\Documents\My Files\Files” doesn’t exist.

    I can open the file directly in Notepad++ and it opens if I right-click in Explorer and select open in Notepad++

  • I have the same issue. I found out that this happens when the path to the file is too long.

    It works fine if the path is not longer than 128 characters.

  • I have found a work-around using " around the filename that is passed to Shellexecute.

  • @Stewart-Wilkinson It isn’t a work around; it is a requirement.

    Try typing this at the command prompt:

    dir \Program Files

    then try:

    dir "Program Files"

  • As I said in my first post, you don’t need " added to szFile when using other editors.

    and you don’t need them adding to szEditor either, which still seems a little odd to me.

    The work-around I used in my code is:

    char szArgs[512];
    sprintf( szArgs, “”%s"", szFile);
    ShellExecute( g_hDlg, “open”, szEditor, szArgs, NULL, SW_SHOWNORMAL );

  • @Stewart-Wilkinson

    FYI, if you want to include code in a post, start each line with 4 spaces. That will make it look like:


    You are saying that using ShellExecute() you can open a file that has spaces in its path but no embedded quotation marks if szEditor is, say, notepad.exe, but not if it is Notepad++:

    szNotepad         = "C:\\Windows\\notepad.exe";
    szNotepadPlusPlus = "C:\\notepad++\\notepad++.exe";
    szFileNone        =   "C:\\Users\\Stewart\\Documents\\My Files\\Doc1.txt";
    szFileQuotes      = "\"C:\\Users\\Stewart\\Documents\\My Files\\Doc1.txt\"";
    /// The next line works?
    ShellExecute( g_hDlg, "open", szNotepad,         szFileNone, NULL, SW_SHOWNORMAL );
    /// But this one does not?
    ShellExecute( g_hDlg, "open", szNotepadPlusPlus, szFileNone, NULL, SW_SHOWNORMAL );
    /// But this one does?
    ShellExecute( g_hDlg, "open", szNotepadPlusPlus, szFileQuotes, NULL, SW_SHOWNORMAL );

    Is that what you are seeing? If so, it sure doesn’t sound right to me. I suppose the ShellExecute implementation could take exceptional action based on the name of the program being launched (e.g. notepad.exe takes only a single filename as a parameter, so treat the parameter as a single filename even if it contains spaces), but that sounds a bit far fetched.

    Also you are saying that embedded spaces in the name of the program to be executed (szEditor) are fine, even if there are no quotes embedded on either end? That I could understand, I suppose, since there can only be one “program”, so it would make sense for Windows to treat szEditor as if it had quotes embedded on either end even if they weren’t present.

  • I have the same problem when I right click on a text file that is within a path that has spaces in the name. In my case it is a file within my google drive.

    Notepad++ opens and then reports…
    “D:\Users\Stewart\Google” doesn’t exist. Create it?

    The actual path is:-
    D:\Users\Stewart\Google Drive\Projects\PRP31\settings.json

  • @Stewart-Clark

    there’s probably an old open command, with missing double-quotes for notepad++ paths, inside your registry.
    (a left-over from an older notepad++ installation, which i remember had this issue)

    if you dare to have a look inside your registry:

    • open regedit.exe and search for notepad++.exe
      (hit the windows icon at the bottom left and type regedit)

    • look for all instances where notepad++.exe is at a shell\open\command structure.
      for example: HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command

    • the correct (default) value should be something like
      "C:\Program Files (x86)\Notepad++\notepad++.exe" "%1"
      where the path might be different on your system, but the important thing is, that your path to the exe as well as "%1" is always between " double-quotes
      (not like C:\Program Files (x86)\Notepad++\notepad++.exe %1 without quotes)

    hope this helps.
    please report back, if you need further assistance.

  • Thanks Meta Chuh - that solved it!!

Log in to reply