View Full Version : Automatically delete files

04-21-2008, 04:58 PM
I am going to try and hit 2 similar birds with 1 stone in this thread.

1) Our network printer has a scan to PDF function and the files are saved into the network folder G:\Scan Storage. People forget or accidentally hit the wrong button and the files never get removed. What I am looking to do is have files older than 14 days old get automatically deleted from the folder above. If it matters, only PDF's are stored in this folder.

2) The next questions is similar to the one above. I would like to delete *.bak and plot.log files that are older than 30 days from the G: drive. There are hundreds of subdirectories and it needs to scan all of them.

Is there some script that can automatically run these functions so I don't have to hit a button? If it matters, our server is using Win Server 2003. I don't know what kind of script is needed for my application. I don't care if the script is on our network or on my local (Win XP) machine.

04-21-2008, 09:25 PM
Certainly possible. Basically, you will loop through every folder like this:
open the main directory (G:). Loop through every file/directory inside that:
if file-- check if it's too old based on criteria (last 3 letters, date, etc), and delete if applicate
if folder-- repeat this whole process by looping through that folder and its subfolders.
When that subfolder is done, the main loop will continue through the rest of the folders in the main folder--
and you'll have gone through every file on the drive.

There are also some ways to set restrictions along the way (don't delete anything starting with an underscore, or skip folders named "save", etc.)

Now, the real question is which language. If you happen to have PHP installed I could write this for you easily. I really should learn another language soon-- because right now that's about all I have to offer. You could probably do this with a batch file, if looping works properly-- I'm not sure how complex that can get, but I assume reasonable. (Or write a basic C++ program, etc.-- actually would be possible to compile a program from PHP like that, but you can probably find an easier way.)

04-22-2008, 06:39 AM
Are you allowed to use external program? If so look for a tool called LF and than use a cron job. An observation: i see you use spaces in your directory names. Delete doesn't like that very much
Getting the names is simple dir /s (/b) *.ext, but also includes files you don't want to delete. I would try LF.