Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient method of continually deleting files older than X hours on Windows?

I have a directory that continually fills up with "artefact" files. Many different programs dump their temporary files in this directory and it's unlikely that these programs will become self-cleaning any time soon.

Meanwhile, I would like to write a program that continually deletes files in this directory as they become stale, which I'll define as "older than 30 minutes".

A typical approach would be to have a timed mechanism that lists the files in the directory, filters on the old stuff, and deletes the old stuff. However, this approach is not very performant in my case because this directory could conceivably contain 10s or hundreds of thousands of files that do not yet qualify as stale. Consequently, this approach would continually be looping over the same thousands of files to find the old ones.

What I'd really like to do is implement some kind of directory listener that was notified of any new files added to the directory. This listener would then add those files to a queue to be deleted down the road. However, there doesn't appear to be a way to implement such a solution in the languages I program in (JVM languages like Java and Scala).

So: I'm looking for the most efficient way to keep a directory "as clean as it can be" on Windows, preferably with a JVM language. Also, though I've never programmed with Powershell, I'd consider it if it offered this kind of functionality. Finally, if there are 3rd party tools out there to do such things, I'd like to hear about them.

Thanks.

like image 814
marc esher Avatar asked Jul 16 '09 00:07

marc esher


People also ask

How can I delete old files quickly?

Click Start > File Explorer > This PC (Windows 10). Right-click your main hard drive (usually the C: drive) and select Properties. Click the Disk Cleanup button and you'll see a list of items that can be removed, including temporary files and more. For even more options, click Clean up system files.

What is the best way to delete files?

With the mouse, click the files that you want to select. Release the CTRL key when done. TIP: To select all files in a folder, press CTRL+A on your keyboard. To delete the selected file(s) and move it (them) to the Recycle Bin, press the Delete key on your keyboard.


2 Answers

Why can't you issue a directory system command sorted by oldest first: c:>dir /OD

Take the results and delete all files older than your threshold or sleep if no files are old enough.

Combine that with a Timer or Executor set to a granularity 1 second - 1 minute which guarantees that the files don't keep piling up faster than you can delete them.

like image 154
Kelly S. French Avatar answered Sep 22 '22 18:09

Kelly S. French


If you don't want to write C++, you can use Python. Install pywin32 and you can then use the win32 API as such:

import win32api, win32con
change_handle = win32api.FindFirstChangeNotification(
    path_to_watch,
    0,
    win32con.FILE_NOTIFY_CHANGE_FILE_NAME
)

Full explanation of what to do with that handle by Tim Golden here: http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html.

like image 20
Lanny Avatar answered Sep 20 '22 18:09

Lanny