Search code examples
batch-filecmdwhitespace

How can use CD command in command prompt to change to a folder named only with a space?


I'm creating a Batch-file and I want go into a directory named only with whitespace e.g.:

"D:/Drive/ "

I tried with "",'',"/" and "%20" but nothing worked.

How can I do this?


Solution

  • I was able to create the directory with a fully-qualified path by mkdir "\\?\D:\ ". However that folder can't be accessed by either Explorer or cmd

    In Windows Explorer entering the folder always show the parent folder's content although it has actually entered the subfolder, therefore you can't do anything with that content, and Explorer's title bar becomes blank since it displays the folder name. In cmd cd stays on the parent folder unless you use the trick in SomethingDark's answer. You probably need to remove or rename it

    Sad to say I've yet to find a way to rename the folder. However it's easy to delete it with the same path

    rmdir "\\?\D:\ "
    

    A normal path will also work but you always need a slash at the end

    mkdir ".\ \"
    rmdir ".\ \"
    

    It's possible to store files in that folder

    D:\>>".\ \a.txt" echo abc
    
    D:\>type ".\ \a.txt"
    abc
    

    Once you have some content in the folder Explorer can now enter it normally. Unfortunately cmd still can't change into it. A fully-qualified path may help, unluckily cmd doesn't support it. However PowerShell does support it and can enter it like Explorer

    PS D:\> cat '.\ \a.txt'
    abc
    PS D:\> cd '.\ \'
    cd : An object at the specified path D:\  does not exist.
    At line:1 char:1
    + cd '.\ \'
    + ~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Set-Location], PSArgumentException
        + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetLocationCommand
    
    PS D:\> cd '\\?\D:\ \'
    PS Microsoft.PowerShell.Core\FileSystem::\\?\D:\ > dir
    
    
        Directory: \\?\D:\
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----        8/23/2018   3:39 PM              5 a.txt
    

    Sadly once you entered you can't use cd .. to go back and you must use fully-qualified path again

    PS Microsoft.PowerShell.Core\FileSystem::\\?\D:\ > cd ..
    cd : Cannot find path '\\?\D:' because it does not exist.
    At line:1 char:1
    + cd ..
    + ~~~~~
        + CategoryInfo          : ObjectNotFound: (\\?\D::String) [Set-Location], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
    
    PS Microsoft.PowerShell.Core\FileSystem::\\?\D:\ > echo `"$(pwd)`"
    "Microsoft.PowerShell.Core\FileSystem::\\?\D:\ "