Subscribe to Windows IT Pro
June 17, 2009 12:00 AM

Time to Round Up Those Scripts

Don't leave your scripts scattered about
Windows IT Pro
InstantDoc ID #102139
Rating: (1)
Downloads
102139.zip

Listing 1: ScriptRoundUp.vbs

Const adPersistXML  = 1
Const adFldIsNullable = 32
Const adLongVarChar = 201

ColPath = "C:\Scripts\AllScripts\"
DestRoot = ColPath & "ScriptFiles"

strQuery = "Select Drive,Extension,Name,Path from CIM_DataFile " & _
  "Where (Drive='c:' OR Drive='d:') AND (extension='vbs' OR extension='hta')"

Set fso = CreateObject("Scripting.FileSystemObject")

If Not fso.FolderExists(ColPath) Or Not fso.folderExists(DestRoot) Then
  Msgbox "Collection Folder " & ColPath & " or " & DestRoot & " does not exist... Terminating Script"
  WScript.Quit
End If

strMessage = "A message box will appear when process is complete."
strMsgTitle = "Script Round-up"
CreateObject("WScript.Shell").Popup strMessage,10,strMsgTitle,vbInformation

' Begin Callout A
Set objShell = CreateObject("Shell.Application")
Set RootFolder = objShell.NameSpace(DestRoot)
' End Callout A

' Begin Callout B
Set DRS = CreateObject("ADODB.Recordset")
DRS.Fields.Append "Drive",adLongVarChar,256,adFldIsNullable
DRS.Fields.Append "Extension",adLongVarChar,256,adFldIsNullable
DRS.Fields.Append "Name",adLongVarChar,256,adFldIsNullable
DRS.Fields.Append "Path",adLongVarChar,256,adFldIsNullable
DRS.Open
' End Callout B

' Begin Callout C
Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colFiles = objWMIService.ExecQuery(strQuery)

For Each objFile In colFiles
  DRS.AddNew
  DRS("Drive") = objFile.Drive
  DRS("Extension") = objFile.Extension
  DRS("Name") = objFile.Name
  DRS("Path") = objFile.Path
Next

DRS.MoveFirst
' End Callout C

' Begin Callout D
Do While Not DRS.EOF
  FolderPath = Replace(DRS.Fields.Item("Drive"),":","") & DRS.Fields.Item("Path")
  Dest = DestRoot & "\" & Replace(DRS.Fields.Item("Drive"),":","") & DRS.Fields.Item("Path")
  If Not fso.FolderExists(Dest) Then
    RootFolder.NewFolder(FolderPath)
  End If
  Set sourcefile = nothing
  Set sourcefile = fso.getFile(DRS.Fields.Item("Name"))
  sourcefile.Copy Dest
  DRS.MoveNext
Loop
' End Callout D

If fso.FileExists(ColPath & "Scripts.xml") Then
  fso.DeleteFile(ColPath & "Scripts.xml")
End If

DRS.Save ColPath & "Scripts.xml",adPersistXML
DRS.Close
MsgBox "Done"

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.