Freitag, 10. September 2010

Search for specific Files and write to INI

This script was developed as a part of an automated backup solution for user data. It is the engine, which performs filesystem searches for files of a specified type (e.g. PST) using WMI Query. All found files will be written to an INI file with their complete path for further processing. The needed informations are read from a specific registry key.


'Script created by: Zierer, Markus
'Script created on: 2010 08 26
'
'This script performs file search operations and writes the results into an INI File.
'Note that it values stored in Windows Registry
'
'Prepare Variables
Dim objWMIService, objFSO, objTextFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
const HKCU = &H80000001
const HKLM = &H80000002
strComputer = "."
strRegKeyPath = "SOFTWARE\FITS\Software\Backup"
strRegValueExtension = "Extension"
strRegValueSearchDrive = "SearchDrive"
strRegValueINIFile = "INIFile"
strRegValueSection = "Section"

'Read Variable Values from Registry
Set objRegRead = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objRegRead.GetStringValue HKLM,strRegKeyPath,strRegValueExtension,strExtension
objRegRead.GetStringValue HKLM,strRegKeyPath,strRegValueSearchDrive,strSearchDrive
objRegRead.GetStringValue HKLM,strRegKeyPath,strRegValueINIFile,strINIFile
objRegRead.GetStringValue HKLM,strRegKeyPath,strRegValueSection,strSection

'Wscript.Echo strExtension,strSearchDrive,strINIFile,strSection
'Wscript.Quit

'Perform File Search Operation
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Extension = '" & strExtension & "' AND Drive = '" & strSearchDrive & "'")

'Prepare Write to INI File Operation
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objINIFile = objFSO.OpenTextFile(strINIFile, ForAppending)

'Write Section to INI File
objINIFile.WriteLine(strSection)

'End Script if no files found
If colFiles.Count = 0 Then
objINIFile.WriteLine("1=")
Wscript.Quit
End If

'Write Keys and Values to INI File
intCounter = 1
intKey = intCounter

For Each objFile in colFiles
boolWriteToINI = True
Call Exception()
If boolWriteToINI = True Then
objINIFile.WriteLine(intKey & "=" & objFile.Drive & objFile.Path & objFile.FileName & "." & objFile.Extension)
intKey = intCounter + 1
intCounter = intKey
End If

Next

'Write an Empty Key to declare End of Section
objINIFile.WriteLine(intKey & "=")

'Function to Check for Excluded Paths
'Copy a IF Section to add additional Exception
Function Exception
If instr(objFile.Name, "c:\winxp\pchealth\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\winxp\softwaredistribution\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\winxp\system32\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\recycler\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\programme\microsoft office\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\programme\sap\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\programme\techsmith\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\programme\vorlagen\fi-ts\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "normal.dot") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\temp\") Then
boolWriteToINI = False
End If
If instr(objFile.Name, "c:\programme\netinst\") Then
boolWriteToINI = False
End If
End Function

Keine Kommentare:

Kommentar veröffentlichen