So to delete a specific file, call the FileSystemObject and then use the DeleteFile function to delete that specific file through specifying it's path and it's extension. Here is an example:
Set obj = CreateObject("Scripting.FileSystemObject") 'Calls the File System Object
obj.DeleteFile("C:\MyFolder\MyFile.txt") 'Deletes the file throught the DeleteFile function
In this case, the VB Script will delete a .txt (text file) file called MyFile found in Drive C, Folder MyFolder...
Sometimes you may to delete multiple files, for instance all files in a folder, here is how you can do that:
It is basically the same code as above but there is a little change in the path, were instead of adding the file name with the extension we add a *(asterisk) followed with the file extension which will allow us to delete all files contained in that directory with that extension for example:
obj.DeleteFile("C:\MyFolder\*.txt") 'Deletes all files with the extension .txt in the folder MyFolder
Here is a working demonstration which deletes all files including those that are read only .txt files in a folder after creating a variable DeleteReadOnly and set it to true.
Const DeleteReadOnly = True
Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("C:\MyFolder\*.txt"),DeleteReadOnly
If we did not create the DeleteReadOnly variable, only non read only files would be deleted and read only files will stay, here is an example if you wish to delete only non read only files
Set obj = CreateObject("Scripting.FileSystemObject")
obj.DeleteFile("C:\MyFolder\*.txt")
This was the VB Script tutorial to delete files in windows, i hope this helps, good luck in your VB Script programming





MultiQuote




|