vbscript Deleting an existing folder and creating a new Folder


Example

Methods used:

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

The following example illustrates the Deletion and creation of a folder using the methods "DeleteFolder" and "CreateFolder".

Code:

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

Similarly, One can Delete a File using the "DeleteFile" method:

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