Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Purging old files

nchernoff

Administrator
May
42
2
Staff member
Migrated From JP Software Wiki

If you have a shared network directory that many users can write to, you will want to purge old files from it regularly. This chore is trivial with a batch file, which may be run automatically using the Windows scheduler or just run 'by hand' at regular intervals.

A date range will select the files to be deleted. Don't use the last-write date — it's almost meaningless. The creation date is a much more reliable indicator of how long a file has been on your server.

You will probably want to use the DEL command's /S and /X options to remove subdirectories in your purge. One potential gotcha is that, if all files are deleted and the shared directory is left empty, the shared directory itself will be removed (which is bound to cause consternation and unhappiness.) One way to prevent removal of the shared directory is to change to it before doing the purge, since you cannot remove the current directory:

Code:
set n=8
pushd e:\share || quit
del /[dc-%n,1980-01-01] /s /e /x /y /z *
popd

Note that we check PUSHD's exit code, and QUIT if there was a problem. You really, really don't want to do a wildcard delete in the wrong directory on your file server!

Another way to prevent the shared directory from inadvertently being removed is to make sure that it is never emptied — that at least one file will survive the purge. It is a good idea to provide a text file in the shared directory warning users that files will be deleted; you can kill two birds with one stone by also using this file to prevent the shared directory from being removed. Be sure to set the file permissions such that ordinary users can view this text file, but not change or delete it. Your text file might look something like this README.TXT:


Code:
\\JEEVES\SHARE     Temporary public storage folder

This folder is provided as a service to the staff, to make it easier to share files. It is not for long-term storage! Anything kept in this folder for more than a week WILL BE DELETED AUTOMATICALLY, no exceptions! Use your own network folder for long-term storage.


If this warning file is never deleted, then your shared directory will not be removed. How do you prevent it from being purged when it reaches 'retirement age'? You might be tempted to use an exclusion range, but of course users might create files with the same name, and the exclusion range would spare those as well. You might also set its file permissions such that the batch job does not have rights to delete it. Another, sneakier way is to simply set its creation date to some date in the distant future:

Code:
touch /dc2099-12-31 e:\share\readme.txt

Once you know that your shared directory will never be emptied, you can use a simpler (and safer) purge command:

Code:
set n=8
del /[dc-%n,1980-01-01] /s /e /x /y /z e:\share\*

Elaborations could be added. For example, you could use the @DISKFREE function to calculate the space freed by the purge, and note it in the event log with EVENTLOG.
 
Back
Top
[FOX] Ultimate Translator
Translate