Schedule task to delete contents from folders

43,177

Solution 1

Create a delete.bat file, put this in for every folder you want to delete from:

del "C:\your_folder_name\*.*"

Then schedule a task to run delete.bat. Or to loop folders under a certain folder and delete all those files, you can use:

C:\> CD \your_folder_name
C:\> FOR /D /r %G in ("*") DO del "%G" /s /Q

/s deletes from all subfolders
/q does it quietly (doesn't prompt for every file)
%G is the subfolder var

Further Reading:

Solution 2

The DEL command has a /S (subfolders) option that deletes under sub-folders, but preserves the directory structure. You can combine this feature with the AT command to schedule the command to be run every day. Assuming the folder to delete files is C:\tmp, and you want to run the cleanup every day at 23:59, you can issue the following command.

AT 23:59 /EVERY:m,t,w,th,f,s,su "del C:\tmp\* /S /Q >> C:\cleanup.log"

It will also write the deleted filenames to C:\cleanup.log, since there's no other way to figure out the results of the command.

For a complete reference see DEL and AT.

Share:
43,177
Axle
Author by

Axle

System Engineer Working with SQL Server and Visual Studio.

Updated on June 28, 2020

Comments

  • Axle
    Axle almost 4 years

    I want to run a job which deletes the contents of a folder every night. The folder contains 5/6 subfolders. Is it possible to delete from each of these sub folders but not delete the actual folders? Or do I have to run a task for each folder?