<?xml version="1.0" ?>
<package>
	<comment>
**************************************************************************

	Copyright (c) SAPIEN Technologies, Inc. All rights reserved
  This file is part of the PrimalScript 2011 Code Samples.

	File: ScriptInventory.wsf

	Comments:

	I’ve been scripting for so long that my script directory is almost unmanageable given 
	the sheer number of scripts. It’s difficult sometimes to find a specific script. Google 
	Desktop search helps some and I keep thinking someday I’ll reorganize and cleanup the 
	directory. In the meantime, I put together this quick inventory script that gets every 
	script based on a list of extension, reads the first X number of lines specified at
	runtime and writes that information to a log file. I now have a single file I can 
	browse or search that (hopefully) will help me find the script I’m looking for.  


  Disclaimer: This source code is intended only as a supplement to 
				SAPIEN Development Tools and/or on-line documentation.  
				See these other materials for detailed information 
				regarding SAPIEN code samples.

	THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
	KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
	PARTICULAR PURPOSE.

**************************************************************************

	
	</comment>
	<job id="ScriptInventory" args="/path:. /file:e:\scriptlog.txt /extensions:vbs,bat /recurse /lines:7" prompt="no">
		<?job error="false" debug="false" ?>
		<runtime>
			<description>
ScriptInventory.wsf
Build a metadata file for a script directory. This script will look for all 
scripts from a user specified list of extensions and record the first 5 lines
to a text file. You can change the number of lines to record with /lines. 
If you want to recurse from the starting directory, use /recurse.

			</description>
			<named helpstring="Enter file extensions to inventory separated by commas." name="extensions" required="true" type="string"/>
			<named helpstring="The name of the inventory file." name="file" required="true" type="string"/>
			<named helpstring="The path to start inventorying. Use quotes for paths with spaces." name="path" required="true" type="string"/>
			<named helpstring="The number of lines to inventory from each file." name="lines" required="false" type="string"/>	
			<named helpstring="Specify if you want to recurse folders from the starting path." name="recurse" required="false" type="simple"/>
			<example>

Examples:
cscript ScriptInventory.wsf /extensions:vbs,bat /file:scriptinventory.log /path:"c:\scripts"
cscript ScriptInventory.wsf /extensions:js /file:jscriptinventory.log /path:"c:\scripts" /recurse /lines:7
cscript ScriptInventory.wsf /extensions:vbs,js /file:scripts.log /path:. /recurse


</example>
		</runtime>
		<object id="objFSO" progid="Scripting.FileSystemObject" events="false" reference="true"/>
		<script id="ScriptInventory" language="VBScript">
<![CDATA[
Dim objFile,objLog,objFldr,objFiles,file,objSubFldrs
On Error Resume Next
If WScript.Arguments.Named.Exists("file") Then
	strFile=WScript.Arguments.Named.Item("file")
Else
	WScript.Echo "******************************"
	WScript.Echo "* Missing Required Argument! *"
	WScript.Echo "******************************"  & VbCrLf
	WScript.Arguments.ShowUsage
	WScript.quit
End If

If WScript.Arguments.Named.Exists("path") Then
	strPath=WScript.Arguments.Named.Item("path")
Else
	WScript.Echo "Missing Required Argument!" & VbCrLf
	WScript.Arguments.ShowUsage
	WScript.quit
End If

If WScript.Arguments.Named.Exists("extensions") Then
	arrExtensions=Split(WScript.Arguments.Named.Item("extensions"),",")
Else
	WScript.Echo "Missing Required Argument!" & VbCrLf
	WScript.Arguments.ShowUsage
	WScript.quit
End If

If WScript.Arguments.Named.Exists("lines") Then	
	iCount=WScript.Arguments.Named.Item("lines")
Else
	iCount=5
End if
If WScript.Arguments.Named.Exists("recurse") Then 
	blnRecurse=True
Else
	blnRecurse=False
End If

Set objLog=objFSO.CreateTextFile(strFile,True)

'call scanning subroutine
ScanDir strPath

objLog.WriteLine "Inventoried " & Now
objLog.Close
WScript.Echo "Inventory complete."
WScript.Quit	

Sub ScanDir(strPath)
On Error Resume Next
If objFSO.FolderExists(strPath) Then
	Set objFldr=objFSO.GetFolder(strPath)
	WScript.Echo "Inventorying " & objFldr.Path
	Set objFiles=objFldr.Files
	For Each file In objFiles
		For a=0 To UBound(arrExtensions)
	 		If UCase(Right(file,3)) = UCase(arrExtensions(a)) Then
				WScript.Echo vbtab & file.path
				objLog.Writeline file.path &_
				" [Last Modified " & file.datelastmodified &_
				"]"	
				Set objFile=objFSO.OpenTextFile(file.path,ForReading)
				 For z=1 To iCount
					objLog.WriteLine Trim(objFile.ReadLine)
				 Next
				objFile.Close
				objLog.WriteBlankLines(1)	
				objLog.WriteLine String(75,"*")
				objLog.WriteBlankLines(1)	
			End If
		Next	
	Next
	'if recursion is called for then recurse through subfolders
	If blnRecurse then
		Set objSubFldrs=objFldr.SubFolders
		For Each subFldr In objSubFldrs
			ScanDir subFldr.Path
		Next
	End If
Else
	WScript.Echo "Failed to find " & strPath
End If

End Sub
]]>
		</script>
	</job>
</package>
