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?

Upvotes: 1

Views: 2416

Answers (3)

phuclv
phuclv

Reputation: 41794

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:\ "

Upvotes: 3

Tibix
Tibix

Reputation: 390

Uh well this is a kind of special case since Windows does not allow folder and file names to begin with a space.

As Paxz mentioned in his comment, I also was not able to reproduce this so I'm just going to assume that you used a no-break space?

In that case you would use this syntax: cd " " but hold up.

You can't just press space here, you will need to hold down Alt and then press 0160 while holding Alt down. Also don't forget that you need to use the numbers from the Num Block on the right of your keyboard.

Upvotes: 1

SomethingDark
SomethingDark

Reputation: 14305

To enter a directory called , you need to use cd " \ \". I have no idea why you need to do it twice.

enter image description here

Note that under ordinary circumstances, this would never be possible. However, if the folder was created on Linux and then copied over, Windows would do its best to make it work.

Upvotes: 1

Related Questions