Delete a file named "NUL" on Windows

CWindowsNullCygwin

C Problem Overview


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?

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

run Git Bash Here

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

after command execution

Solution 3 - C

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.

Solution 4 - C

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

Solution 5 - C

If you have git on windows, just right click on the folder containing the nul file -> go to gitbash here -> and type "rm nul" or "rm nul.json" depending upon the file type.

Solution 6 - C

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.

Solution 7 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEM0View Question on Stackoverflow
Solution 1 - CxxbbccView Answer on Stackoverflow
Solution 2 - CGeovani MartinezView Answer on Stackoverflow
Solution 3 - CBen JacobsView Answer on Stackoverflow
Solution 4 - COlivierView Answer on Stackoverflow
Solution 5 - Chitesh_fulwaniView Answer on Stackoverflow
Solution 6 - CspryceView Answer on Stackoverflow
Solution 7 - CasdfView Answer on Stackoverflow