Computing.Net > Forums > Programming > VB Script for deleting files

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB Script for deleting files

Reply to Message Icon

Original Message
Name: Plowman33
Date: August 30, 2008 at 07:16:22 Pacific
Subject: VB Script for deleting files
OS: XP
CPU/Ram: Intel 8 GB
Model/Manufacturer: HP
Comment:

I have a script that I'm trying to use to delete files off a server. I have a .txt file with the location of the files needing deleting. How do I get the VB script to look line for line through the txt file and delete the files listed there?

Here's the VB stuff I have so far:

'Created by Larry Plowman
On Error Resume Next

Dim strFiles
Dim objFileListing
Dim strDeleteFile
Dim objFSO
Const ForReading = 1

set objFSO = CreateObject("Scripting.FileSystemObject")
strDeleteFile = InputBox("Enter path to text document with files to be deleted:")
Set objFileListing = objFSO.OpenTextfile(strDeleteFile,ForReading)

Do Until objFileListing.AtEndOfStream
strFiles = objFileListing.ReadLine
Del ("strFiles")
Loop
objDeleteFile.Close
WScript.Echo "File Deletion Complete."

and here's what the txt file lines will look like:

\\laj-dm-01\c$\LAJ-RDR1\laj-rdr1\MSG\SVS\SVM\SVM\my voting assistance brief.ppt

Only about 3,000 lines worth of stuff like that. Thus the necessity for a script. :)


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: August 30, 2008 at 19:31:12 Pacific
Reply: (edit)

Changes are in (a brighter shade of) red:

Dim strFiles
Dim objFileListing
Dim strDeleteFile
Dim objFSO
Const ForReading = 1

set objFSO = CreateObject("Scripting.FileSystemObject")
strDeleteFile = InputBox("Enter path to text document with files to be deleted:")
Set objFileListing = objFSO.OpenTextfile(strDeleteFile,ForReading)

Do Until objFileListing.AtEndOfStream
objFSO.DeleteFile objFileListing.ReadLine
Loop
objFileListing.Close
WScript.Echo "File Deletion Complete."



Report Offensive Follow Up For Removal

Response Number 2
Name: Plowman33
Date: August 31, 2008 at 09:31:02 Pacific
Reply: (edit)

Thanks for the help. I figured it was something with the syntax. I've just recently been teaching myself scripting, so I'm still learning as I go. I added a few things to give a notification to help track the numbers as well. Thanks again.


EDIT:
I just found a little problem with another part of it. When the InputBox pops up, if you cancel out of it, it doesn't kill the wscript process in windows. Any idea how to do that?


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: August 31, 2008 at 14:52:33 Pacific
Reply: (edit)

strDeleteFile = InputBox("Enter path to text document with files to be deleted:")
If IsEmpty(strDeleteFile) Then _
WScript.Quit 1


Report Offensive Follow Up For Removal

Response Number 4
Name: Plowman33
Date: September 2, 2008 at 01:37:06 Pacific
Reply: (edit)

Awesome. Works like a charm. Here's the whole thing, (After a few modifications in case anyone wants to borrow from it:

'Created by Larry Plowman

On Error Resume Next
Dim folder, fso, outputText, count, strFileToDelete
folder = InputBox("Enter path to text document with files to be deleted:")
If IsEmpty(folder) Then
WScript.Quit 1
Else
Set fso = CreateObject("Scripting.FileSystemObject")
Set outputText = fso.OpenTextFile(folder, 1, false, 0)
count = 0
Do Until outputText.AtEndofStream
strFileToDelete = outputText.ReadLine
if fso.FileExists(strFileToDelete) Then
Wscript.echo "the file " & strFileToDelete & " is deleted"
fso.deletefile trim(strFileToDelete), True
count = count + 1
End If
Loop
Wscript.echo "Total " & count & " files are deleted"
set fso = Nothing
End if


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software