20130812

VBSCript 檔案系統處理

檔案處理
取得基本檔名 getbasename

path = "c:\a_a\filename.xyz.123.abc.txt" 
set fso = createobject("scripting.filesystemobject") 
getbase = fso.getbasename(path) 
msgbox getbase

目錄處理

取得目錄名稱 Folder.Name

列舉目錄下所有檔案

使用 Folder.Files 方法。

strFolder = "C:\Documents and Settings\Mark\Desktop\VBS Sample"
Set FSO = CreateObject("Scripting.FileSystemObject") 
set FLD = FSO.GetFolder(strFolder) 
For Each Fil In FLD.Files 
    MsgBox Fil.Name 
Next

列舉目錄下整個子目錄

使用 Folder.SubFolders 方法。

strFolderToSearch = "C:\Windows\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
Set colSubfolders = objRootFolder.SubFolders
For Each objFolder in colSubfolders
    strOutput = strOutput & objFolder.name 
    strOutput = strOutput & vbCrLf 
Next

判斷目錄是否存在 FolderExists

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\FolderName") Then
    MsgBox "The folder exists!"
Else
    MsgBox "The folder doesn't exists!"
End If


建立目錄 FS.CreateFolder方法

strDirectory = "c:\logs"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
WScript.Quit


複製整個目錄 Folder.copyFolder(dst)

利用 Folder.copyFolder 方法可以遞迴的複製整個目錄到 dst 指定的目錄下。



沒有留言:

張貼留言