Solved

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

Posted on 2014-09-12
18
268 Views
Last Modified: 2014-10-01
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
Comment
Question by:rakkad
18 Comments
 
LVL 4

Expert Comment

by:Saschao
ID: 40320261
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40320416
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
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 40320485
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:rakkad
ID: 40320622
~bp

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

Thanks
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40321344
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
 

Author Comment

by:rakkad
ID: 40322056
Thanks ~bp

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

Thanks
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40322993
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
 

Author Comment

by:rakkad
ID: 40331486
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
 

Author Comment

by:rakkad
ID: 40331513
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
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 40343132
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
 

Author Comment

by:rakkad
ID: 40345595
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40351386
What types of errors do you want to trap for?

~bp
0
 

Author Comment

by:rakkad
ID: 40351533
say if the script fails ?
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40352842
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
 

Author Comment

by:rakkad
ID: 40355398
Or can you include a log file to indicate which files have been removed ?

Thanks
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40356174
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question