Delete a file named “NUL” on Windows

2019-01-22 11:32发布

问题:

I ran a program on Windows 7 that was compiled under Cygwin and passed "NUL" as an output file name. Instead of suppressing output it actually created a file named "NUL" in the current directory. (Apparently it expects "/dev/null", even on Windows.) Now I'm stuck with this "NUL" file that I cannot delete!

I've already tried:

  • Windows Explorer - error: "Invalid MS-DOS function" (yes, that is seriously what it says!)
  • Command prompt using "del NUL" - error: "The filename, directory name, or volume label syntax is incorrect."
  • Deleting the entire directory - same deal as just deleting the file
  • remove() in a C program - also fails

How can I get rid of these NUL files (I have several by now), short of installing the full Cygwin environment and compiling a C program under Cygwin to do it?

回答1:

Open a command prompt and use these commands to first rename and then delete the NUL file:

C:\> rename \\.\C:\..\NUL. deletefile.txt
C:\> del deletefile.txt

Using the \\.\ prefix tells the high-level file I/O functions to pass the filename unparsed to the device driver - this way you can access otherwise invalid names.

Read this article about valid file / path names in Windows and the various reserved names.



回答2:

I had a similar issue. It was probably caused by Cygwin as well (since I use this regularly), but I've since forgotten exactly how the file was created.

I also had trouble deleting it. I followed the advice of some other posts and tried booting into safe mode to delete the file, though this did nothing. The tip from +xxbbcc didn't work for me either.

However, I was able to delete the file using the Cygwin terminal! Cygwin createth and Cygwin destroyeth.



回答3:

To remove a nul file situated here: C:\unix\cygwin\dev\nul

I simply use (tested only on Windows 10) : Del \?\C:\unix\cygwin\dev\NUL



回答4:

If you have Git for Windows Installed (v2.18) do the following

  1. Open the directory containing the files you want to remove
  2. Left Click and select Git Bash Here
  3. Type rm nul.json at the command prompt and hit ENTER, the file now should be removed.

NOTE: These screenshots show the removal of file nul.topo.json which is another file that I could not removed with a simple delete.



回答5:

I solved this in a slightly different way.

I thought I would add this here because it is high in the google results and I had a similar issue for a folder named NUL.

I tried rmdir\\?\C:\My\Path\NUL and rmdir\\.\C:\My\Path\NUL without any success and also tried several commands using bash from my SourceTree installation. No joy.

In the end I used DIR /X /A from cmd to list the short names in the parent directory. This showed me NUL~1 for my NUL folder and identified the crux of the problem.

This was then used in the standard command rmdir /s NUL~1 and finally resolved the issue.



回答6:

Try writing a short C program that calls the Windows API to delete that file.

http://msdn.microsoft.com/en-us/library/aa363915%28v=vs.85%29.aspx

If that doesn't work, try opening a handle to the file with CreateFile() with FILE_FLAG_DELETE_ON_CLOSE, and then close the handle.