[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

01/30/2007 at 04:43PM PST, ID: 22142993
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

VBScript to Filter Files by Last Modified Date

Asked by justjess59 in Extensible Markup Language (XML), JavaScript, Visual Basic v1.0.5.x

Tags: vbscript, date, modified, last, files

I have a VBScript that ive been working with , its shy of what I'd like to accomplish .  I would like to be able to specify in the .ini to only output files last modified after a particular date or not modified within x amount of days.  This sorts them by modified date, and can specify overall age of file inx days.  

I also have an issue with the xml output , I can and have modified the script to output to a txt file successfully. I really need to get this out in a xml format.  Some help is greatly needed as a novice who's brain is cramping and eyes crossing.   See the script and ini  below.

`Contents of DOCSCAN.VBS
-----------------------
'DocScan Report Program
'Scans folders for old files, and generates a report
'Reads a text file for options of folders to scan, file types and age

Const ForReading = 1
Dim aDirs=C:\
Dim aExts
Dim dOptionsDate=01/01/2007
Dim strSortBy
Dim intFileCount
Dim intTotalMB

Const adVarChar = 200
Const adDouble = 5
Const adInteger = 3
Const adDate = 7
Const adCurrency = 6
Const MaxCharacters = 255
strFile="options.ini"
bDebug=False
Dim log

Set oShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
'set up regular expression to use to filter file types
set oregexp=new regexp
oregexp.global=true
oregexp.ignorecase=true

'read the Options.ini file
readoptions

Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Path", adVarChar, MaxCharacters
DataList.Fields.Append "Size", adDouble, MaxCharacters
DataList.Fields.Append "Age", adInteger, MaxCharacters
DataList.Fields.Append "LastMod", adDate, MaxCharacters
DataList.Open

baseDate = Now - dOptionsDate

'loop through all folder to read files
for i=lbound(aDirs) to ubound(aDirs)
  on error resume next
  Set fl = fso.GetFolder(aDirs(i))
  If err.number *{> 0 then
    Err.Clear
  Else
    'set up regexp search pattern based on extensions from options file
    'pattern should appear something like "^[w- .]+.(com|pif|vbs|vbe|exe|bat|cmd)$"
    strpattern="^[w- .]+.("
    for j=lbound(aExts) to ubound(aExts)
       strpattern = strpattern & (aExts(j))
       if j *{> ubound(aExts) then strpattern = strpattern & "|"
    next
    strpattern = strpattern & ")$"
    if bDebug then wscript.echo strpattern
    oregexp.pattern=strpattern
    'call routine to scan folder and recurse through subfolders
    Call ScanFolder (fl)
  End If
next

'Write results to file
WriteLogFile

'If msgbox("Results are located in docscan.xml" + vbcrlf + "View results now?", 68, "DocScan Report") = 6 then
  'display results in IE
  Set objFile = FSO.GetFile(wscript.scriptfullname)
  'create an XSL file to help in the display of the output
  MakeXSLFile
  Set myIE = CreateObject("InternetExplorer.Application")
  myIE.Navigate FSO.GetParentFolderName(objFile) & "docscan.xml"
  myIE.ToolBar = False
  myIE.StatusBar = False
  myIE.AddressBar = False
  myIE.MenuBar = False
  myIE.Resizable = True
  myIE.TheaterMode = False
  myIE.Width = 650
  myIE.Height = 600
  myIE.Left = 0
  myIE.Top = 0
  myIE.Visible = True
  oShell.AppActivate("Microsoft Internet Explorer")


'**************************************************************************
Function ScanFolder(fl)
  on error resume next
  Dim sfls, sfl, f, fs
  Set fs = fl.files
  if bDebug then wscript.echo oregexp.pattern
  if bDebug then wscript.echo fl.path
  'loop through each file
  For Each f In fs
    if bDebug then wscript.echo f.name
    If f.DateLastModified *{ baseDate and oregexp.test(f.name) then
      intFileCount=intFilecount+1
      intTotalMB = intTotalMB + (f.size/1024)
      DataList.AddNew
      DataList("Path") = f.path
      DataList("Size") = f.size/1024
      DataList("Age") = now-f.DateLastModified
      DataList("LastMod") = f.DateLastModified
      DataList.Update
      err.clear
    End If
  Next
  set sfls = fl.SubFolders
  'recurse through subfolders
  For Each sfl in sfls
    ScanFolder sfl
  Next
End Function

'**************************************************************************
Sub ReadOptions()
Set objFSO = CreateObject("Scripting.FileSystemObject")
if objFSO.fileexists (strFile) then
  Set objTextFile = objFSO.OpenTextFile (strFile, ForReading)
  Do Until objTextFile.AtEndOfStream
    strTextLine = objTextFile.Readline
    select case ucase(mid(strTextLine,1,4))
      case "DIRS"
        strValue=mid(strTextLine,7,len(strTextLine)-7)
        aDirs=split(strValue,";")
      case "EXTS"
        strValue=mid(strTextLine,7,len(strTextLine)-7)
        aExts=split(strValue,";")
      case "AGE="
        dOptionsDate=mid(strTextLine,5)
      case "SORT"
        strSortBy=mid(strTextLine,7,len(strTextLine)-7)
    end select
  Loop
  objTextFile.Close
else
  'no options file - abort
  'kill message box
  retval=oShell.AppActivate("** DocScan Report **")
  if retval then oShell.SendKeys "{Enter}"

  wscript.echo "DocScan Error #1 - no options.ini file found.  Program aborted."
  wscript.quit(1)
end if

if bDebug then
  strmsg="DIRS" & vbcrlf
  for i=lbound(aDirs) to ubound(aDirs)
    strmsg=strmsg & vbtab & aDirs(i) & vbcrlf
  next
  strmsg=strmsg & "EXTS" & vbcrlf
  for i=lbound(aExts) to ubound(aExts)
    strmsg=strmsg & vbtab & aExts(i) & vbcrlf
  next
  strmsg=strmsg & "Date=" & dOptionsDate & vbcrlf
  strmsg=strmsg & "Sort By=" & strSortBy & vbcrlf
  wscript.echo strmsg
end if
End Sub

'**************************************************************************
Sub WriteLogFile()
'open output file to store results
Set log = fso.CreateTextFile("docscan.xml", true)
If Err.Number *{> 0 then
  oShell.Popup "DocScan Error #2 - Cannot Create docscan.xml File: " & Err.Description
  Wscript.Quit(2)
end if

'write header and title info into output file
log.WriteLine "*{?xml version=" & chr(34) & "1.0" & chr(34) & "?>"
log.WriteLine "*{?xml-stylesheet type=" & chr(34) & "text/xsl" & chr(34) & " href=" & chr(34) & "docscan.xsl" & chr(34) & "?>"
log.WriteLine "*{Report>"
log.WriteLine "*{TitleLine1>DocScan Report - Files Older than " & dOptionsDate & " days.*{/TitleLine1>"
log.WriteLine "*{TitleLine2>(As of: " & Date & " @ " & Time & ")*{/TitleLine2>"
log.WriteLine "*{Files>"

DataList.Sort = strSortBy
DataList.MoveFirst
Do Until DataList.EOF
  log.WriteLine "*{File>"
  log.WriteLine "*{FileName>" & DataList.Fields.Item("Path") & "*{/FileName>" & _
      "*{Size>" & formatnumber(DataList.Fields.Item("Size"),1,0,0,-1) & "*{/Size>" & _
      "*{Age>" & formatnumber(DataList.Fields.Item("Age"),0,0,0,-1) & "*{/Age>" & _
      "*{LastMod>" & DataList.Fields.Item("LastMod") & "*{/LastMod>"
  log.WriteLine "*{/File>"
  DataList.MoveNext
Loop


'write trailing data to output file
log.WriteLine "*{/Files>"
log.WriteLine "*{Summary>Total files: " & formatnumber(intFileCount,0,0,0,-1) & " - using " & formatnumber(intTotalMB,1,0,0,-1) & "KB *{/Summary>"
log.WriteLine "*{/Report>"

log.close

End Sub

'**************************************************************************
Sub MakeXSLFile()
  Set xsl = fso.CreateTextFile("docscan.xsl", true)
  xsl.writeline "*{xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0' >"
  xsl.writeline "*{xsl:template match='Report'>"
  xsl.writeline "*{HTML>"
  xsl.writeline "   *{BODY> "
  xsl.writeline "      *{INPUT id='PrintMe' name='PrintMe' onclick='javascript:window.print()' type='button' value='Print'/>"
  xsl.writeline "      *{INPUT id='CloseMe' name='CloseMe' onclick='javascript:window.close()' type='button' value='Close Window'/>"
  xsl.writeline "      *{P style='color: blue; font-size: 16pt; margin-top: 0pt; margin-bottom: 0pt'>*{xsl:value-of select='TitleLine1'/>*{/P>"
  xsl.writeline "      *{P style='color: blue; font-size: 16pt; margin-top: 0pt; margin-bottom: 0pt'>*{xsl:value-of select='TitleLine2'/>*{/P>"
  xsl.writeline "      *{TABLE border='1' style='table-layout: fixed;border: medium none' cellpadding='0' cellspacing='0' width='300'> "
  xsl.writeline "         *{TR bgcolor='yellow'>"
  xsl.writeline "            *{TD width='320' style='font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>File*{/TD>"
  xsl.writeline "            *{TD width='70' align='right' style='font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>Size(KB)*{/TD>"
  xsl.writeline "            *{TD width='50' align='right' style='font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>Age(Days)*{/TD>"
  xsl.writeline "            *{TD width='150' style='font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>Last Modified*{/TD>"
  xsl.writeline "         *{/TR>"
  xsl.writeline "         *{xsl:for-each select='Files/File'>"
  xsl.writeline "            *{TR>   "
  xsl.writeline "               *{TD size='1' style='font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>*{xsl:value-of select='FileName'/>*{/TD>"
  xsl.writeline "               *{TD align='right' style='font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>*{xsl:value-of select='Size'/>*{/TD>"
  xsl.writeline "               *{TD align='right' style='color: red; font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>*{xsl:value-of select='Age'/>*{/TD>"
  xsl.writeline "               *{TD style='font-size: 10pt; padding-right: 5pt; padding-left: 5pt'>*{xsl:value-of select='LastMod'/>*{/TD>"
  xsl.writeline "            *{/TR>"
  xsl.writeline "         *{/xsl:for-each>"
  xsl.writeline "      *{/TABLE>"
  xsl.writeline "      *{P style='color: red; font-size: 16pt'>*{xsl:value-of select='Summary'/>*{/P>"
  xsl.writeline "   *{/BODY>"
  xsl.writeline "*{/HTML>"
  xsl.writeline "*{/xsl:template>"
  xsl.writeline "*{/xsl:stylesheet>"
  xsl.close
End Sub
 

`Contents of OPTIONS.INI
-----------------------
'dirs identifies folder to scan. Scan is done recursively through all sub folders
dirs="c:"

'exts is used to filter only these files for display
exts="doc;xls;ppt;nsf;txt;dat;inf;exe;gif;ini;tmp"

'the age setting limits the files displayed to be this age or older
age=180

'valid sort values are age, path, size or lastmod
sort="lastmod"

 

[+][-]01/30/07 08:34 PM, ID: 18433583

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01/31/07 01:33 AM, ID: 18434535

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Extensible Markup Language (XML), JavaScript, Visual Basic v1.0.5.x
Tags: vbscript, date, modified, last, files
Sign Up Now!
Solution Provided By: Zvonko
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01/31/07 01:36 AM, ID: 18434549

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090824-EE-VQP-74