How Do You Delete Files And Folders With Visual Basic Script (VBS)?
This will give an example of how to create a Visual Basic script that will delete a file and folder. Open a plain text editor like Notepad.exe. Copy and paste the text below into your plain text document. ‘================= On Error Resume Next Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fldr1, FileName1, FileName2 Set wshShell = WScript.CreateObject(“WScript.Shell”) Set fso = CreateObject(“Scripting.FileSystemObject”) ‘Set Folder path to be deleted ‘This can be replaced by using the command line parameter method described in a later article. fldr1 = “C:\goober” ‘Set ini file names FileName1 = “C:\goober\goober.ini” FileName2 = “C:\goober\whataname.INI” ‘Call Subs to cleanup DeleteFolder (fldr1) DeleteFile (FileName1) DeleteFile (FileName2) Function DeleteFolder(FldrPath) If (fso.FolderExists(FldrPath)) then fso.DeleteFolder(FldrPath), True End If End Function Function DeleteFile(File) If (fso.FileExists(File)) then fso.DeleteFile(File), True End If End Function ‘=======