5

I have an application at hand, which insists on writing huge amounts of log lines into a file, that I have to clean out manually all the time.

Is it possible to somehow replace the file with a symlink to NUL or similar, so that any data written to the file is redirected to nirvana immediately?

I already tried with mklink <logfile> NUL, mklink <logfile> NUL:, mklink <logfile> \\?\NUL et al., but while this works, a (hard to delete) NUL file is created in the directory (for NUL) or results in an error message (for NUL:), as soon as the application starts to log anything to the symbolic link. So, symlinking to NUL (or NUL:) does not seem to do the job, at least not with the means I came up with.

Any ideas on a solution?

6
  • What happens if you symlink it to \??\Nul? The mklink will succeed, but IO on it will fail; is the program alright with that?
    – Ben N
    Commented Feb 28, 2016 at 21:48
  • 1
    What is \??\Nul? This syntax is unknown to me.
    – Jinxed
    Commented Feb 28, 2016 at 21:51
  • It's an NT device name. See this article for more info.
    – Ben N
    Commented Feb 28, 2016 at 21:52
  • I doubt that. The correct syntax for accessing files and devices directly is different: See here.
    – Jinxed
    Commented Feb 28, 2016 at 21:56
  • Whoops, right, they're MS-DOS device names.
    – Ben N
    Commented Feb 28, 2016 at 21:57

1 Answer 1

1

You should use NUL: rather than those variants you were trying. This will work, on Windows 10 for me, at least, for a file named c:\bob.log:

mklink C:\bob.log NUL:

Note you do need to run this "as admin", and also the file needs to be deleted first.

3
  • 2
    Sadly, this does not do what I intended, either: The symlink is created (even without admin rights enabled), but piping anything into that file still results in an error message. Might it be that this is just not possible?
    – Jinxed
    Commented Oct 18, 2020 at 17:01
  • 1
    The filesystem as exposed by most of the Win32 API, including common user-facing things like explorer.exe and cmd.exe is not what's really underneath the hood. The real "filesystem" in Windows is the "object namespace" (check out SysInternals WinObj to browse it). Applications via various system DLLs can access things like \\?\Device\Null but that's not exposed by most built-in stuff in Windows.
    – LawrenceC
    Commented Apr 4, 2022 at 16:58
  • For folders add the /D flag like this: mklink /D c:\path\to\dir NUL:
    – W.M.
    Commented Jun 21, 2023 at 18:30

You must log in to answer this question.

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