39

I have created a folder via PHP, but I made a mistake since I forgot to clear spaces. Now I cannot delete this folder.

I have found a similar issue here - look at the second answer (Quoted below), but I don't know how to rename a folder with 7-Zip.

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 Windows Explorer (both work).

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

I also tried the command line, but it does not work. My folder is named " 1 ".

I downloaded 7-Zip, and Google does not provide the help I need.

5
  • 1
    Folder names that start and end with spaces shouldn't be a problem on Win7. I was able to create a folder named " 1 " and then delete without problem using just the mkdir and rmdir commands built into cmd.exe. Is it possible the name contains other characters? E.g., are those double quotes part of the name or is it possible those spaces aren't spaces and displaying as spaces? Mar 13, 2013 at 16:39
  • I have to agree with Nicole, here must be something else you guys are missing. Leading or trailing spaces are no problem for Windows 7 using mkdir to create a folder called " test " and normal deletion via Explorer afterwards
    – nixda
    Dec 27, 2014 at 12:45
  • Great solutiion i had a file named "Invisble, Inc." which was created by Steam and belonged to the game Invisible Inc. Using 7 zip i was able to rename it and after that deleting it.
    – l1zard
    Feb 15, 2016 at 18:38
  • I had this issue as well on Win7x32. After creating a folder without the space, I was able to rename the old one (ie: new folder "1" and then delete "1 ") Apr 2, 2019 at 14:52
  • You could use mv.exe from GnuWin32 or Cygwin. On Windows 10, use wsl mv from Windows Subsystem for Linux.
    – bers
    Aug 16, 2019 at 6:40

10 Answers 10

9

Find the item you want renamed in the main window. Then right click the file/folder in the list and click "rename".

You could also select it by clicking it once and press F2.

Edit:

Try this from the commandline:

rename "\\?\c:\<PATH HERE>\[ ]1[ ]" "<NEW FILE NAME>"

or

rename "\\?\c:\<PATH HERE>\ 1 " "<NEW FILE NAME>"

Mind you, the NEW FILE NAME doesn't take the full path, so just use the file's name. PATH HERE is just the path without the file.

10
  • With the first example : not found - With the second : incorrect syntax - Should I do it from the root ?
    – Ocelote
    Mar 13, 2013 at 15:49
  • 1
    I used cd to go to images, and from there : \\?\C:\Websites\education-fonctionnelle\images\[ ]1[ ]" "2" => not found. \\?\C:\Websites\education-fonctionnelle\images\ 1 " "2" => incorrect syntax
    – Ocelote
    Mar 13, 2013 at 15:55
  • @Ocelote, after the NEW FILE NAME, you should end with a ". I don't see this in your comment, did you use this? so: \\?\C:\Websites\education-fonctionnelle\images\[ ]1[ ]" "2"
    – Pylsa
    Mar 13, 2013 at 15:56
  • 1
    I managed to do it again. For whoever passes by here try the command lines above. I also tried rd "\\?\C:\Websites\education-fonctionnelle\images\[ ]1[ ]" "2" (which is not correct but still, it is part of what I did) THANK YOU !!!!!!!
    – Ocelote
    Mar 13, 2013 at 16:22
  • 2
    I have a folder with a blank space at the end. Nothing from this answer or the comments have worked. Possibly not possible in Server 2012.
    – HippoDuck
    Jan 23, 2018 at 16:08
34

The answer from @Ocelot did not work on Windows Server 2008 R2. What did work was:

Open a command, change to the directory and do a "dir /x". You will see the long and the short name of the directory. You can always rename the short version as it does not contain spaces most of the time. So a ren 3839F~1.4_T whatever should be possible.

4
  • 1
    This is the correct answer with the current versions of the operating system. Smart, using the 8.3 file name support. I wonder why the "\\?\" hardware path was is longer supported, that is also required to support paths with a total length of more than 250-odd characters.
    – Tony Wall
    Oct 6, 2015 at 9:58
  • 1
    this works for folders with a shortname...if you find yourself in the unfortunate position with no shortname you'll have to use a third-party tool (mingw64 which comes with git worked for me), this also wroked for me https://stackoverflow.com/a/21074385/167304
    – Jason
    Dec 27, 2018 at 17:05
  • Worked on Windows 10, but not Windows 2012R2 for me: Some NTFS versions no longer generate 8.3 filenames. the 7Zip method below worked on everything.
    – Dave
    Sep 29, 2021 at 17:14
  • wow nice trick, worked
    – bryc
    Apr 22, 2022 at 22:43
29

I just had a similar problem with folder "Monuments - Discography " created in linux. Windows Vista and Windows 7 couldn't recognize this folder as a valid data and when I tried to rename or remove it I got Info message saying that folder does not exist etc. The solution was to explore a dir with 7zip file manager and rename the folder by removing a white space from the end. Simple. Now I can enjoy the music once again :D

4
  • The OP said in their question that they do not know how to rename a folder using 7-zip. Can you please explain how to do that? Thanks.
    – Cfinley
    May 7, 2015 at 18:30
  • 1
    press f2 like in windows explorer. Note you need the GUI filemanager of 7z, "7zFM.exe" May 19, 2018 at 12:43
  • 5
    This worked for me on Windows 10 when all the rest failed
    – Deleted
    Jul 20, 2019 at 0:37
  • I searched for several days. DotNet solution in C# failed, with administrator priviliges failed, just using 7Zip worked. Windows 10, directory created by NextCloud with space at the end.
    – Nasenbaer
    Dec 13, 2021 at 16:09
9

As I was not able to reproduce the solution of the accepted answer, and other answers did not help either (except the 8.3 naming workaround), I want to share some methods that I already included in this answer:

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
  • 1
    Remove-Item -LiteralPath "\\?\drive:\FolderWithTrailingPeriod." -Force -Recurse was the only solution that worked for me, I had a non-empty directory and the directory ended with a "."
    – Justin
    Jul 7, 2023 at 15:38
4

Use the old DOS 8.3 naming method:

If the directory was called "Blah " then you could cd Blah~1 for example. Delete the contents of the folder, then at the root you could rmdir Blah~1

1
  • I had a folder called test and another called test <space>. rmdir TEST~1 removed the one with a space. This is a nice trick for those that do not have 7zip around.
    – tresf
    Jun 19, 2017 at 17:52
4

Install winrar. Open it up, navigate to the file location, and you should be able to rename it. (I was having the same problem and this worked for me).

1
  • 1
    Winrar works but you need to run it as an "Administrator".
    – Regmi
    Mar 6, 2019 at 23:12
2

I just had the same problem. After using a renaming utility, I discovered that I had four files that had a leading space. I tried renaming it, but Windows coughed and said that the names couldn't be changed.

I decided to add a character (in my case, I just typed "1") in front of the leading space and I could save it (renamed). I then could rename the file by removing the character (again, in my case I just added a "1") and the aforementioned leading space without any problems.

1

Are you using Windows 7? Try to do a chkdsk /f on the affected drive. It should correct illegal filenames (beware: it will move the files to a FOUND.000 folder on the root)

1

Most of the answers, oddly, no longer work with Win10/Server 201X.

I found the only thing that did work was using the short name.

Situation: QT allowed me to create a directory with a trailing space. I found that: rmdir * didn't work (I had 3 such directories)

rmdir "director with space " didn't remove it with CMD.

rmdir * /s (TCC from JPSoft) didn't do it.

The Explorer wouldn't remove it; I kept getting an error that the directory no longer existed. I also have Win10 in developer mode.

Powershell wouldn't do it.

The \?\ syntax wouldn't do it.

However, dir /x and then using the shortname to remove the directory worked fine.

I would have made a comment to @rantygrant but my SO rep doesn't follow me here, so I made an answer to explicitly mention most of the other answers are now broken in Win10. Here's hoping you still have compatibility with 8.3 turned on. I did not try the 7-zip solution.

2
  • 1
    Tried all these in Win10, nothing worked. dir /x showed no 8.3 filenames at all. Finally, rename from inside WinRAR worked.
    – egrunin
    Jan 26, 2021 at 22:24
  • 1
    Hey, it's great that worked! I didn't need to try it, and once they were gone, I didn't have a chance to check WinRAR. Glad it worked for you! Thanks for commenting so others have two potential ways to do it.
    – J. Gwinner
    Jan 28, 2021 at 5:25
-3

I had two files, one with a space at the end, and the other with a period at the end. Windows refused to allow me to rename or delete either. I tried doing it through Python's os module, as well, to no avail. The solution that finally worked for me was to open a command prompt (cmd.exe), cd to the folder, and then run the command:

del *.*

Of course, this means moving everything you want to keep out of the folder first.

EDIT: Sorry, reread the question and realized you are talking about a folder, not a file, so my answer really doesn't help you, but I'm going to leave it here for anyone else who has similar issues.

1
  • 4
    It's pretty dangerous to suggest things like this. People who don't know what they're doing will delete everything by accident.
    – Brad
    Mar 25, 2019 at 15:43

You must log in to answer this question.

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