Asynchronously Enumerating Files
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 event retrieval", _
POPUP_DURATION, "Event Retrieval", OK_BUTTON)
Sub SINK_OnObjectReady(objEvent, objAsyncContext)
Wscript.Echo objEvent.Name
End SubChanging 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)
Wscript.Echo errResult
End If
NextCopying 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\", OverwriteExistingCopying 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")
objFSO.CopyFile "C:\FSO\*.txt" , "D:\Archive\" , OverwriteExistingDeleting 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"), DeleteReadOnlyDeleting 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")Enumerating 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
NextEnumerating All the Files on a Computer
Enumerates 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. Depending on the number of files and on available memory, this script could fail before finishing.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile")
For Each objFile in colFiles
Wscript.Echo objFile.Name
NextEnumerating File Attributes
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 IfEnumerating 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.TypeEnumerating a Specific Set of Files
Returns a list of all the files larger than 1,000,000 bytes.
Enumerating a Specific Set of Files
Modifying 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 IfMonitoring 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
LoopMonitoring 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
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
LoopMonitoring 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
LoopMoving a File
Demonstration script that uses the FileSystermObject 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"Moving 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 _
& "." & objFile.Extension
objFile.Copy(strCopy)
objFile.Delete
NextMoving a Set of Files
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\"Parsing a Path Name
Demonstration script that uses the FileSystemObject to return pathname information for a while, including file name, file extension, complete file 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)Performing 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")
NextRenaming 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"Renaming Files
Renames the file C:\Scripts\Toggle_Service.vbs to C:\Scripts\Toggle_Service.old.
strComputer = "."
Set objWMIService = GetObject _
("winmgmts:" & "!\\" & 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
NextRetrieving 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
NextRetrieving 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
NextRetrieving File Properties
Lists the properties for the file C:\Scripts\Adsi.vbs.
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
NextRetrieving 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")Retrieving Summary Information for a File
Uses the Shell's Application object to retrieve the file name for all the files in a folder.
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)
NextUsing Wildcards in File Queries
Returns a list of all the files whose name beings with a 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
NextVerifying that a File Exists
Demonstration script that uses the FileSystemObject to verify that a file exists. Script must be run on the 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
No comments:
Post a Comment