0

I've been working with this script to help achieve my goal, which is deleting files and folders, but I'm a little confused as to how I can add a time length to it. I would like to delete files and folders after 2 weeks, any suggestions?

set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)

I've built off this post (Batch file. Delete all files and folders in a directory) but am unable to comment as my reputation doesn't allow it. Thanks again

  • 1
    What was wrong with you updating your previous [same question](https://stackoverflow.com/q/46183829/6738015). You have had two days additional time to learn how the forum works, further your knowledge and provide a better quality pertinent question. Adding code that is copied from a non similar question and which does not consider file dates or ages is really not good enough. – Compo Sep 15 '17 at 17:24
  • Or your other same question here: https://stackoverflow.com/questions/46221946/looking-to-include-subdirectories-as-an-item-to-delete – Squashman Sep 15 '17 at 19:02

2 Answers2

1

Apart from the answer provided by kranz, my humble suggestion is to add a fail safe in second command otherwise in case cd command fails, rmdir and del will wreak havoc in your current directory.

Following code will exit the script in case %folder% is not a valid path.

cd /d %folder% || exit /B 1
sohaib
  • 256
  • 1
  • 6
0

You can create a batch file and add to Task Scheduler to be executed every 2 weeks

Check this link for more details

kranz
  • 5
  • 5