Link to home
Start Free TrialLog in
Avatar of rakkad
rakkadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Saschao
Saschao

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
Avatar of Bill Prew
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
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"
Avatar of rakkad

ASKER

~bp

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

Thanks
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
Avatar of rakkad

ASKER

Thanks ~bp

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

Thanks
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
Avatar of rakkad

ASKER

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
Avatar of rakkad

ASKER

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.. :-)
SOLUTION
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 rakkad

ASKER

Hi ~bp

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

Thanks
What types of errors do you want to trap for?

~bp
Avatar of rakkad

ASKER

say if the script fails ?
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
Avatar of rakkad

ASKER

Or can you include a log file to indicate which files have been removed ?

Thanks
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