I have seen some posts to delete all the files (not folders) in a specific folder, but I simply don't understand them.
I need to use a UNC path and delete all the files that are older than 7 days.
Mypath = \\files\data\APIArchiveFolder\
Does someone have quick script that they can specifically input the path above into that would delete all files older than 7 days?
You can free up space and keep things organized by only deleting files that are older than a certain number of days in any folder — here's how to do it. To delete files older than 30 days on Windows 10, use the “ForFiles” command. The command is: ForFiles /p “C:\path\to\folder” /s /d -30 /c “cmd /c del /q @file”.
This code removes files in the current working directory that were created >= 7 days ago. Run at your own risk.
import os
import time
current_time = time.time()
for f in os.listdir():
creation_time = os.path.getctime(f)
if (current_time - creation_time) // (24 * 3600) >= 7:
os.unlink(f)
print('{} removed'.format(f))
Another version:
import os
import time
import sys
if len(sys.argv) != 2:
print "usage", sys.argv[0], " <dir>"
sys.exit(1)
workdir = sys.argv[1]
now = time.time()
old = now - 7 * 24 * 60 * 60
for f in os.listdir(workdir):
path = os.path.join(workdir, f)
if os.path.isfile(path):
stat = os.stat(path)
if stat.st_ctime < old:
print "removing: ", path
# os.remove(path) # uncomment when you will sure :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With