5

I'm using Windows 7 and need, for reasons outside my control, to create a directory ending in a single space. It will contain various files and subdirectories.

I have attempted md and rename, also with UNC pathes, but it failed, either giving 'incorrect syntax' or, more often, simply ignoring the space.

How can I do it?

barlop
  • 23,849
mafu
  • 2,865
  • I notice two votes to close as 'off topic'. I'm rather dumbfounded how "this question does not appear to be about computer software or computer hardware". I thought it lies in a core topic of this site. – mafu May 17 '15 at 12:23
  • You mean to say you have never seen good questions that people have voted to close or have been closed for absurd reasons that sometimes don't even apply? It happens often, so you just have to adapt the wording of the question accordingly. . Rather than arguing it, find out the way around it to make the question idiot proof.. e.g. go to chat and ask advice on how to ask the question to avoid that happening. – barlop May 17 '15 at 19:47
  • I'm used to other sites in the SE network, so yeah, usually the votes I've seen are quite reasonable. Probably SU is a bit special. I've already gotten the good answer I was looking for, so I'm happy regardless :) – mafu May 18 '15 at 00:22
  • I describe the creation of directory names with trailing spaces without any third party tools as part of this answer. It also covers renaming and deletion of such directories, either in command prompt or in PowerShell. – stackprotector May 11 '20 at 14:59
  • 1
    Thanks for the hint @Thomas. Gave you an updoot – mafu May 11 '20 at 15:25

3 Answers3

9

To create a directory in Windows with a trailing space character, bring up a command prompt and enter the following:

md "\\?\C:\mydirectory "

The directory must be an absolute (not relative) directory and must be prefixed with \\?\

Similarly you can delete the directory with the rd command

rd "\\?\C:\mydirectory "

but the directory must be empty before deleting it with this command.

TampaHaze
  • 206
  • This is what I tried at the time and failed. But I may just have gotten the syntax wrong (I cannot remember it precisely). I don't have access to the system anymore, maybe someone else can confirm if this works. – mafu Sep 13 '19 at 17:33
  • Best answer so far imo as it works and does not need any third party tools. – stackprotector May 11 '20 at 14:55
  • 1
    Note that the same DOES NOT WORK in PowerShell! – Ian Kemp Apr 14 '21 at 09:16
3

cygwin can

$ mkdir 'abcde'

$ mkdir 'abcde '

$ mkdir 'abcde  '

$ ls -l
total 0
drwxr-xr-x+ 1 user None 0 May 15 17:11 abcde
drwxr-xr-x+ 1 user None 0 May 15 17:11 abcde
drwxr-xr-x+ 1 user None 0 May 15 17:11 abcde

ok it clearly worked since we have 3 distinct directories, but let's try to get some kind of further proof

$ echo * | xxd
0000000: 6162 6364 6520 6162 6364 6520 2061 6263  abcde abcde  abc
0000010: 6465 2020 0a                             de  .

6162 6364 65 20<---- that's the dir abcde, and echo * put a space in after as there is another item it lists.

6162 6364 6520 20 <-- that's the dir 'abcde ', and echo * displayed it with a space in as there is another item it lists

61 62636465 2020 0a  <-- that's the dir 'abcde  ', and echo * displayed it followed by new line(0a)

Another

ls offers some formatting options like verical and others e.g. with commas

--format=WORD
       across -x, commas -m, horizontal -x, long -l, single-column  -1,
       verbose -l, vertical -C

--

$ ls -m
abcde, abcde , abcde

Notice the space before the comma that is because the space is in the filename. The ', ' is formatting. But ' ,' shows the space is in the filename.

$ ls -m | xxd
0000000: 6162 6364 652c 2061 6263 6465 202c 2061  abcde, abcde , a
0000010: 6263 6465 2020 0a                        bcde  .

--

a very clear demonstration without needing to look at any hex to verify, is-

$ mkdir 'abcde           '


$ mkdir z


$ ls -m
abcde, abcde , abcde  , abcde           , z


$

Another way, as Mafu mentions in comment, is Mingw which has an Msys package you can install in the ming mackage manager, and msys installs many commands to C:\MinGW\msys\1.0\bin> e.g. mkdir.exe, and has a bash.exe too

barlop
  • 23,849
  • Thank you for the good idea, I used MinGW and it worked just as well. – mafu May 17 '15 at 12:31
  • @mafu can you post an answer about how you did it in MinGW? Did you write a C program with a line to create a directory, and use the Ming compiler? (in which case any compiler e.g. Tiny C compiler or any other one would've done it I suppose). – barlop May 17 '15 at 19:56
  • No, I did not have to resort to such measures. MinGW includes a MSYS option, which further includes basically a linux command prompt. I suspect it is very much the same as cygwin provides. The commands are obviously exactly the same, too, so I could completely follow your answer. (Sorry if I misworded this comment, I am not very familiar with linux environments.) – mafu May 18 '15 at 00:27
2

Please note that creating a file not supported by Windows can create a situation where you may be unable to delete it by normal means.

Boot into a linux install or live cd, then run this command

mkdir "<mountpoint>/dirname"

You will need the quotation marks.

See this for how to delete

  • 2
    -1 Read the question. He is asking how to CREATE and IN WINDOWS 7. Not how to delete it from another OS. And not even how to create it from a linux live cd. (though creating it from a linux live CD might be OK as a comment) – barlop May 15 '15 at 16:10