Reputation: 181
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?
Upvotes: 2
Views: 48198
Reputation: 1003
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:
Upvotes: 6
Reputation: 6703
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.
Upvotes: 2