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("WScr
ipt.Shell"
)
Set fso = CreateObject("Scripting.Fi
leSystemOb
ject")
'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.Records
et")
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|b
at|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.script
fullname)
'create an XSL file to help in the display of the output
MakeXSLFile
Set myIE = CreateObject("InternetExpl
orer.Appli
cation")
myIE.Navigate FSO.GetParentFolderName(ob
jFile) & "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("Micros
oft 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.Fi
leSystemOb
ject")
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(strTe
xtLine)-7)
aDirs=split(strValue,";")
case "EXTS"
strValue=mid(strTextLine,7
,len(strTe
xtLine)-7)
aExts=split(strValue,";")
case "AGE="
dOptionsDate=mid(strTextLi
ne,5)
case "SORT"
strSortBy=mid(strTextLine,
7,len(strT
extLine)-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("docsca
n.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.Fiel
ds.Item("S
ize"),1,0,
0,-1) & "*{/Size>" & _
"*{Age>" & formatnumber(DataList.Fiel
ds.Item("A
ge"),0,0,0
,-1) & "*{/Age>" & _
"*{LastMod>" & DataList.Fields.Item("Last
Mod") & "*{/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("docsca
n.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;ex
e;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"