Getting rid of nul, or: How I learnt to love UNC (29 Apr 2006)

Every now and then, some tool on my system runs berserk and starts to generate files called nul. This is a clear indication that there's something going wrong with output redirection in a script, but I still have to figure out exactly what's going on. Until then, I need at least a way to get rid of those files.

Yes, that's right, you cannot delete a file called nul that easily - neither using Windows Explorer nor via the DOS prompt. nul is a very special filename for Windows - it is an alias for the null device, i.e. the bit bucket where all the redirected output goes, all those cries for help from software which we are guilty of ignoring all the time.

UNC path notation to the rescue: To remove a file called nul in, say c:\temp, you can use the DOS ren and del commands as follows:

  ren \\.\c:\temp\nul foobar
  del foobar

Because I always forget the exact UNC syntax, I came up with the following naïve batch file which does the job for me. Usage: delnul x:\some\path\nul.

@echo off
set fullpath=
for %%i IN (%1x) DO set fullpath=%%~di%%~pi

set filename=
for %%i IN (%1x) DO set filename=%%~ni

if "x%filename%" == "xnulx" goto :deletenul
echo This script is only meant to be used for "nul" files.
goto :end

:deletenul:
echo Deleting %fullpath%nul...
ren \\.\%fullpath%nul wasnul && del \\.\%fullpath%wasnul

:end



When asked for a TWiki account, use your own or the default TWikiGuest account.


Revision: r1.1 - 29 Apr 2006 - 09:49 - ClausBrod
Blog > BlogOnSoftware20060429
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback