Link to home
Start Free TrialLog in
Avatar of Damarado
Damarado

asked on

Script to delete contents of sub folders after a week

Hi

I want  a script that will delete the content of users sub folders, after 7 days, on a windows 2012 server.It will need to go through the list of users and delete any content older that 7 days in the sub folders. The list is based on the main folder name.

eg     main user   folder   bcotton
         sub folders              bcotton-a1
                                           bcotton-c2
                                           bcotton-c3
                                           bocttton-c4
                                           bcotton-c5

This is the one I have below, but the problem is that when I run it it is also deleting new folders in the sub account folders, less that 7 days old.

@echo off
set UserList=H:\users.txt
set topLevel=H:\ftpusers\ftp_users
for /f "delims=" %%u in ('type "%UserList%"') do (
      cd /D "%topLevel%\%%~u"
      FOR /D %%a IN (*) DO (
            forfiles /P "%topLevel%\%%~u\%%a" /S /M *.* /D -7 /C "cmd /C  del @PATH"
            cd /D "%topLevel%\%%~u\%%a"
            for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do  rd "%%i">NUL
      )
)

How do i alter to have it only look at files in the sub folders older than a week and not delete any new folders in the users subfolders.
Avatar of NVIT
NVIT
Flag of United States of America image

> ...it is also deleting new folders in the sub account folders, less that 7 days old.
Do these new folders have file older than 7 days? If so, do you want those files deleted?
Avatar of Bill Prew
Bill Prew

What would be the correct criteria to remove a folder?  Simply stated, you probably want to delete any folder that is (1) empty, and (2) was created more than 7 days ago.  Is that true?  If so the following VBS script should do that.

As always test thoroughly all situations.

' Specify base folder to purge, and age in days of files to keep
Const strBaseDir = "C:\Temp"
Const intAgeInDays = 7

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Remove old files recursively
RemoveOldFiles objFSO.GetFolder(strBaseDir)

Sub RemoveOldFiles(objFolder)
    On Error Resume Next

    ' If no files of subfolders exist on first check, leave it alone
    If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
        Exit Sub
    End If

    ' Remove any files older than specified days from this folder
    For Each objFile In objFolder.Files
        If DateDiff("d", objFile.DateLastModified, Now) > intAgeInDays Then
            objFile.Delete
        End If
    Next

    ' See if we were able to access this folder, if not don't recurse into it
    If Err.Number = 0 Then
        ' Remove all older files in any subfolders of this one
        For Each objSubFolder In objFolder.Subfolders
            RemoveOldFiles objSubFolder
        Next
    End If

    ' If folder is now empty, we purged its contents, so remove it if created more than required days ago
    If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
        If DateDiff("d", objFolder.DateCreated, Now) > intAgeInDays Then
            objFolder.Delete
        End If
    End If
End Sub

Open in new window

~bp
Avatar of Damarado

ASKER

to answer your question

If there is a new folder in the sub account folders(created less that 7 days) then I don't to delete the files.

How do I alter to achieve this?
Bill

I tested  the vbs script and its works. One question, is there any way of preventing it deleting content in a folder that is not one of the B1 -B5 folders, but is in the users main folder.

for e.g.

   sub folders              bcotton-a1
                                           bcotton-c2
                                           bcotton-c3
                                           bocttton-c4
                                           bcotton-c5
                                           mystuff

for example a user had put in folder called "my stuff" and I didnt want to delete the contents of this folder, even if the content is over the 7 days.
One question, is there any way of preventing it deleting content in a folder that is not one of the B1 -B5 folders, but is in the users main folder.
You mention B1-B5, but your example doesn't show any folder with a Bx in the name?  So not sure what you mean, or what the rules would be not to delete a folder.  If we can state it in a general way that could be applied to all folders to only look at the certain folders you want then we can script it.

~bp
The root folders follow the have the username, then the five sub folders,

eg jdavis,    jdavis-a1   -   jdavis-a5

any folder and files in the sub folders older than 7 days should be deleted, If a folder is in the main folder but but one of the sub folders for the users it doesn't get deleted.

In the original I had it go through a user list and check the sub folders.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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