vbs script to delete folders/files which are present for more than 30 days

We have log files which occur daily in the format c:\test\yyyy\mm\dd date format in one of our systems

However, I need to delete previous months folders/files so the most recent files/folders are present

Thanks
rakkadAsked:
Who is Participating?
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.

SaschaoCommented:
Hi, here is some script I am using, just translated from german:

this is removing all zip files older than 6 days from this d:\backup folder.

--
    Option Explicit
    Dim intNumber
    Dim strExtension
    Dim intDays
    Dim objFile
    Dim objFSO
    Dim objFolder
    Dim strFolder
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strFolder = "D:\Backup\"
    strExtension = "zip"
    intDays = 6
     
    Set objFolder = objFSO.GetFolder(strFolder)
    intNumber = 0
    For Each objFile In objFolder.Files
        If LCase(Right(objFile.name, Len(strExtension))) = LCase(strExtension) _
            And DateDiff("d", objFile.DateLastModified, Now) > intDays Then
            objFile.Delete
            intNumber = intNumber + 1
        End If
    Next

Open in new window

--

Sascha
0
Bill PrewCommented:
So, do you want to delete all days and the month folder where the month is less than the current month?

Or do you need to keep say exactly 30 days of folders, and delete back from there?

~bp
0
Mohammed KhawajaManager - Infrastructure:  Information TechnologyCommented:
You don't need a VBScript, you could use forfiles command.  The command could be as simple as below:

forfiles -p "C:\what\ever" -s -m *.* -d 30 -c "cmd /c del @path"
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

rakkadAuthor Commented:
~bp

Yes I want to delete all files where the month is less than the current month

Thanks
0
Bill PrewCommented:
Here's a BAT script approach, which does what you described.  Run it from the command prompt.  Currently the RD command will just ECHO to the screen to allow testing so you can validate it works.  If the folders shown look right then remove the word ECHO before the RD command and run for real.

Keep in mind all days folders and files in that month folder will be removed.

@echo off
setlocal EnableDelayedExpansion

REM Define base folder for log folders
set BaseDir=B:\EE\EE28517538\test

REM Get current Month in YYYYMM format
set CurrentMonth=%DATE:~-4%%DATE:~-10,2%
set CurrentMonth=%CurrentMonth: =0%

REM Get month folders, and if older than current month remove them
for /d %%Y in ("%BaseDir%\????") do (
  set YYYY=%%~nY
  for /d %%M in ("%%~Y\??") do (
    set FolderMonth=%%~nY%%~nM
    if !FolderMonth! LSS %CurrentMonth% (
      ECHO rd /q /s "%%~M"
    )
  )
)

Open in new window

~bp
0
rakkadAuthor Commented:
Thanks ~bp

Do you have this as a vbscript format
Can you describe how the script works ?

Thanks
0
Bill PrewCommented:
Here is a VBS version. I commented clearly to explain what it is doing.  Currently the actual DeleteFolder() calls are commented out, so it will just display the folders it would delete to the screen.  Give it some testing and if it looks good uncomment those lines.

' Specify base location to purge old backups from
' Folders to purge should be subfolders under this one with YYYY\MM\DD format names
sBaseDir = "B:\EE\EE28517538\test"

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

' Get todays year and month for checks below
iCurrentYear = Year(Date)
iCurrentMonth = Month(Date)

' Process the first level folders under the base folder
For Each oYearFolder In oFSO.GetFolder(sBaseDir).SubFolders
   ' Make sure the folder name is YYYY (length of 4 and numeric)
   If Len(oYearFolder.Name) = 4 And IsNumeric(oYearFolder.Name) Then
      ' If the year of these backups is older than current year, remove them 
      If Cint(oYearFolder.Name) < iCurrentYear Then
         Wscript.Echo "Removing year folder: " & oYearFolder.Path
         ' oFSO.DeleteFolder(oYearFolder.Path)
      End If
      ' If the year of these backups is same as current year, need to check month subfolders
      If Cint(oYearFolder.Name) = iCurrentYear Then
         For Each oMonthFolder In oYearFolder.SubFolders
            ' Make sure the folder name is MM (length of 2 and numeric)
            If Len(oMonthFolder.Name) = 2 And IsNumeric(oMonthFolder.Name) Then
               ' If the month of these backups is older than current month, remove them 
               If Cint(oMonthFolder.Name) < iCurrentMonth Then
                  Wscript.Echo "Removing month folder: " & oMonthFolder.Path
                  ' oFSO.DeleteFolder(oMonthFolder.Path)
               End If
            End If
         Next
      End If
   End If
Next

Open in new window

~bp
0

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
rakkadAuthor Commented:
the vbscript works brilliant, I would like to include some error trapping, i.e. if the code fails can you include any error trapping messages

Thanks
0
rakkadAuthor Commented:
I need to modify the script yet again, so... in addition to removing the month folder which is not the current month, I would also like to remove dd folders within the current month so the most recent dd folder is present

thanks once again.. :-)
0
Bill PrewCommented:
Give this a try to handle the day folders before the current day.

' Specify base location to purge old backups from
' Folders to purge should be subfolders under this one with YYYY\MM\DD format names
sBaseDir = "B:\EE\EE28517538\test"

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

' Get todays year and month for checks below
iCurrentYear = Year(Date)
iCurrentMonth = Month(Date)
iCurrentDay = Day(Date)

' Process the first level folders under the base folder
For Each oYearFolder In oFSO.GetFolder(sBaseDir).SubFolders
   ' Make sure the folder name is YYYY (length of 4 and numeric)
   If Len(oYearFolder.Name) = 4 And IsNumeric(oYearFolder.Name) Then
      ' If the year of these backups is older than current year, remove them 
      If Cint(oYearFolder.Name) < iCurrentYear Then
         Wscript.Echo "Removing year folder: " & oYearFolder.Path
         ' oFSO.DeleteFolder(oYearFolder.Path)
      End If
      ' If the year of these backups is same as current year, need to check month subfolders
      If Cint(oYearFolder.Name) = iCurrentYear Then
         For Each oMonthFolder In oYearFolder.SubFolders
            ' Make sure the folder name is MM (length of 2 and numeric)
            If Len(oMonthFolder.Name) = 2 And IsNumeric(oMonthFolder.Name) Then
               ' If the month of these backups is older than current month, remove them 
               If Cint(oMonthFolder.Name) < iCurrentMonth Then
                  Wscript.Echo "Removing month folder: " & oMonthFolder.Path
                  ' oFSO.DeleteFolder(oMonthFolder.Path)
               End If
              ' If the month of these backups is same as current month, need to check day subfolders
               If Cint(oMonthFolder.Name) = iCurrentMonth Then
                  For Each oDayFolder In oMonthFolder.SubFolders
                     ' Make sure the folder name is DD (length of 2 and numeric)
                     If Len(oDayFolder.Name) = 2 And IsNumeric(oDayFolder.Name) Then
                        ' If the day of these backups is older than current day, remove them 
                        If Cint(oDayFolder.Name) < iCurrentDay Then
                           Wscript.Echo "Removing day folder: " & oDayFolder.Path
                           ' oFSO.DeleteFolder(oDayFolder.Path)
                        End If
                     End If
                  Next
               End If
            End If
         Next
      End If
   End If
Next

Open in new window

~bp
0
rakkadAuthor Commented:
Hi ~bp

I would like to include some error trapping, i.e. if the code fails can you include any error trapping messages

Thanks
0
Bill PrewCommented:
What types of errors do you want to trap for?

~bp
0
rakkadAuthor Commented:
say if the script fails ?
0
Bill PrewCommented:
Well, if the script itself fails, this it would not be able to trap that condition, you would have to trap that in whatever launched the script initially.

~bp
0
rakkadAuthor Commented:
Or can you include a log file to indicate which files have been removed ?

Thanks
0
Bill PrewCommented:
Simple way to get that (since I was already writing the folders removed to standard output) would be to send the output from the script to a file when you run it, something like this:

cscript //nologo yourscript.vbs >c:\logs\yourscript.txt

~bp
0
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
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.