How to Delete files and folders with Visual Basic script (VBS)




(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)



(5.00 out of 5)





How to Delete files and folders with Visual Basic script (VBS)
Log in to answer.
Copyright © dBuggr LLC - All Rights Reserved.
nishantbaxi 2:24 am on May 13, 2010
1.
Step 1
Open a plain text editor like Notepad.exe.
2.
Step 2
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
‘===========================
3.
Step 3
Save your file as somethin.vbs where something is a name of your choosing. I suggest using the 8.3 naming convention from the good old DOS days.
4.
Step 4
Create a folder and files to test the script making sure the folder name and file names match what is in the script.
5.
Step 5
Run CMD from start/run. In explorer, drag the saved script file to the CMD window and hit enter. The files and folder should now be deleted.