Announcement

Collapse
No announcement yet.

VBSscript - Enumerates unwanted files but ignore/exclude RECYCLER folder

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBSscript - Enumerates unwanted files but ignore/exclude RECYCLER folder

    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?

    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("\\server1\folder1", _
                          "\\server2\folder2")
    
       LogFolder = "\\server1\logs\check_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 "<a href='" & Dir & "\" & FileName & "' target='_blank'>" &  Dir & "\" & FileName & "</a><br>"
       Else
          mLogFile.WriteLine "<a href='" & Dir & "\" & Archive  & "' target='_blank'>" &  Dir & "\" & FileName & " (" & Archive & ")</a><br>"
       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

  • #2
    Re: VBSscript - Enumerates unwanted files but ignore/exclude RECYCLER folder

    A simple way is to exclude the folder by its expected name;
    Code:
    <snip>
       For Each oSubFolder In colFolders
         If not UCASE(oSubFolder.name) = "RECYCLER" Then
           Set colFiles = oSubFolder.Files
           ScanFolder(oSubFolder)
         End If
       Next
    Or,
    If you planned to run the script as a user log-on script, then it can determine the user's SID so you are able to exclude the users's own {subfolder} in the Recycler folder. Or you can read the exact name and location of the RECYCLER folder from registry, in:
    HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Explorer\BitBucket\@
    (where the key @ is used only when there is just one Recycler for all local hdds)

    And other possibility can be to exclude all folders assigned the 'system' attribute.
    http://bree.teisa.unican.es/caspdoc/...s_property.htm

    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment

    Working...
    X