vbscript-delete-a-folder

VBScript - Delete A Folder

This script allows you to delete a folder.

VBScript

  • 218
  • 114
  • 0
  • 0
Add to collection
© 2021VBAmacros.net

1 Open your favorite text editor. In our case we are going to use Notepad++

2 Copy the VBScript code from below.

3 Paste the code in the newly created file.


4 Save the file with a ".vbs" extension.


5 Run the script file by double-clicking on it or via the CMD console.


6 That's it!

Advertisement

Code
' This script deletes a folder.
Sub deleteFolder()
    ' Set variables
    Dim fso
    Dim folderName
    Dim folderPath
    Dim folderToBeDeleted
    Dim msg
    ' Set folder path
    folderPath = "F:\VBAmacros\VBScript\"
    ' Set folder name
    folderName = "My Folder"
    ' Ser folder to be deleted
    folderToBeDeleted = folderPath & folderName
    ' Create a file system object
    Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
    ' Check if the folder already exists
    If Not fso.FolderExists(folderToBeDeleted) Then
        ' Set a message
        msg = "Warning! " & folderName & " not found!"
        ' Output a message
        MsgBox msg
    Else
        ' Delete folder
        fso.DeleteFolder(folderToBeDeleted)
        ' Check if folder still exists
        If Not fso.FolderExists(folderToBeDeleted) Then
            ' Set a message
            msg = "Success! " & folderName & " was successfully deleted!"
            ' Output a message
            MsgBox msg
        Else
            ' Set a message
            msg = "Error! " & folderName & " was not deleted!"
            ' Output a message
            MsgBox msg
        End If
    End If
    ' Release the file system object
    Set fso = Nothing
End Sub
' Run the procedure
Call deleteFolder()
Advertisement


Comments
Sort by:
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
VBAmacros.net VBAmacros.net
Code was successfully copied!
VBAmacros.net VBAmacros.net
Please sign in!
VBAmacros.net VBAmacros.net
You've already voted!