刪除現有資料夾並建立新資料夾

使用的方法:

.DeleteFolder(FileSpec, Force (True/False))
.CreateFolder(Path)
.DeleteFile(FileSpec, Force (True/False))

以下示例說明使用DeleteFolderCreateFolder 方法刪除和建立資料夾。

碼:

Dim strFolderPath, objFso
strFolderPath = "C:\Users\GS\Desktop\testFolder"
Set objFso = CreateObject("Scripting.Filesystemobject")

'Checking for the folder's existence and deleting it, if found
If objFso.FolderExists(strFolderPath) then
    objFso.DeleteFolder strFolderPath, True                   'True indicates forceful deletion
End If

'Creating a new Folder
objFso.CreateFolder strFolderPath

Set objFso = Nothing

同樣,可以使用DeleteFile方法刪除檔案 :

Dim strFilePath:strFilePath = "C:\Users\GS\Desktop\tasks.txt"
If objFso.FileExists(strFilePath) then
    objFso.DeleteFile strFilePath, True                      'true indicates forceful deletion
End If