VBSscript – Enumerates unwanted files but ignore/exclude RECYCLER folder

Home Forums Scripting General Scripting VBSscript – Enumerates unwanted files but ignore/exclude RECYCLER folder

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    nebukadnezar
    Member
    #129764

    Hello, I have following script, it scans some unwanted files format, like mp3, avi from user’s homedirs on a file server. The output is an html file. But in the result is the folder RECYCLER listed in, is it possible to exclude or ignore this folder from the search?

    Option Explicit
    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    Dim arrFolders, strFolder, LogFolder
    Dim LogFileName
    Dim oFSO, oFolder, oShell
    Dim mLogFile

    ‘ define an array of the folders whose subfolder tree is to be searched…
    ‘ NOTE: This could also be done dynamically by reading in a text file containing
    ‘ the folder names at run time.

    arrFolders = Array(“\server1folder1”, _
    “\server2folder2”)

    LogFolder = “\server1logscheck_unwanted_files”

    Set oShell = WScript.CreateObject(“WScript.Shell”)
    Set oFSO = CreateObject(“Scripting.FileSystemObject”)

    SetLogFileName

    For Each strFolder In arrFolders
    wscript.echo “Searching tree of: ” & strFolder
    Set oFolder = oFSO.GetFolder(strFolder)
    ScanFolder(oFolder)
    Next

    mLogFile.Close

    ‘——————————————————————-
    Sub ScanFolder(oFolder)
    ‘ This is a recursive routine to scan a folder and all its subfolders for
    ‘ filenames matching certain criteria…
    Dim Dir, FileName
    Dim colFiles, colFolders
    Dim oSubFolder, oFile

    Dir = oFolder.Path

    Set colFiles = oFolder.Files
    Set colFolders = oFolder.SubFolders

    For Each oFile In colFiles
    FileName = LCase(oFile.Name)
    If LogThisFile(FileName) Then
    FileLogFile Dir, FileName, “”
    End If

    If InStr(FileName, “.zip”) Then
    HandleZipArchive Dir, FileName
    End If
    Next

    For Each oSubFolder In colFolders
    Set colFiles = oSubFolder.Files
    ScanFolder(oSubFolder)
    Next
    End Sub

    ‘——————————————————————-
    Sub SetLogFileName
    Dim myDate, myWeekDay, myDay, myMonth, myYear, logs

    myDate = Date()
    myWeekday = Weekday(myDate)
    myDay = Day(myDate)
    myMonth = Month(myDate)
    myYear = Year(myDate)

    Select Case myWeekday
    Case “1” : logs = “Sunday ”
    Case “2” : logs = “Monday ”
    Case “3” : logs = “Tuesday ”
    Case “4” : logs = “Wednesday ”
    Case “5” : logs = “Thursday ”
    Case “6” : logs = “Friday ”
    Case “7” : logs = “Saturday ”
    End Select

    logs = logs & myDay & “-” & myMonth & “-” & myYear
    LogFileName = LogFolder & logs & “.htm”
    Set mLogFile = oFSO.OpenTextFile(LogFileName, ForAppending, True)
    End Sub

    ‘——————————————————————-
    Sub FileLogFile(Dir, FileName, Archive)
    If Archive = “” Then
    mLogFile.WriteLine “” & Dir & “” & FileName & “

    Else
    mLogFile.WriteLine “” & Dir & “” & FileName & ” (” & Archive & “)

    End If
    End Sub

    ‘——————————————————————-
    Sub HandleZipArchive(Dir, FileName)

    ‘ NOTE: This routine uses UNZIP.EXE v5.52 from Info-ZIP It’s freely available from
    http://www.info-zip.org/pub/infozip/UnZip.html
    ‘ The correct filename to download is: unz552xN.exe

    ‘ Executing UNZIP below will produce an output file with contents similar to the following.
    ‘ In order to find the filename(s) we will read each line and look for “:” in the Time field.
    ‘ When we find it we will then take the substring starting 6 positions to the right of the “:” and
    ‘ ending with the end of the line. After removing leading & trailing spaces, we will have the
    ‘ filename. Last, we will examine the name for having one of the target extensions we’re looking
    ‘ for.

    ‘ Length Date Time Name
    ‘ ——– —- —- —-
    ‘ 6232 01/14/02 11:11 InString.cls
    ‘ 774 08/25/02 17:13 @PSC_ReadMe_30761_1.txt
    ‘ ——– ——-
    ‘ 7006 2 files

    Dim ArchDir
    Dim oFile
    Dim LineIn, nPos, fn, command

    ArchDir = LogFolder & “ArchDir.txt”
    command = “cmd /c unzip -lq ” & Chr(34) & Dir & “” & FileName & Chr(34) & ” 2>1>”& Chr(34) & ArchDir & Chr(34)

    wscript.echo command
    oShell.Run command, 0, True

    Set oFile = oFSO.OpenTextFile(ArchDir)

    Do Until oFile.AtEndOfStream
    LineIn = oFile.ReadLine
    nPos = InStr(LineIn, “:”)
    If nPos > 0 Then
    fn = Trim(Mid(LineIn, nPos+6))
    If LogThisFile(fn) Then
    FileLogFile Dir, fn, FileName
    End If
    End If
    Loop

    oFile.Close
    Set oFile = Nothing
    oFSO.DeleteFile(ArchDir)
    End Sub

    ‘——————————————————————-
    Function LogThisFile(FileName)
    Dim fn

    LogThisFile = False
    fn = LCase(FileName)

    If InStr(fn, “.avi”) _
    Or InStr(fn, “.jpeg”) _
    Or InStr(fn, “.tif”) _
    Or InStr(fn, “.mpeg”) _
    Or InStr(fn, “.mpg”) _
    Or InStr(fn, “.mp3”) _
    Or InStr(fn, “.mp4”) _
    Or InStr(fn, “.wmv”) Then
    LogThisFile = True
    End If

    ‘ COMMENT: The following code is an alternative to that just above. This logic might execute
    ‘ quicker but it won’t find files named like “HideThisFile.mp3.disguised” whereas
    ‘ the first code will. (However, the first code would also report “standard.common.txt”
    ‘ as a file of interest, too.)

    ‘ fn = LCase(Right(FileName,4))

    ‘ If fn = “.exe” _
    ‘ Or fn = “.com” _
    ‘ Or fn = “.pif” _
    ‘ Or fn = “.bat” _
    ‘ Or fn = “.mp3” _
    ‘ Or fn = “.mp4” _
    ‘ Or fn = “.swf” Then
    ‘ LogThisFile = True
    ‘ End If

    End Function

    [/CODE][CODE]

    Option Explicit
    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    Dim arrFolders, strFolder, LogFolder
    Dim LogFileName
    Dim oFSO, oFolder, oShell
    Dim mLogFile

    ‘ define an array of the folders whose subfolder tree is to be searched…
    ‘ NOTE: This could also be done dynamically by reading in a text file containing
    ‘ the folder names at run time.

    arrFolders = Array(“\server1folder1”, _
    “\server2folder2”)

    LogFolder = “\server1logscheck_unwanted_files”

    Set oShell = WScript.CreateObject(“WScript.Shell”)
    Set oFSO = CreateObject(“Scripting.FileSystemObject”)

    SetLogFileName

    For Each strFolder In arrFolders
    wscript.echo “Searching tree of: ” & strFolder
    Set oFolder = oFSO.GetFolder(strFolder)
    ScanFolder(oFolder)
    Next

    mLogFile.Close


    Sub ScanFolder(oFolder)
    ‘ This is a recursive routine to scan a folder and all its subfolders for
    ‘ filenames matching certain criteria…
    Dim Dir, FileName
    Dim colFiles, colFolders
    Dim oSubFolder, oFile

    Dir = oFolder.Path

    Set colFiles = oFolder.Files
    Set colFolders = oFolder.SubFolders

    For Each oFile In colFiles
    FileName = LCase(oFile.Name)
    If LogThisFile(FileName) Then
    FileLogFile Dir, FileName, “”
    End If

    If InStr(FileName, “.zip”) Then
    HandleZipArchive Dir, FileName
    End If
    Next

    For Each oSubFolder In colFolders
    Set colFiles = oSubFolder.Files
    ScanFolder(oSubFolder)
    Next
    End Sub


    Sub SetLogFileName
    Dim myDate, myWeekDay, myDay, myMonth, myYear, logs

    myDate = Date()
    myWeekday = Weekday(myDate)
    myDay = Day(myDate)
    myMonth = Month(myDate)
    myYear = Year(myDate)

    Select Case myWeekday
    Case “1” : logs = “Sunday “
    Case “2” : logs = “Monday “
    Case “3” : logs = “Tuesday “
    Case “4” : logs = “Wednesday “
    Case “5” : logs = “Thursday “
    Case “6” : logs = “Friday “
    Case “7” : logs = “Saturday “
    End Select

    logs = logs & myDay & “-” & myMonth & “-” & myYear
    LogFileName = LogFolder & logs & “.htm”
    Set mLogFile = oFSO.OpenTextFile(LogFileName, ForAppending, True)
    End Sub


    Sub FileLogFile(Dir, FileName, Archive)
    If Archive = “” Then
    mLogFile.WriteLine “” & Dir & “” & FileName & “

    Else
    mLogFile.WriteLine “” & Dir & “” & FileName & ” (” & Archive & “)

    End If
    End Sub


    Sub HandleZipArchive(Dir, FileName)

    ‘ NOTE: This routine uses UNZIP.EXE v5.52 from Info-ZIP It’s freely available from
    http://www.info-zip.org/pub/infozip/UnZip.html
    ‘ The correct filename to download is: unz552xN.exe

    ‘ Executing UNZIP below will produce an output file with contents similar to the following.
    ‘ In order to find the filename(s) we will read each line and look for “:” in the Time field.
    ‘ When we find it we will then take the substring starting 6 positions to the right of the “:” and
    ‘ ending with the end of the line. After removing leading & trailing spaces, we will have the
    ‘ filename. Last, we will examine the name for having one of the target extensions we’re looking
    ‘ for.

    ‘ Length Date Time Name


    —- —- —-
    ‘ 6232 01/14/02 11:11 InString.cls
    ‘ 774 08/25/02 17:13 @PSC_ReadMe_30761_1.txt



    ‘ 7006 2 files

    Dim ArchDir
    Dim oFile
    Dim LineIn, nPos, fn, command

    ArchDir = LogFolder & “ArchDir.txt”
    command = “cmd /c unzip -lq ” & Chr(34) & Dir & “” & FileName & Chr(34) & ” 2>1>”& Chr(34) & ArchDir & Chr(34)

    wscript.echo command
    oShell.Run command, 0, True

    Set oFile = oFSO.OpenTextFile(ArchDir)

    Do Until oFile.AtEndOfStream
    LineIn = oFile.ReadLine
    nPos = InStr(LineIn, “:”)
    If nPos > 0 Then
    fn = Trim(Mid(LineIn, nPos+6))
    If LogThisFile(fn) Then
    FileLogFile Dir, fn, FileName
    End If
    End If
    Loop

    oFile.Close
    Set oFile = Nothing
    oFSO.DeleteFile(ArchDir)
    End Sub


    Function LogThisFile(FileName)
    Dim fn

    LogThisFile = False
    fn = LCase(FileName)

    If InStr(fn, “.avi”) _
    Or InStr(fn, “.jpeg”) _
    Or InStr(fn, “.tif”) _
    Or InStr(fn, “.mpeg”) _
    Or InStr(fn, “.mpg”) _
    Or InStr(fn, “.mp3”) _
    Or InStr(fn, “.mp4”) _
    Or InStr(fn, “.wmv”) Then
    LogThisFile = True
    End If

    ‘ COMMENT: The following code is an alternative to that just above. This logic might execute
    ‘ quicker but it won’t find files named like “HideThisFile.mp3.disguised” whereas
    ‘ the first code will. (However, the first code would also report “standard.common.txt”
    ‘ as a file of interest, too.)

    ‘ fn = LCase(Right(FileName,4))

    ‘ If fn = “.exe” _
    ‘ Or fn = “.com” _
    ‘ Or fn = “.pif” _
    ‘ Or fn = “.bat” _
    ‘ Or fn = “.mp3” _
    ‘ Or fn = “.mp4” _
    ‘ Or fn = “.swf” Then
    ‘ LogThisFile = True
    ‘ End If

    End Function

    [/CODE]

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.