Link to home
Start Free TrialLog in
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
Avatar of Ganesh Anand
Ganesh Anand
Flag of Bahrain image

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
Avatar of yo_bee
yo_bee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
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
Avatar of 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.
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.
The /recurse switch with DELAGE accomplishes this.

~bp
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