Page 1 of 1

Deleting files in VBscript This tutorial shows you how to delete files in Visual Basic Script in Rate Topic: -----

#1 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Posted 22 February 2009 - 09:52 AM

Sometimes, VB Scripting proves to be very useful in windows as a windows scripting language especially for routine tasks such as file creation, movement, delete, folder and directory creation, and even remote PC shutdown over networks, in this tutorial we are going to cover how to delete a file, multiple files and folders on windows through simple Visual basic Scripting.

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 :)

Is This A Good Question/Topic? 0
  • +

Replies To: Deleting files in VBscript

#2 The Gothic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-June 09

Posted 07 June 2009 - 10:19 AM

Thx a lot, I need things like this. May I have a question? How did you discover this: Scripting.FileSystemObject
Did you learn it from some textbook or you learnt it by yourself. I mean if you were trying and trying...

This post has been edited by The Gothic: 07 June 2009 - 10:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1