Last active
July 18, 2017 02:59
-
-
Save anteriovieira/ed8fb5624f0f10ab2c4efd03abb327ed to your computer and use it in GitHub Desktop.
Scripts VBS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' Remove arquivos de logs antigos | |
On Error Resume Next | |
Set aArgs = WScript.Arguments | |
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject") | |
today = Date | |
' Informe a extensão dos arquivos que deseja | |
' remover separados por virgula. | |
' | |
' @example: log,txt,tmp | |
sExt = "log" | |
' Informe o caminho relativo para o diretório | |
' contendo os arquivos de log. | |
sRoot = "d:\diretorio\de\arquivos\logs" | |
' Informe a idade maxima do arquivo em dias | |
' | |
' @exapmles: | |
' 2 => arquivos criados a dois dias atrás | |
' -1 => arquivos criados hoje | |
nMaxFileAge = 7 | |
' Esta função será invocada quano o script | |
' for executado. | |
DeleteFiles(sRoot) | |
' Função DeleteFiles | |
' Esta função irá percorrer todos os arquivos | |
' do diretório informado e verficar se a data | |
' de criação do arquivo é superior ao a idade | |
' máxima informada, caso verdadeiro o arquivo | |
' será removido. | |
' | |
' @param {string} sFolder | |
' | |
Function DeleteFiles(ByVal sFolder) | |
Set oFolder = oFileSys.GetFolder(sFolder) | |
Set aFiles = oFolder.Files | |
Set aSubFld = oFolder.SubFolders | |
For Each file in aFiles | |
dFileCreated = FormatDateTime(file.DateCreated, "2") | |
If DateDiff("d", dFileCreated, today) > nMaxFileAge AND isLogType(file) Then | |
file.Delete(True) | |
End If | |
Next | |
' Percorre as sub pastas do diretório | |
For Each folder in aSubFld | |
DeleteFiles(folder.Path) | |
Next | |
End Function | |
' Função inArray | |
' Verifica se um determinado valor exite | |
' no array. | |
' | |
' @param {String} needle | |
' @param {Array} haystack | |
' | |
Function inArray(needle, haystack) | |
inArray = False | |
needle = trim(needle) | |
For Each hay in haystack | |
If trim(hay) = needle Then | |
inArray = True | |
Exit For | |
End If | |
Next | |
End Function | |
' Função isLogType | |
' Verifica se o arquivo é um arquivo | |
' do tipo log. | |
' | |
' @param {Object} oFile | |
' | |
Function isLogType(ByVal oFile) | |
isLogType = False | |
If inArray(oFileSys.GetExtensionName(oFile.Name), Split(sExt, ",")) Then | |
isLogType = True | |
End If | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment