vbscript FileSystem Objects Deleting an existing folder and creating a new Folder

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any vbscript Question?