A relatively unknown batch file utility which ships with Windows (Vista) is forfiles:

Selects a file (or set of files) and executes a command on that file. This is helpful for batch jobs.

On all of my workstations I have a task scheduled to run nightly:

forfiles /p "%TEMP%" /m *.* /s /d -28 /c "cmd /c if @isdir==TRUE (echo rmdir @path) else (echo del /q @path)"

This deletes all files from my temp directory older than 28 days.

I used to have a Perl script that does this, and I’ve seen this done with complicated for syntax, however this doesn’t require anything to be installed, and is much simpler :)

pushd/popd and UNC paths

June 23rd, 2008

According to the help for pushd:

Stores the current directory for use by the POPD command, then changes to the specified directory.

Generally you use it like:

C:\>pushd c:\windows\system32
c:\Windows\System32>rem blah
c:\Windows\System32>popd
C:\>

Not very exciting.

However pushd has a little known feature; you can dynamically map UNC paths:

C:\>pushd \\server\netlogon
Z:\>rem blah
Z:\>popd
C:\>

In this case pushd finds the first available drive letter (working backwards from Z:), maps a network drive, and changed the current working directory to it.  popd then unmaps the network drive.

This makes is a very handy tool for batch scripting, including in login scripts.