Log Inventory Of Folder Into A File Multiple Options

So you want to log a content of a folder to a comma dilimted file (or something like that) but you need more options then what DOS can give…
For example you can use the following to use the DIR command

  1. Get to the MS-DOS prompt or the Windows command line.
  2. Navigate to the directory you wish to print the contents of. If you’re new to the command line, familiarize yourself with the cd command and thedir command.
  3. Once in the directory you wish to print the contents of, type one of the below commands.
    dir > print.txt
    The above command will take the list of all the files and all of the information about the files, including size, modified date, etc., and send that output to the print.txt file in the current directory.
    dir /b > print.txt
    This command would print only the file names and not the file information of the files in the current directory.
    dir /s /b > print.txt
    This command would print only the file names of the files in the current directory and any other files in the directories in the current directory.

But the options are kinda limited to what I needed here.

So here is the idea, you run your script with an argument option – remember my guide?
So we would run our script File_Inv.vbs /C:\Windows and our output file would look like so



NAME| SIZE| TYPE| DATE LAST MMODIFIED| FILE VERSION| FILE PATH
bfsvc.exe|65024|Application|7/13/2009 9:14:12 PM|6.1.7600.16385|C:\Windows
entrust.ini|4133|Configuration settings|12/15/2009 2:41:14 PM||C:\Windows
explorer.exe|2614784|Application|2/26/2011 1:33:07 AM|6.1.7600.16768|C:\Windows
fveupdate.exe|13824|Application|7/13/2009 9:14:20 PM|6.1.7600.16385|C:\Windows
HelpPane.exe|497152|Application|7/13/2009 9:14:21 PM|6.1.7600.16385|C:\Windows
hh.exe|15360|Application|7/13/2009 9:14:21 PM|6.1.7600.16385|C:\Windows
ina32.ini|304|Configuration settings|7/12/2012 12:23:19 PM||C:\Windows
mercury.ini|134|Configuration settings|7/16/2012 2:22:43 PM||C:\Windows
msdfmap.ini|1405|Configuration settings|6/10/2009 5:19:27 PM||C:\Windows
notepad.exe|179712|Application|7/13/2009 9:14:27 PM|6.1.7600.16385|C:\Windows
regedit.exe|398336|Application|7/13/2009 9:14:30 PM|6.1.7600.16385|C:\Windows
system.ini|219|Configuration settings|6/10/2009 5:46:28 PM||C:\Windows
twain.dll|94784|Application extension|6/10/2009 5:41:17 PM|1.7.0.0|C:\Windows
twain_32.dll|51200|Application extension|7/13/2009 9:16:16 PM|1.7.1.3|C:\Windows
twunk_16.exe|49680|Application|6/10/2009 5:41:17 PM|1.7.0.0|C:\Windows
twunk_32.exe|31232|Application|7/13/2009 9:14:42 PM|1.7.1.0|C:\Windows
vbaddin.ini|39|Configuration settings|7/18/2012 8:47:38 AM||C:\Windows
win.ini|478|Configuration settings|7/11/2012 2:36:45 PM||C:\Windows
winhelp.exe|256192|Application|6/10/2009 5:42:20 PM|3.10.0.425|C:\Windows
winhlp32.exe|9728|Application|7/13/2009 9:14:45 PM|6.1.7600.16385|C:\Windows
write.exe|9216|Application|7/13/2009 9:14:49 PM|6.1.7600.16385|C:\Windows

In my example of my output file above you can see that i’m pulling only EXE, INI and OCX files with specific content of each file
NAME| SIZE| TYPE| DATE LAST MODIFIED| FILE VERSION| FILE PATH

Here goes the script
At first we add our argument option to specify a folder from a command line, as well we add the option to remove the forward slash from the argument (that’s how I like it but do what’s good for you)
As well i’m going to use the machine name as part of my output file name


Const ForAppending = 8
Const ForWriting = 2
Const ForReading = 1
Set wshNetwork = WScript.CreateObject( "WScript.Network" )
set objShell = CreateObject("Wscript.Shell")
strComputer = "."
strComputerName = wshNetwork.ComputerName
Set objArgs = WScript.Arguments
'WScript.Echo WScript.Arguments.Count
For Each strArg in objArgs
'WScript.Echo strArg
RetalixArgs = strArg

Next
'--- Check File Versions Before Upgrade

MachineType = Right(strComputerName, 3)
SiteOfMachine = Left (strComputerName, 6)
'RDW
If Not RetalixArgs = Empty Then
'WScript.Echo (RetalixArgs)
RetalixArgsPath = (Replace(RetalixArgs,"/",""))
BaseFolderNameX = RetalixArgsPath
OutPutFileName = "C:\" & strComputerName & "_Data.txt"
'WScript.Echo (RetalixArgsPath)
Else

'Do something

End if

Second part is creating and validating my file, please see that I delete it if it exists every time I use this script


End if
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(OutPutFileName)then
objFSO.Deletefile(OutPutFileName)
end if

Set objFile = objFSO.CreateTextFile(OutPutFileName)
objFile.Close

Set objTextFile = objFSO.OpenTextFile(OutPutFileName, ForAppending, True)
objTextFile.WriteLine("NAME|    SIZE|    TYPE|    DATE LAST MODIFIED|    FILE VERSION|   FILE PATH")
objTextFile.Close

Now I call the function


ViewFolders BaseFolderNameX

Here you can see that i’m specifying the type of files that I need from the folder and what data to read from them, remember to match up to the first line that you ad above so you don’t mess up excel or beyond compare :)


Sub ViewFolders(strFolder)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set colListOfFiles = objFolder.Files

For Each objFile in colListOfFiles
sExtension = objFSO.GetExtensionName (objFile)
'WScript.Echo(sExtension)
If sExtension = "exe" Or sExtension = "dll" Or sExtension = "ocx" Or sExtension = "ini" Then
Set objTextFile = objFSO.OpenTextFile (OutPutFileName, ForAppending, True)
objTextFile.WriteLine(objFile.Name&"|"&objFile.Size&"|"&objFile.Type&"|"&objFile.DateLastModified&"|")& objFSO.GetFileVersion(objFile) & ("|"&objFile.ParentFolder)
'WScript.Echo (objFile.Name&"|"&objFile.Size&"|"&objFile.Type&"|"&objFile.DateLastModified&"|")& objFSO.GetFileVersion(objFile) & ("|"&objFile.ParentFolder)
objTextFile.Close
End If
'Set objFolder = Nothing
'Set colListOfFiles = Nothing
Next
End Sub

You can download a text file from my repo – Rename Local User Script (1578)

 

This entry was posted in Scripting and tagged , , , , . Bookmark the permalink.