DIY · TechIt · Windows

Deltree on Windows Server 2003

So I have some recurring tasks running on an old server running Windows Server 2003.

One of these tasks likes to fill up the directory %windir%\temp (usually that’s C:\windows\temp) with lots of files, which – if I don’t watch it – results in the C: drive filling up.

Disk cleanup doesn’t clear out files left in %windir%\temp for some reason, so I decided to run a weekly job to do it.

“A job just made for yee olde Deltree command!” I thought triumphantly.

But then, there was no Deltree command shipped with Windows Server 2003. 😦

I read that installing Deltree may not be a good idea, as it may fail in circumstances for which it was not tested for. So began the search for a Deltree equivalent.

I tried using Del /f/s/q, but it leaves empty directories lying around. Then I don’t know what’s empty without checking each.

Rmdir needs a dir name, but I wanted to delete all files and directories under %windir%\temp, and the names are not repeated for the most part. (if you have the dir name, then just rmdir /s/q DIRNAME2>NUL should work for you).

One post suggest doing a CD to the target directory and then deleting it. This would have the effect of deleting all sub folders and files but not the directory itself as it is the processes current directory. For e.g.:

cd %windir%\temp
rmdir /s/q . 2>NUL

Now, this should be fine with %windir%\temp, as that dir should always exist. But I just could not get away from what might happen if for whatever reason the cd fails… likely mass file deletion from wherever the command was run (the users home dir?).

So I went for the option to rmdir the target and recreate it with mkdir.

rmdir /s/q %windir%\temp 2>NUL
mkdir %windir%\temp 2>NUL
exit 0

I guess it could be a problem if something trys to write to the target between when it was deleted and recreated.

But so far there is always a file in use to prevent the deletion of %windir%\temp.

Still it’s not ideal, but so far it works just fine in my case.

I am curious about ways to do this better. Perhaps by forcing use of %windir%\temp somehow so it is definitely never removed,or to list the contents of %windir%\temp and pipe them each individually to del or rmdir.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s