删除现有文件夹并创建新文件夹

使用的方法:

.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