22

I have a bunch of folders on my hardrive that have trailing spaces in them (I have no idea how they got there). I need to rename and/or delete them, but Windows won't allow me to do this. Is there a way around this problem?

I've tried to delete it via the commandline, but that did not work.

I'm running Windows XP, and it's an NTFS drive.

8 Answers 8

29

First try some quoting:

rd "my folder   "

Then UNC paths:

rd "\\.\C:\temp\my folder   "

Then try adding a trailing [back]slash:

rd "my folder   \"
rd "\\.\C:\temp\my folder   \"

Then 8.3 names (if they are enabled on your system):

rd myfold~1

Then a Linux LiveCD:

rmdir "/media/blahblah/temp/my folder   "
4
  • 3
    Deleting using the 8.3 names worked. Thanks Oct 11, 2010 at 15:18
  • "mydomain.com " as a folder. DOS thought the 8.3 filename was "mydoma~1.com". But it worked. Super thanks!
    – Coomie
    Dec 8, 2011 at 6:09
  • 3
    Backslash method worked for me on Windows 10
    – KnightHawk
    Dec 21, 2016 at 15:57
  • For those who look for a more user-friendly solution, I uploaded a registry file on GitHub which you can import on your computer. This will add two new context menu options to your right-click, one to delete files with trailing spaces and one for folders. See github.com/thomaspdt/delete-file-with-space
    – Thomas
    Nov 15, 2023 at 17:03
19

I had the same problem, in Windows 7 x64, and none of the command-line solutions worked here.

What fixed it for me:

  • Rename the folder using 7-Zip
  • Delete, either using 7-Zip or Explorer (both work)

Note that deleting the folder in 7-Zip before renaming it was impossible.

3
  • 1
    7-Zip worked for be on a 2008R2 trailing folder that I could not delete.
    – user135634
    May 22, 2012 at 17:34
  • 1
    Thanks this was the only method that worked for me on a folder mounted from a remote file server
    – Anigel
    Apr 2, 2015 at 10:23
  • This workaround worked on windows server 2019
    – Force Flow
    Jul 16, 2021 at 18:02
7

windows 7

I was having issues getting rid of a folder. "C:\Users\Rob\Desktop\Music\ A k o n "

This is what finally worked for me: I opened up the cmd prompt in the Music folder (shift right click, open command window here)

then typed in the following command

rd " A k o n \"

notice the back slash.

beginning spaces and trailing spaces really mess crap up. I spent at least 2 hours searching for a solution.

Thank you grawity for posting the solution.

4

There is still not the best support for trailing spaces in folder names on Windows. Most commands will work, when using UNC paths.

Windows command prompt

Creating a folder with a trailing space in its name:

md "\\?\C:\FolderWithTrailingSpace "

Renaming a folder with a trailing space in its name seems to require a workaround. Create a folder with the new name, move the content to it, delete the old folder:

md "C:\FolderWithoutTrailingSpace"
move "\\?\C:\FolderWithTrailingSpace \*" "C:\FolderWithoutTrailingSpace"
rd "\\?\C:\FolderWithTrailingSpace "

Deleting a folder with a trailing space in its name:

rd "\\?\C:\FolderWithTrailingSpace "

Windows PowerShell

Creating a folder with a trailing space in its name:

New-Item -Path "\\?\C:\FolderWithTrailingSpace " -Type Directory

Renaming a folder with a trailing space in its name:

Rename-Item -LiteralPath "\\?\C:\FolderWithTrailingSpace " -NewName "\\?\C:\RenamedFolderWithTrailingSpace "

Deleting a folder with a trailing space in its name:

Remove-Item -LiteralPath "\\?\C:\FolderWithTrailingSpace "

Workaround for cd/Set-Location

In order to change your directory to a folder that contains a trailing space in its name, you can use a workaround with symbolic links. Create a symbolic link to the folder with a trailing space in its name using the mklink command of an elevated Windows command prompt (not available in PS):

mklink /D "C:\FolderWithoutTrailingSpace" "\\?\C:\FolderWithTrailingSpace "

After that, you can use cd in the command prompt or in PowerShell to switch to that directory:

cd C:\FolderWithoutTrailingSpace

Or use Set-Location in PowerShell:

Set-Location -LiteralPath "C:\FolderWithoutTrailingSpace"
1

Then the DOS name:

dir /x
rd FOLDER~1

Then the (UNC?) name which I'm not sure how to find or what exactly it looks like:

rd \Device\HarddiskVolume1\temp\folder
rd \\?\Device\HarddiskVolume1\temp\folder
1
  • System-local UNC paths start with \\.` for devices and \\?` for files (official doc); using `\\?\C:` is often enough. Oct 12, 2010 at 18:21
1

RoboCopy seems particularly good at addressing folders/paths verbatim, so you could make an empty folder alongside the parent of the folder that has your erroneously named folder and then copy the empty folder to be the parent folder path and specify the PURGE option, which will purge the other entries in the destination folder. Hopefully you will also ensure that only bad folders are there

In summary, you want to copy 0 files into the folder that houses your bad folder, and specify /purge so that the operation clears out anything else in the folder (including your bad folder)

Confused? Then an example will surely help. Suppose you have:

C:\path\leading-to-a\folder-with-space \

Run these commands:

cd c:\path\
mkdir empty
robocopy empty leading-to-a /purge

robocopy will copy the empty folder to the leading-to-a folder, and purge the other entries in the leading-to-a folder, one of which is your folder with a trailing space:


  ROBOCOPY     ::     Robust File Copy for Windows

 Started : Thu Jan 01 00:00:00 1970

  Source : C:\path\empty\
    Dest : C:\path\leading-to-a\

   Files : *.*

 Options : *.* /COPY:DAT /PURGE /R:1000000 /W:30

                          0    C:\path\empty\
       *EXTRA Dir        -1    C:\path\leading-to-a\folder-with-spaces \

              Total    Copied   Skipped  Mismatch    FAILED    Extras
   Dirs :         1         0         1         0         0         1
  Files :         0         0         0         0         0         0
  Bytes :         0         0         0         0         0         0
  Times :   0:00:00   0:00:00                       0:00:00   0:00:00

  Ended : Thu Jan 01 00:00:00 1970

Warning:

Make sure that anything you want to keep is moved out of the leading-to-a folder first!

0

Boot with a live CD of Ubuntu, and you will be able to delete anything you want.

To boot with the CD you should change the boot order in your BIOS settings.

0

Use WinRAR to find that file and rename it without the space at the end. You should be able to delete it!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .