Avatar of Davidfoley
Davidfoley
 asked on

script to delete old files in server folders after a week

Hi

We have an FTP server with users with multiple folders. The requirement is that users delete the old files after a week,
I am looking at doing this automatically,
Does anyone know if this can be done is power shell or a batch script, that will go to a folder or sub folders, look for files older than a week and delete them

Thanks

V
Shell ScriptingWindows Batch

Avatar of undefined
Last Comment
skullnobrains

8/22/2022 - Mon
Ganesh Anand

save as cleanup.cmd or cleanup.bat

You need to modify the line 2 to specify the top level folder.
================================================================
@echo off
set topLevel=d:\some\directory
cd /D %topLevel%
FOR /D %%a IN (*) DO (
forfiles /P "%topLevel%\%%a" /S /M *.* /D -7 /C "cmd /C del @PATH"
cd /D %topLevel%\%%a
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i">NUL
)
===============================================================
ASKER CERTIFIED SOLUTION
yo_bee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

Why not just use a small free utility called DELAGE for this, it's a great tool, that I use often.

http://www.horstmuc.de/wbat32.htm
http://www.horstmuc.de/win/delage.htm

~bp
Davidfoley

ASKER
if the directory folder names are different is there a select all command

set topLevel=d:\some\directory  

e.g   c:\ftp\ftp_users\jakeholland

where there could be a hundred different usernames, but I want to delete all files in each users directory without running the script for each user.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
yo_bee

The powershell script I supplied for you does not care about the folder structure, but the age of the file.

I am not sure if you need to be concerned with folders since you are looking for file ages.
Bill Prew

The /recurse switch with DELAGE accomplishes this.

~bp
skullnobrains

find LOCATION -not -mtime 6 -delete
deletes all the files which have not been modified in the past 7 days

if you are running windows, versions of the find command for windows are available in cygwin, in findutils (http://gnuwin32.sourceforge.net/packages/findutils.htm) and in various other places. the builtin find command in windows does not have such capabilities afaik
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.