Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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
0
rakkad
Asked:
rakkad
2 Solutions
 
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 KhawajaCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now