|
  
|
1#
发表于 2007-4-22 06:41 PM
| 只看该作者
VBS-操作文件.文件夹几则实例
VBS-操作文件.文件夹几则实例(上)
1.更改文件夹属性
使用 FileSystemObject 检查文件夹是否隐藏,如果没有隐藏,就隐藏它
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")
If objFolder.Attributes = objFolder.Attributes AND 2 Then
objFolder.Attributes = objFolder.Attributes XOR 2
End If
2.复制指定文件
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\ScriptLog.txt" , "D:\Archive\", OverwriteExisting
3.复制指定文件夹
Const OverWriteFiles = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\Scripts" , "C:\FSO" , OverWriteFiles
4.复制指定文件夹内所有同一格式的文件
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\FSO\*.txt" , "D:\Archive\" , OverwriteExisting
5.使用 Shell 复制文件夹
Const FOF_CREATEPROGRESSDLG = &H0&
ParentFolder = "D:\Archive"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(ParentFolder)
objFolder.CopyHere "C:\Scripts", FOF_CREATEPROGRESSDLG
6.使用 WMI 复制文件夹
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.Copy("D:\Archive")
Wscript.Echo errResults
Next
7.创建文件夹
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\FSO")
8.创建和命名文本文件
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)
9.创建网络共享
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 |
感谢Baidu,Google,Dreams8给我这次机会!
还要感谢我的爸爸妈妈!
HOHO |
|