You are on page 1of 14

06/03/13 File System Management with VBScript

www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 1/14
You are here:
ActiveXperts.com > ActiveXperts Network Monitor > WindowsManagement > Scripts > Storage >
File Systems
Monitor servers, workstations, devices and applications in your network
Quicklinks
Download ActiveXperts Network Monitor 7.4 (29118 KB)
Download Manual (374 KB)
Screenshots - Product image gallery
File System Management with VBScript
Create a Custom Document Property
Copy a File
Copy a Set of Files
Delete All Files in a Folder
Delete a Custom Document Property
Delete a File
List All the Files on a Computer
List All the Files in a Folder
List Detailed Summary Information for a File
List File Attributes
List File Properties
List File Properties
List Files Using an Asynchronous Query
List File Version Information
List Summary Information for a Set of Files
List a Specific Set of Files
Modify a Custom Document Property
Modify Document Property Information
Move a File
Monitor File Deletion
Modify File Extensions
Modify File Attributes
Move Files
Monitor File Modification
Monitor File Creation
Move a Set of Files
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 2/14
Perform Actions on Files
Parse a Path Name
Retrieving Document Property Information
Retrieving Extended File Properties
Rename a File
Rename Files
Search for Files Using a Wildcard Query
Verify that a File Exists
You can use any of the VBScript programs below in ActiveXperts Network Monitor. Click
here for an explanation about how to include scripts in ActiveXperts Network Monitor.
Create a Custom Document Property
Adds a custom property (TestProperty, with a value of "Test") to the summary information properties
for a document named C:\Scripts\Test.doc.
Set objPropertyReader = CreateObject("DSOleFile.PropertyReader")
Set objDocument = objPropertyReader.GetDocumentProperties _
("C:\Scripts\Test.doc")
Set colCustomProperties = objDocument.CustomProperties
errReturn = ColCustomProperties.Add("TestProperty", "Test")

Copy a File
Demonstration script that uses the FileSystemObject to copy a file. Script must be run on the local
computer.
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\", OverwriteExisting

Copy a Set of Files
Demonstration script that uses the FileSystemObject to copy all the .txt files in a folder to a new
location.
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 3/14
objFSO.CopyFile "C:\FSO\*.txt" , "D:\Archive\" , OverwriteExisting

Delete All Files in a Folder
Demonstration script that deletes all the .txt files in a folder. Script must be run on the local computer.
Const DeleteReadOnly = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\FSO\*.txt"), DeleteReadOnly

Delete a Custom Document Property
Deletes a custom property (TestProperty) from the summary information properties for a document
named C:\Scripts\Test.doc.
Set objPropertyReader = CreateObject("DSOleFile.PropertyReader")
Set objDocument = objPropertyReader.GetDocumentProperties_
("C:\Scripts\Test.doc")
Set colCustomProperties = objDocument.CustomProperties
For Each strProperty in colCustomProperties
If strProperty.Name = "TestProperty" Then
strProperty.Remove()
End If
Next

Delete a File
Demonstration script that uses the FileSystemObject to delete a file. Script must be run on the local
computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\FSO\ScriptLog.txt")

List All the Files on a Computer
Enumerates all the files on a computer. This is a demonstration script; if actually run, it could take an
hour or more to complete, depending on the number of files on the computer. Depending on the
number of files and on available memory, this script could also fail before finishing.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 4/14
Set colFiles = objWMIService.ExecQuery("Select * from CIM_Datafile")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next

List All the Files in a Folder
Returns a list of all the files in the Scripts folder. If the computer has more than one scripts folder (for
example, C:\Scripts and D:\Scripts), files will be returned from each of these folders.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Path = '\\Scripts\\'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next

List Detailed Summary Information for a File
Uses the Shell's Application object to retrieve detailed summary information including name, size,
owner, and file attributes) for all the files in a folder.
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace ("C:\Scripts")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim arrHeaders(13)
For i = 0 to 13
arrHeaders(i) = objFolder.GetDetailsOf (objFolder.Items, i)
Next
For Each strFileName in objFolder.Items
For i = 0 to 13
If i <> 9 then
Wscript.echo arrHeaders(i) _
& ": " & objFolder.GetDetailsOf (strFileName, i)
End If
Next
Wscript.Echo
Next

List File Attributes
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 5/14
Demonstration script that uses the FileSystemObject to enumerate the attributes of a file. Script must
be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\FSO\ScriptLog.txt")
If objFile.Attributes AND 0 Then
Wscript.Echo "No attributes set."
End If
If objFile.Attributes AND 1 Then
Wscript.Echo "Read-only."
End If
If objFile.Attributes AND 2 Then
Wscript.Echo "Hidden file."
End If
If objFile.Attributes AND 4 Then
Wscript.Echo "System file."
End If
If objFile.Attributes AND 32 Then
Wscript.Echo "Archive bit set."
End If
If objFile.Attributes AND 64 Then
Wscript.Echo "Link or shortcut."
End If
If objFile.Attributes AND 2048 Then
Wscript.Echo "Compressed file."
End If

List File Properties
Demonstration script that uses the FileSystemObject to enumerate the properties of a file. Script must
be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\windows\system32\scrrun.dll")
Wscript.Echo "Date created: " & objFile.DateCreated
Wscript.Echo "Date last accessed: " & objFile.DateLastAccessed
Wscript.Echo "Date last modified: " & objFile.DateLastModified
Wscript.Echo "Drive: " & objFile.Drive
Wscript.Echo "Name: " & objFile.Name
Wscript.Echo "Parent folder: " & objFile.ParentFolder
Wscript.Echo "Path: " & objFile.Path
Wscript.Echo "Short name: " & objFile.ShortName
Wscript.Echo "Short path: " & objFile.ShortPath
Wscript.Echo "Size: " & objFile.Size
Wscript.Echo "Type: " & objFile.Type

List File Properties
Lists the properties for the file C:\Scripts\Adsi.vbs.
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 6/14
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where name = 'c:\\Scripts\\Adsi.vbs'")
For Each objFile in colFiles
Wscript.Echo "Access mask: " & objFile.AccessMask
Wscript.Echo "Archive: " & objFile.Archive
Wscript.Echo "Compressed: " & objFile.Compressed
Wscript.Echo "Compression method: " & objFile.CompressionMethod
Wscript.Echo "Creation date: " & objFile.CreationDate
Wscript.Echo "Computer system name: " & objFile.CSName
Wscript.Echo "Drive: " & objFile.Drive
Wscript.Echo "8.3 file name: " & objFile.EightDotThreeFileName
Wscript.Echo "Encrypted: " & objFile.Encrypted
Wscript.Echo "Encryption method: " & objFile.EncryptionMethod
Wscript.Echo "Extension: " & objFile.Extension
Wscript.Echo "File name: " & objFile.FileName
Wscript.Echo "File size: " & objFile.FileSize
Wscript.Echo "File type: " & objFile.FileType
Wscript.Echo "File system name: " & objFile.FSName
Wscript.Echo "Hidden: " & objFile.Hidden
Wscript.Echo "Last accessed: " & objFile.LastAccessed
Wscript.Echo "Last modified: " & objFile.LastModified
Wscript.Echo "Manufacturer: " & objFile.Manufacturer
Wscript.Echo "Name: " & objFile.Name
Wscript.Echo "Path: " & objFile.Path
Wscript.Echo "Readable: " & objFile.Readable
Wscript.Echo "System: " & objFile.System
Wscript.Echo "Version: " & objFile.Version
Wscript.Echo "Writeable: " & objFile.Writeable
Next

List Files Using an Asynchronous Query
Uses an asynchronous query to enumerate all the files on a computer. This is primarily a
demonstration script; if actually run, it could take an hour or more to complete, depending on the
number of files on the computer.
Const POPUP_DURATION = 120
Const OK_BUTTON = 0
Set objWSHShell = Wscript.CreateObject("Wscript.Shell")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objSink = WScript.CreateObject("WbemScripting.SWbemSink","SINK_")
objWMIService.ExecQueryAsync objSink, "Select * from CIM_DataFile"
objPopup = objWshShell.Popup("Starting file retrieval", _
POPUP_DURATION, "File Retrieval", OK_BUTTON)
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 7/14
Sub SINK_OnObjectReady(objEvent, objAsyncContext)
Wscript.Echo objEvent.Name
End Sub

List File Version Information
Demonstration script that uses the FileSystemObject to retrieve the file version for a .dll file. Script
must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wscript.Echo objFSO.GetFileVersion("c:\windows\system32\scrrun.dll")

List Summary Information for a Set of Files
Lists summary information for all the files in the folder C:\Scripts.
Const FILE_NAME = 0
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace ("C:\Scripts")
For Each strFileName in objFolder.Items
Wscript.Echo "File name: " & objFolder.GetDetailsOf _
(strFileName, FILE_NAME)
Next

List a Specific Set of Files
Returns a list of all the files larger than 1,000,000 bytes
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where FileSize > 1000000")
For Each objFile in colFiles
Wscript.Echo objFile.Name & " -- " & objFile.FileSize
Next

Modify a Custom Document Property
Modifies a custom property (TestProperty, setting the new value to "New value") found in the summary
information properties for a document named C:\Scripts\Test.doc.
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 8/14
Set objPropertyReader = CreateObject("DSOleFile.PropertyReader")
Set objDocument = objPropertyReader.GetDocumentProperties _
("C:\Scripts\Test.doc")
Set colCustomProperties = objDocument.CustomProperties
For Each strProperty in colCustomProperties
If strProperty.Name = "TestProperty" Then
strProperty.Value = "New value"
End If
Next

Modify Document Property Information
Modifies the Category property included in the summary information properties for a document named
C:\Scripts\Test.doc.
Set objPropertyReader = CreateObject("DSOleFile.PropertyReader")
Set objDocument = objPropertyReader.GetDocumentProperties _
("C:\Scripts\Test.doc")
objDocument.Category = "Scripting Documents"

Move a File
Demonstration script that uses the FileSystemObject to move a file from one location to another.
Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.log" , "D:\Archive"

Monitor File Deletion
Temporary event consumer that issues an alert any time a file is deleted from the C:\Scripts folder.
Best when run under Cscript.exe.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceDeletionEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 9/14
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

Modify File Extensions
Changes the file extension for all the .log files in the C:\Scripts folder to .txt.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='c:\Scripts'} Where " _
& "ResultClass = CIM_DataFile")
For Each objFile In FileList
If objFile.Extension = "log" Then
strNewName = objFile.Drive & objFile.Path & _
objFile.FileName & "." & "txt"
errResult = objFile.Rename(strNewName)
End If
Next

Modify File Attributes
Demonstration script that checks to see if a file is read-only and, if it is not, marks it as read-only.
Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\FSO\TestScript.vbs")
If objFile.Attributes = objFile.Attributes AND 1 Then
objFile.Attributes = objFile.Attributes XOR 1
End If

Move Files
Moves all the Windows Media (.wma) files to the folder C:\Media Archive.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where Extension = 'wma'")
For Each objFile in colFiles
strCopy = "C:\Media Archive\" & objFile.FileName _
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 10/14
& "." & objFile.Extension
objFile.Copy(strCopy)
objFile.Delete
Next

Monitor File Modification
Temporary event consumer that issues an alert any time the file C:\Scripts\Index.vbs is modified. Best
when run under Cscript.exe.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' and " _
& "TargetInstance.Name='c:\\scripts\\index.vbs'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo "File: " & objLatestEvent.TargetInstance.Name
Wscript.Echo "New size: " & objLatestEvent.TargetInstance.FileSize
Wscript.Echo "Old size: " & objLatestEvent.PreviousInstance.FileSize
Loop

Monitor File Creation
Temporary event consumer that issues an alert any time a file is created in the C:\Scripts folder. Best
when run under Cscript.exe.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop

Move a Set of Files
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 11/14
Demonstration script that uses the FileSystemObject to move all the .txt files in a folder to a new
location. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\*.txt" , "D:\Archive\"

Perform Actions on Files
Uses the Shell object to print all the files in the C:\Logs folder.
TargetFolder = "C:\Logs"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For i = 0 to colItems.Count - 1
colItems.Item(i).InvokeVerbEx("Print")
Next

Parse a Path Name
Demonstration script that uses the FileSystemObject to return pathname information for a file,
including name, extension, complete path, etc. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("ScriptLog.txt")
Wscript.Echo "Absolute path: " & objFSO.GetAbsolutePathName(objFile)
Wscript.Echo "Parent folder: " & objFSO.GetParentFolderName(objFile)
Wscript.Echo "File name: " & objFSO.GetFileName(objFile)
Wscript.Echo "Base name: " & objFSO.GetBaseName(objFile)
Wscript.Echo "Extension name: " & objFSO.GetExtensionName(objFile)

Retrieving Document Property Information
Lists the summary information properties for a document named C:\Scripts\Test.doc.
Set objPropertyReader = CreateObject("DSOleFile.PropertyReader")
Set objDocument = objPropertyReader.GetDocumentProperties _
("C:\Scripts\Test.doc")
Wscript.Echo "Application name: " & objDocument.AppName
Wscript.Echo "Author: " & objDocument.Author
Wscript.Echo "Byte count: " & objDocument.ByteCount
Wscript.Echo "Category: " & objDocument.Category
Wscript.Echo "Character count: " & objDocument.CharacterCount
Wscript.Echo "Character count with spaces: " & _
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 12/14
objDocument.CharacterCountWithSpaces
Wscript.Echo "CLSID: " & objDocument.CLSID
Wscript.Echo "Comments: " & objDocument.Comments
Wscript.Echo "Company: " & objDocument.Company
Set colCustomProperties = objDocument.CustomProperties
For Each strProperty in colCustomProperties
Wscript.Echo vbTab & strProperty.Name & ": " & strProperty.Value
Next
Wscript.Echo "Date created: " & objDocument.DateCreated
Wscript.Echo "Date last printed: " & objDocument.DateLastPrinted
Wscript.Echo "Date last saved: " & objDocument.DateLastSaved
Wscript.Echo "Has macros: " & objDocument.HasMacros
Wscript.Echo "Hidden slides: " & objDocument.HiddenSlides
Wscript.Echo "Icon: " & objDocument.Icon
Wscript.Echo "Is read only: " & objDocument.IsReadOnly
Wscript.Echo "Keywords" & objDocument.Keywords
Wscript.Echo "Last edited by: " & objDocument.LastEditedBy
Wscript.Echo "Line count: " & objDocument.LineCount
Wscript.Echo "Location: " & objDocument.Location
Wscript.Echo "Manager: " & objDocument.Manager
Wscript.Echo "Multimedia clips: " & objDocument.MultimediaClips
Wscript.Echo "Name: " & objDocument.Name
Wscript.Echo "Page count: " & objDocument.PageCount
Wscript.Echo "Paragraph count: " & objDocument.ParagraphCount
Wscript.Echo "Presentation format: " & objDocument.PresentationFormat
Wscript.Echo "Presentation notes: " & objDocument.PresentationNotes
Wscript.Echo "ProgID: " & objDocument.ProgID
Wscript.Echo "Revision number: " & objDocument.RevisionNumber
Wscript.Echo "Slide count: " & objDocument.SlideCount
Wscript.Echo "Subject: " & objDocument.Subject
Wscript.Echo "Template: " & objDocument.Template
Wscript.Echo "Thumbnail: " & objDocument.Thumbnail
Wscript.Echo "Title: " & objDocument.Title
Wscript.Echo "Version: " & objDocument.Version
Wscript.Echo "Word count: " & objDocument.WordCount

Retrieving Extended File Properties
Uses the Shell object to return extended properties for all the files in the folder C:\Scripts.
Dim arrHeaders(34)
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:\Scripts")
For i = 0 to 33
arrHeaders(i) = objFolder.GetDetailsOf(objFolder.Items, i)
Next
For Each strFileName in objFolder.Items
For i = 0 to 33
Wscript.Echo i & vbtab & arrHeaders(i) _
& ": " & objFolder.GetDetailsOf(strFileName, i)
Next
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 13/14
Next

Rename a File
Demonstration script that uses the FileSystemObject to rename a file. Script must be run on the local
computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.txt" , "C:\FSO\BackupLog.txt"

Rename Files
Renames the file C:\Scripts\Toggle_Service.vbs to C:\Scripts\Toggle_Service.old.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile where Name = " _
& "'c:\\scripts\\toggle_service.vbs'")
For Each objFile in colFiles
errResult = objFile.Rename("c:\scripts\toggle_service.old")
Wscript.Echo errResult
Next

Search for Files Using a Wildcard Query
Uses the Like keyword to search for all files on a computer that begin with the tilde (~).
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where FileName Like '%~%'")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next

Verify that a File Exists
Uses the FileSystemObject to determine whether or not the file C:\FSO\ScriptLog.txt exists on the
06/03/13 File System Management with VBScript
www.activexperts.com/activmonitor/windowsmanagement/scripts/storage/filesystems/#MLC.htm 14/14
local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\FSO\ScriptLog.txt") Then
Set objFolder = objFSO.GetFile("C:\FSO\ScriptLog.txt")
Else
Wscript.Echo "File does not exist."
End If