17 May 2010

Elimina file più vecchi di n giorni con ricorsione - BosoCleanDir.vbs / Delete old files

NOTE:
- impostare i parametri
- la riga che elimina i file è remmata

HACK:
- ottenere i parametri da commandline.

BosoCleanDir.vbs

' -- 2015-10-31 - aggiunto qualche echo()

Option Explicit

'------------------------------------------------------------------------------

' -- PARAMETRI
const elencoCartelle = "c:\test\1,c:\test\bla bla 2"
const maxGiorni = 30

'------------------------------------------------------------------------------

sub eliminaFile(wPath, oggi)
 
 if trim(wpath) = "" then exit sub
 
 with wscript
  .echo
  .echo "**** Processing directory: " & wpath
 end with

 dim wFileSystem, wFolder, wSubFolders, f, file
 set wFileSystem = createobject("Scripting.FileSystemObject")
 set wFolder = wFileSystem.getfolder(wPath)
 
 ' -- ELIMINA TUTTI I FILE PIU' VECCHI DI [maxGiorni] GIORNI
 for each file in wFolder.Files
  if DateDiff("d", file.DateCreated, oggi) > maxGiorni then
   wscript.echo "Deleting " & file.name
'   file.delete(true)
  end if
 next
 
 ' -- ELIMINA I FILES NELLE SOTTOCARTELLE
 dim subFolder
 for each subFolder in wFolder.SubFolders
  eliminaFile subFolder, oggi
 next

 set file = nothing
 set wFileSystem = nothing
 set wFolder = nothing
 Set wSubFolders = nothing
 
end sub

'------------------------------------------------------------------------------

sub Main()
 
 ' -- LOOP SULL'ELENCO DELLE CARTELLE
 dim arrFolder
 arrFolder = Split(elencoCartelle, ",")

 dim oggi
 oggi = now
 
 dim riga
 riga =  string(80, "*")
 
 with wscript
  .echo
  .echo riga
  .echo "* DELETING FILES OLDER THAN " & maxGiorni & " DAYS"
  .echo riga

  dim cartella
  for each cartella in arrFolder
   eliminaFile cartella, oggi
  next

  .echo
  .echo "** DONE"
 end with

end sub

'------------------------------------------------------------------------------

Main



Versione alternativa, senza output


Questa versione pulisce i log di IIS:
sLogFolder = "c:\inetpub\logs\LogFiles"
iMaxAge = 30   'in days

Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)

For Each colSubfolder in colFolder.SubFolders
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files

        For Each objFile in colFiles
                iFileAge = now-objFile.DateCreated
                if iFileAge > (iMaxAge+1)  then
                        objFSO.deletefile objFile, True
                end if
        Next
Next

Via.