Is there a way to delete a folder in
Windows and not having the time taken
proportional to the number of files
within it?
I don't think so, but some methods are clearly much quicker than others.
The worst way is to send to Recycle Bin: you still need to delete them. Next worst is shift+delete with Windows Explorer: it wastes loads of time checking the contents before starting deleting anything.
Next best is to use rmdir /s/q foldername
from the command line. del /f/s/q foldername
is good too, but it leaves behind the directory structure.
The best I've found is a two line batch file with a first pass to delete files and outputs to nul to avoid the overhead of writing to screen for every singe file. A second pass then cleans up the remaining directory structure:
del /f/s/q foldername > nul
rmdir /s/q foldername
This is nearly three times faster than a single rmdir, based on time tests with a Windows XP encrypted disk, deleting ~30GB/1,000,000 files/15,000 folders: rmdir
takes ~2.5 hours, del+rmdir
takes ~53 minutes. More info here.
This is a regular task for me, so I usually move the stuff I need to delete to C:\stufftodelete and have those del+rmdir
commands in a deletestuff.bat batch file. This is scheduled to run at night, but sometimes I need to run it during the day so the quicker the better.
When you copy uncompressed files into a NTFS compressed folder the compression level isn't changed, and you cannot set them to compress them automatically (only newly created files are created as compressed). Instead you have to use compact.exe to compress all the files in the folder after you have moved files in there.
You can do this using a batch file - change directory to the compressed directory and run the command:
compact /c (use /s to include subdirectories)
or, to save changing directory:
compact /c "c:\compressedfolder\*.*"
Best Answer
If you simply want to delete files after a given number of minutes with no advanced logic, the GNUWin32 build of the Linux "find" command is a simple option.
Setup
Use
Delete all files older than X minutes (e.g. 5 minutes) in a given directory (e.g. TempFiles)
Note that the ! (negation) operator is important. Leaving this out will delete files under (less than) X minutes old.
Options
-name can be used to filter items. -name should always appear before -delete or it will have no effect e.g.
In the example above, -name matches any file with the sequence "text" anywhere in the file name (including extensions). Use an asterisk (*) for any portion where you want wildcard matching. Asterisks can appear more than once. If matching an exact name, you must include an extension on Windows.
-exec ex_command {} + allows the use of an external command e.g. "ex_command" when eligible items are found. This could be useful for running a separate process (such as a batch file or similar) when "find" matches items successfully. This option should be put at the end of the "find" options i.e.
-delete is optional (as are all the options). You can leave it off if you want to preview what "find" will be working on (e.g. for testing or troubleshooting) or simply want a listing of files.
Be aware that if you are listing files for output, a UNIX forward slash will be placed between the directory name and the file name e.g.
Therefore, you may need to take appropriate steps to correct this later.
Other Options
If you need fancier options than the ones listed above, you may have to write your own script.
Features vary by language but, as an example, Python has time.time() (current time via the time module) as well as os.path.getctime() and os.path.getmtime() (Windows creation time and file modification times respectively via the os module). These all return floating points in seconds from an epoch time and make time comparison extremely easy.
As you mentioned Java, you may want to check out this StackOverflow question on Java NIO for similar features.
Automation
As mentioned in comments, Windows Task Scheduler can be used to run items periodically, including once ever X minutes. You could use it to run a single-line batch file with the GNUWin32 "find" command above.
You can set increments as small as 1 minute in the Trigger settings for the Windows 7 Task Scheduler. You must type in custom increments e.g. 1 minute manually in the "Repeat task every:" drop-down field, otherwise an item from the default increment list will be used.
Note that while Task Scheduler may be acceptable, one quirk is that it technically starts a task at a given time, then repeats it for 24 hours. This can lead to problems if the computer is not on when a task is set to run.
To help counter this, you must enable "Settings → Run task as soon as possible after a scheduled start is missed".
Other Automation Options
As a suggestion, a tool similar to the UNIX "cron" utility might be preferable. There are a number of better and worse emulations of it, but personally I prefer IntelliAdmin Cron Service v2.0.
Unfortunately, the website link seems broken as of this writing (it doesn't seem to email links for free utilities anymore). However, if you trust strangers on the internet, you can download it from here.
IntelliAdmin Cron installs a simple service that runs listed tasks at specific intervals as long as the service is running. It will accept crontab files for scheduling but also has a GUI interface.
One thing to note is that the fields in the GUI interface still expect crontab style entries for specifying times to run. You may want to research this further but the format to run every X minutes is
with all other values set to "*". So if we wanted to run a command every minute, it would look like this in the interface: