script to delete old files in server folders after a week


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


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ganesh Kumar ASr Infrastructure SpecialistCommented:
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
yo_beeDirector of Information TechnologyCommented:
If you want Powershell here is another option.
get-childitem -path '\\ftpserver\Clients' -recurse | where {$_.lastwritetime -lt (get-date).adddays(-7) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force}

Open in new window

Schedule this to run daily with a Scheduled Task

powershell.exe 'DeleteFTP.ps1'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
Why not just use a small free utility called DELAGE for this, it's a great tool, that I use often.

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

DavidfoleyAuthor Commented:
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.
yo_beeDirector of Information TechnologyCommented:
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 PrewIT / Software Engineering ConsultantCommented:
The /recurse switch with DELAGE accomplishes this.

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 ( and in various other places. the builtin find command in windows does not have such capabilities afaik
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.