'" & _ dtmtargetDate & "'")For Each objFolder in colFolders Wscript.Echo objFolder.Name Next逐个字符地读取文本文件描述演示脚本使用 FileSystemObject 逐个字符地读取文本文件,并分别将这些字符显示在屏幕上。此脚本必须运行在本地计算机上。脚本代码Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("C:\FSO\New Text Document.txt", 1)Do Until objFile.AtEndOfStream strCharacters = objFile.Read(1) Wscript.Echo strCharactersLoop识别 Shell 对象动词描述为 Recycle Bin 返回 Shell 对象动词(上下文菜单项)的列表。脚本代码Const RECYCLE_BIN = &Ha&Set objShell = CreateObject("Shell.Application")Set objFolder = objShell.NameSpace(RECYCLE_BIN) Set objFolderItem = objFolder.Self Set colVerbs = objFolderItem.VerbsFor i = 0 to colVerbs.Count - 1 Wscript.Echo colVerbs.Item(i)Next生成文件名描述演示脚本使用 FileSystemObject 的 GetTempName 方法生成随机文件名。此脚本必须运行在本地计算机上。脚本代码Set objFSO = CreateObject("Scripting.FileSystemObject")For i = 1 to 10 strTempFile = objFSO.GetTempName Wscript.Echo strTempFileNext创建和命名文本文件描述演示脚本使用 FileSystemObject 的 GetTempName 方法生成一个文件名,然后根据该名称创建一个文件。脚本代码Set objFSO = CreateObject("Scripting.FileSystemObject")strPath = "C:\FSO"strFileName = objFSO.GetTempNamestrFullName = objFSO.BuildPath(strPath, strFileName)Set objFile = objFSO.CreateTextFile(strFullName)objFile.CloseobjFSO.DeleteFile(strFullName)创建网络共享描述创建一个名为 FinanceShare 的共享文件夹,将同时连接的最大数量设置为25,然后添加共享描述。脚本代码Const FILE_SHARE = 0Const MAXIMUM_CONNECTIONS = 25strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set objNewShare = objWMIService.Get("Win32_Share")errReturn = objNewShare.Create _ ("C:\Finance", "FinanceShare", FILE_SHARE, _ MAXIMUM_CONNECTIONS, "Public share for the Finance group.")Wscript.Echo errReturn创建文本文件描述演示脚本创建一个新的空文本文件。此脚本必须运行在本地计算机上。脚本代码Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.CreateTextFile("C:\FSO\ScriptLog.txt")创建文件夹描述演示脚本使用 FileSystemObject 创建一个文件夹。此脚本必须运行在本地计算机上。脚本代码Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFolder = objFSO.CreateFolder("C:\FSO")发布共享文件夹描述在 Active Directory 中发布共享文件夹,给该文件夹指派一个描述和三个关键字。脚本代码Set objComputer = GetObject _ ("LDAP://OU=Finance, DC=fabrikam, DC=com")Set objShare = objComputer.Create("volume", "CN=FinanceShare")objShare.Put "uNCName", "[url=file://atl-dc-02/FinanceShare]\\atl-dc-02\FinanceShare[/url]"objShare.Put "Description", "Public share for users in the Finance group."objShare.Put "Keywords", Array("finance", "fiscal", "monetary") objShare.SetInfo在文件上执行操作描述使用 Shell 对象打印 C:\Logs 文件夹中的所有文件。脚本代码 TargetFolder = "C:\Logs" Set objShell = CreateObject("Shell.Application")Set objFolder = objShell.Namespace(TargetFolder) Set colItems = objFolder.ItemsFor i = 0 to colItems.Count - 1 colItems.Item(i).InvokeVerbEx("Print")Next按照从下到上的顺序读取文本文件描述演示脚本使用 FileSystemObject 读取一个文本文件,并以相反的顺序显示该文本文件(即从最后一行开始,以第一行结束)。脚本代码 Dim arrFileLines()i = 0Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1)Do Until objFile.AtEndOfStream Redim Preserve arrFileLines(i) arrFileLines(i) = objFile.ReadLine i = i + 1LoopobjFile.CloseFor l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1 Wscript.Echo arrFileLines(l)Next创建新文件夹描述使用 Shell 对象创建名为 C:\Archive 的新文件夹。脚本代码 ParentFolder = "C:\" set objShell = CreateObject("Shell.Application")set objFolder = objShell.NameSpace(ParentFolder) objFolder.NewFolder "Archive"监视文件修改描述任何时候修改文件 C:\Scripts\Index.vb,临时事件消费程序都会发出警告。最好运行在 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.FileSizeLoop监视文件删除描述任何时候从 C:\Scripts 文件夹删除文件,临时事件消费程序都会发出警告。最好运行在 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.GroupComp'Win32_Directory.Name=""c:\\\\scripts""'")Do Set objLatestEvent = colMonitoredEvents.NextEvent Wscript.Echo objLatestEvent.TargetInstance.PartComponentLoop复制文件夹描述演示脚本使用 FileSystemObject 将文件夹复制到新的位置。此脚本必须运行在本地计算机上。脚本代码Const OverWriteFiles = TrueSet objFSO = CreateObject("Scripting.FileSystemObject")objFSO.CopyFolder "C:\Scripts" , "C:\FSO" , OverWriteFiles监视文件创建描述任何时候在 C:\Scripts 文件夹中创建文件,临时事件消费程序就发出警告。最好在 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.GroupComp'Win32_Directory.Name=""c:\\\\scripts""'")Do Set objLatestEvent = colMonitoredEvents.NextEvent Wscript.Echo objLatestEvent.TargetInstance.PartComponentLoop复制文件描述演示脚本使用 FileSystemObject 来复制文件。此脚本必须运行在本地计算机上。脚本代码Const OverwriteExisting = TrueSet objFSO = CreateObject("Scripting.FileSystemObject")objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\", OverwriteExisting复制文件集描述演示脚本使用 FileSystemObject 将文件夹中的所有 .txt 文件复制到一个新的位置。脚本代码Const OverwriteExisting = TrueSet objFSO = CreateObject("Scripting.FileSystemObject")objFSO.CopyFile "C:\FSO\*.txt" , "D:\Archive\" , OverwriteExisting解析路径名描述演示脚本使用 FileSystemObject 临时返回路径名信息,其中包括文件名、文件扩展名、完整文件路径等等。此脚本必须运行在本地计算机上。脚本代码 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)将文本文件读入数组描述演示代码使用 VBScript Split 命令从逗号分隔的值文件读取一行,然后将该行中的各个项放入一个数组。脚本代码 Const ForReading = 1Set objFSO = CreateObject("Scripting.FileSystemObject")Set objTextFile = objFSO.OpenTextFile _ ("c:\scripts\servers and services.txt", ForReading)Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline arrServiceList = Split(strNextLine , ",") Wscript.Echo "Server name: " & arrServiceList(0) For i = 1 to Ubound(arrServiceList) Wscript.Echo "Service: " & arrServiceList(i) NextLoop解压缩文件夹描述 解压缩文件夹 C:\Scripts。脚本代码 strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colFolders = objWMIService.ExecQuery _ ("Select * from Win32_Directory where name = 'c:\\Scripts'")For Each objFolder in colFolders errResults = objFolder.Uncompress Wscript.Echo errResultsNext将网络共享映射到本地文件夹描述使用查询的 WMI Associators 返回计算机上所有网络共享的本地路径。脚本代码 strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colShares = objWMIService.ExecQuery _ ("Associators of {Win32_Share.Name='Scripts'} Where " _ & "AssocClass=Win32_ShareToDirectory")For Each objFolder in colShares Wscript.Echo objFolder.NameNext将所有的网络共享映射到本地文件夹描述使用查询的 WMI Associators 返回名为 Scripts 的网络共享的本地路径。脚本代码strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colShares = objWMIService.ExecQuery _ ("Select * From Win32_Share")For Each objShare in colShares Set colAssociations = objWMIService.ExecQuery _ ("Associators of {Win32_Share.Name='" & objShare.Name & "'} " _ & " Where AssocClass=Win32_ShareToDirectory") For Each objFolder in colAssociations Wscript.Echo objShare.Name & vbTab & objFolder.Name NextNext枚举文件夹属性描述演示脚本使用 FileSystemObject 枚举文件夹的属性。此脚本必须运行在本地计算机上。脚本代码Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFolder = objFSO.GetFolder("C:\Scripts")Wscript.Echo "Date created: " & objFolder.DateCreatedWscript.Echo "Date last accessed: " & objFolder.DateLastAccessedWscript.Echo "Date last modified: " & objFolder.DateLastModifiedWscript.Echo "Drive: " & objFolder.DriveWscript.Echo "Is root folder: " & objFolder.IsRootFolderWscript.Echo "Name: " & objFolder.NameWscript.Echo "Parent folder: " & objFolder.ParentFolderWscript.Echo "Path: " & objFolder.PathWscript.Echo "Short name: " & objFolder.ShortNameWscript.Echo "Short path: " & objFolder.ShortPathWscript.Echo "Size: " & objFolder.SizeWscript.Echo "Type: " & objFolder.Type检验文件夹是否存在描述演示脚本使用 FileSystemObject 检验文件夹是否存在。如果文件夹存在,此脚本就绑定到该文件夹。此脚本必须运行在本地计算机上。脚本代码 Set objFSO = CreateObject("Scripting.FileSystemObject")If objFSO.FolderExists("C:\FSO") Then Set objFolder = objFSO.GetFolder("C:\FSO")Else Wscript.Echo "Folder does not exist."End If检验文件是否存在描述演示脚本使用 FileSystemObject 检验文件是否存在。此脚本必须运行在本地计算机上。脚本代码 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枚举一组特定的文件夹描述返回计算机上所有隐藏文件夹的列表。脚本代码strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colFiles = objWMIService.ExecQuery _ ("Select * from Win32_Directory Where Hidden = True")For Each objFile in colFiles Wscript.Echo objFile.NameNext枚举文件夹中的所有文件描述返回 Scripts 文件夹中所有文件的列表。如果计算机有多个文件夹(例如 C:\Scripts 和 D:\Scripts),则文件将从这些文件夹中的每一个返回。脚本代码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枚举网络共享描述列出计算机上所有的共享文件夹。脚本代码strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colShares = objWMIService.ExecQuery("Select * from Win32_Share")For each objShare in colShares Wscript.Echo "AllowMaximum: " & vbTab & objShare.AllowMaximum Wscript.Echo "Caption: " & vbTab & objShare.Caption Wscript.Echo "MaximumAllowed: " & vbTab & objShare.MaximumAllowed Wscript.Echo "Name: " & vbTab & objShare.Name Wscript.Echo "Path: " & vbTab & objShare.Path Wscript.Echo "Type: " & vbTab & objShare.Type Next检索扩展的文件属性描述使用 Shell 对象返回文件夹 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)NextFor Each strFileName in objFolder.Items For i = 0 to 33 Wscript.echo i & vbtab & arrHeaders(i) _ & ": " & objFolder.GetDetailsOf(strFileName, i) NextNext检索文件版本信息描述演示脚本使用 FileSystemObject 检索 .dll 文件的版本。此脚本必须运行在本地计算机上。脚本代码 Set objFSO = CreateObject("Scripting.FileSystemObject")Wscript.Echo objFSO.GetFileVersion("c:\windows\system32\scrrun.dll")枚举已发布共享描述在 Active Directory 中枚举所有已经发布的共享文件夹。脚本代码Const ADS_SCOPE_SUBTREE = 2Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = "ADsDSOObject"objConnection.Open "Active Directory Provider"Set objCOmmand.ActiveConnection = objConnectionobjCommand.CommandText = "Select Name, unCName, ManagedBy from " _ & "'LDAP://DC=Fabrikam,DC=com' where objectClass='volume'"objCommand.Properties("Timeout") = 30 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = False Set objRecordSet = objCommand.ExecuteobjRecordSet.MoveFirstDo Until objRecordSet.EOF Wscript.Echo "Share Name: " & objRecordSet.Fields("Name").Value Wscript.Echo "UNC Name: " & objRecordSet.Fields("uNCName").Value Wscript.Echo "Managed By: " & objRecordSet.Fields("ManagedBy").Value objRecordSet.MoveNextLoop,From to Earth - 小事成就大事,细节成就完美" /> 文件和文件夹VBS脚本大全 - From to Earth

文件和文件夹VBS脚本大全

文件和文件夹vbs脚本大全枚举文件夹中的所有文件
描述
返回 Scripts 文件夹中所有文件的列表。如果计算机有多个文件夹(例如C:\Scripts 和 D:\Scripts),则文件将从这些文件夹中的每一个返回。
脚本代码
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

移动文件
描述
演示脚本使用 FileSystermObject 将文件从一个位置移到另一个位置。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.log" , "D:\Archive"

通过日期查找文件夹
描述
查找在 2002 年 3 月 1 日之后创建的所有文件。要修改此脚本,您必须修改赋给变量 dtmTargetDate 的值中的下列项:
?
2002 -- 将此项更改为目标年(如 1999)。
?
03--将此项更改为目标月(01 代表 1 月、 02代表 2月……12 代表 12月)
?
01 -- 将此项更改为目标天(01 代表该月的第一天、02 代表第二天)。
?
-420 -- 为了确保具有正确的结果,可以将此项更改为您所在的时区和格林尼治标准时间之间的偏移量。如果您不知道此偏移量,可以使用脚本确定与格林尼治标准时间的时区偏移量。
脚本代码
On Error Resume Next
dtmTargetDate = "20020301000000.000000-420"
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory Where CreationDate >
'" & _
        dtmtargetDate & "'")
For Each objFolder in colFolders
    Wscript.Echo objFolder.Name
Next

逐个字符地读取文本文件
描述
演示脚本使用 FileSystemObject 逐个字符地读取文本文件,并分别将这些字符显示在屏幕上。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\New Text
Document.txt", 1)
Do Until objFile.AtEndOfStream
    strCharacters = objFile.Read(1)
    Wscript.Echo strCharacters
Loop

识别 Shell 对象动词
描述
为 Recycle Bin 返回 Shell 对象动词(上下文菜单项)的列表。
脚本代码
Const RECYCLE_BIN = &Ha&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(RECYCLE_BIN)
Set objFolderItem = objFolder.Self
Set colVerbs = objFolderItem.Verbs
For i = 0 to colVerbs.Count - 1
    Wscript.Echo colVerbs.Item(i)
Next

生成文件名
描述
演示脚本使用 FileSystemObject 的 GetTempName 方法生成随机文件名。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
For i = 1 to 10
    strTempFile = objFSO.GetTempName
    Wscript.Echo strTempFile
Next

创建和命名文本文件
描述
演示脚本使用 FileSystemObject 的 GetTempName 方法生成一个文件名,然后根据该名称创建一个文件。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = "C:\FSO"
strFileName = objFSO.GetTempName
strFullName = objFSO.BuildPath(strPath, strFileName)
Set objFile = objFSO.CreateTextFile(strFullName)
objFile.Close
objFSO.DeleteFile(strFullName)

创建网络共享
描述
创建一个名为 FinanceShare 的共享文件夹,将同时连接的最大数量设置为25,然后添加共享描述。
脚本代码
Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 25
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set objNewShare = objWMIService.Get("Win32_Share")
errReturn = objNewShare.Create _
    ("C:\Finance", "FinanceShare", FILE_SHARE, _
        MAXIMUM_CONNECTIONS, "Public share for the Finance
group.")
Wscript.Echo errReturn

创建文本文件
描述
演示脚本创建一个新的空文本文件。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\FSO\ScriptLog.txt")

创建文件夹
描述
演示脚本使用 FileSystemObject 创建一个文件夹。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\FSO")

发布共享文件夹
描述
在 Active Directory 中发布共享文件夹,给该文件夹指派一个描述和三个关键字。
脚本代码
Set objComputer = GetObject _
    ("LDAP://OU=Finance, DC=fabrikam, DC=com")
Set objShare = objComputer.Create("volume",
"CN=FinanceShare")
objShare.Put "uNCName", "\\atl-dc-02\FinanceShare"
objShare.Put "Description", "Public share for users in the
Finance group."
objShare.Put "Keywords", Array("finance", "fiscal",
"monetary")
objShare.SetInfo

在文件上执行操作
描述
使用 Shell 对象打印 C:\Logs 文件夹中的所有文件。
脚本代码
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

按照从下到上的顺序读取文本文件
描述
演示脚本使用 FileSystemObject 读取一个文本文件,并以相反的顺序显示该文本文件(即从最后一行开始,以第一行结束)。
脚本代码
Dim arrFileLines()
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt",
1)
Do Until objFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objFile.ReadLine
    i = i + 1
Loop
objFile.Close
For l = Ubound(arrFileLines) to LBound(arrFileLines) Step
-1
    Wscript.Echo arrFileLines(l)
Next

创建新文件夹
描述
使用 Shell 对象创建名为 C:\Archive 的新文件夹。
脚本代码
ParentFolder = "C:\"
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.NameSpace(ParentFolder)
objFolder.NewFolder "Archive"

监视文件修改
描述
任何时候修改文件 C:\Scripts\Index.vb,临时事件消费程序都会发出警告。最好运行在 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

监视文件删除
描述
任何时候从 C:\Scripts 文件夹删除文件,临时事件消费程序都会发出警告。最好运行在 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.GroupComp'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo
objLatestEvent.TargetInstance.PartComponent
Loop

复制文件夹
描述
演示脚本使用 FileSystemObject 将文件夹复制到新的位置。此脚本必须运行在本地计算机上。
脚本代码
Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Scripts" , "C:\FSO" , OverWriteFiles

监视文件创建
描述
任何时候在 C:\Scripts 文件夹中创建文件,临时事件消费程序就发出警告。最好在 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.GroupComp'Win32_Directory.Name=""c:\\\\scripts""'")
Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
    Wscript.Echo
objLatestEvent.TargetInstance.PartComponent
Loop

复制文件
描述
演示脚本使用 FileSystemObject 来复制文件。此脚本必须运行在本地计算机上。
脚本代码
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\",
OverwriteExisting

复制文件集
描述
演示脚本使用 FileSystemObject 将文件夹中的所有 .txt 文件复制到一个新的位置。
脚本代码
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\*.txt" , "D:\Archive\" ,
OverwriteExisting

解析路径名
描述
演示脚本使用 FileSystemObject 临时返回路径名信息,其中包括文件名、文件扩展名、完整文件路径等等。此脚本必须运行在本地计算机上。
脚本代码
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)

将文本文件读入数组
描述
演示代码使用 VBScript Split 命令从逗号分隔的值文件读取一行,然后将该行中的各个项放入一个数组。
脚本代码
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\scripts\servers and services.txt", ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscript.Echo "Server name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
        Wscript.Echo "Service: " & arrServiceList(i)
    Next
Loop

解压缩文件夹
描述 解压缩文件夹 C:\Scripts。
脚本代码
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory where name =
'c:\\Scripts'")
For Each objFolder in colFolders
    errResults = objFolder.Uncompress
    Wscript.Echo errResults
Next

将网络共享映射到本地文件夹
描述
使用查询的 WMI Associators 返回计算机上所有网络共享的本地路径。
脚本代码
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set colShares = objWMIService.ExecQuery _
    ("Associators of {Win32_Share.Name='Scripts'} Where "
_
        & "AssocClass=Win32_ShareToDirectory")
For Each objFolder in colShares
    Wscript.Echo objFolder.Name
Next

将所有的网络共享映射到本地文件夹
描述
使用查询的 WMI Associators 返回名为 Scripts 的网络共享的本地路径。
脚本代码
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set colShares = objWMIService.ExecQuery _
    ("Select * From Win32_Share")
For Each objShare in colShares
    Set colAssociations = objWMIService.ExecQuery _
        ("Associators of {Win32_Share.Name='" &
objShare.Name & "'} " _
            & " Where AssocClass=Win32_ShareToDirectory")
    For Each objFolder in colAssociations
        Wscript.Echo objShare.Name & vbTab & objFolder.Name
    Next
Next

枚举文件夹属性
描述
演示脚本使用 FileSystemObject 枚举文件夹的属性。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Scripts")
Wscript.Echo "Date created: " & objFolder.DateCreated
Wscript.Echo "Date last accessed: " &
objFolder.DateLastAccessed
Wscript.Echo "Date last modified: " &
objFolder.DateLastModified
Wscript.Echo "Drive: " & objFolder.Drive
Wscript.Echo "Is root folder: " & objFolder.IsRootFolder
Wscript.Echo "Name: " & objFolder.Name
Wscript.Echo "Parent folder: " & objFolder.ParentFolder
Wscript.Echo "Path: " & objFolder.Path
Wscript.Echo "Short name: " & objFolder.ShortName
Wscript.Echo "Short path: " & objFolder.ShortPath
Wscript.Echo "Size: " & objFolder.Size
Wscript.Echo "Type: " & objFolder.Type

检验文件夹是否存在
描述
演示脚本使用 FileSystemObject 检验文件夹是否存在。如果文件夹存在,此脚本就绑定到该文件夹。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\FSO") Then
    Set objFolder = objFSO.GetFolder("C:\FSO")
Else
    Wscript.Echo "Folder does not exist."
End If

检验文件是否存在
描述
演示脚本使用 FileSystemObject 检验文件是否存在。此脚本必须运行在本地计算机上。
脚本代码
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

枚举一组特定的文件夹
描述
返回计算机上所有隐藏文件夹的列表。
脚本代码
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("Select * from Win32_Directory Where Hidden = True")
For Each objFile in colFiles
    Wscript.Echo objFile.Name
Next

枚举文件夹中的所有文件
描述
返回 Scripts 文件夹中所有文件的列表。如果计算机有多个文件夹(例如 C:\Scripts 和 D:\Scripts),则文件将从这些文件夹中的每一个返回。
脚本代码
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

枚举网络共享
描述
列出计算机上所有的共享文件夹。
脚本代码
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set colShares = objWMIService.ExecQuery("Select * from
Win32_Share")
For each objShare in colShares
    Wscript.Echo "AllowMaximum: " & vbTab &
objShare.AllowMaximum
    Wscript.Echo "Caption: " & vbTab & objShare.Caption
    Wscript.Echo "MaximumAllowed: " & vbTab &
objShare.MaximumAllowed
    Wscript.Echo "Name: " & vbTab & objShare.Name
    Wscript.Echo "Path: " & vbTab & objShare.Path
    Wscript.Echo "Type: " & vbTab & objShare.Type
Next

检索扩展的文件属性
描述
使用 Shell 对象返回文件夹 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
Next

检索文件版本信息
描述
演示脚本使用 FileSystemObject 检索 .dll 文件的版本。此脚本必须运行在本地计算机上。
脚本代码
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wscript.Echo
objFSO.GetFileVersion("c:\windows\system32\scrrun.dll")

枚举已发布共享
描述
在 Active Directory 中枚举所有已经发布的共享文件夹。
脚本代码
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, unCName, ManagedBy
from " _
    & "'LDAP://DC=Fabrikam,DC=com' where
objectClass='volume'"
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    Wscript.Echo "Share Name: " &
objRecordSet.Fields("Name").Value
    Wscript.Echo "UNC Name: " &
objRecordSet.Fields("uNCName").Value
    Wscript.Echo "Managed By: " &
objRecordSet.Fields("ManagedBy").Value
    objRecordSet.MoveNext
Loop

文章来自:
引用通告: 查看所有引用 | 我要引用此文章
Tags: 文件 文件夹 VBS 脚本
相关日志:
评论: 0 | 引用: 0 | 查看次数: 570
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭