-
Dec 10th, 2015, 03:23 PM
#1
Thread Starter
Member
[RESOLVED] Easy Q About Deleting Files
Just looking for the best way to delete all specific .txt files in a directory.
Code:
Kill("c:/somefolder" , "*.txt")
Kill("C:/somefolder/someotherfolder/" , "*.*")
Would this code work? Or is there a better work around
-
Dec 10th, 2015, 06:49 PM
#2
Re: Easy Q About Deleting Files
You can't just invent methods out of thin air whenever you need them. Have you read the documentation for the Kill function? It has one parameter: a String that specifies the path of the file. What do you think would happen if you passed it the path of a folder and a pattern? Would the code that someone else wrote years ago to delete a single file develop artificial intelligence and know that you wanted it to do something different? No, it would not.
Basically, if you want to delete a file then you issue a delete command for that file. If you want to delete all files of a particular type in a folder then you get a list of all the files of that type in that folder and then you issue a delete command for each one of them, e.g.
Code:
For Each filePath In IO.Directory.GetFiles("c:/somefolder" , "*.txt")
IO.File.Delete(filePath)
Next
-
Dec 12th, 2015, 11:27 PM
#3
Thread Starter
Member
Re: Easy Q About Deleting Files
That's A Very Good Answer. I forgot the Kill Property is only for one file.. haha That code does seem to work great, but what if you want to check first if the directory exists and if so then delete the ".txt" files. That's where I'm getting kind of lost and getting it to work.
Code:
For Each filePath In IO.Directory.GetFiles("c:/somefolder", "*.txt")
If Directory.Exists("C:/somefolder") Then
IO.File.Delete(filePath)
Next
You'd think this would work because we are labeling filepath.. I'm a little cornfused haha
-
Dec 13th, 2015, 05:03 AM
#4
Re: Easy Q About Deleting Files
Originally Posted by jmiller1225
That's A Very Good Answer. I forgot the Kill Property is only for one file.. haha That code does seem to work great, but what if you want to check first if the directory exists and if so then delete the ".txt" files. That's where I'm getting kind of lost and getting it to work.
Code:
For Each filePath In IO.Directory.GetFiles("c:/somefolder", "*.txt")
If Directory.Exists("C:/somefolder") Then
IO.File.Delete(filePath)
Next
You'd think this would work because we are labeling filepath.. I'm a little cornfused haha
Think about what that code is actually doing. It gets all the files from the folder first, then it checks whether the folder exists. Does that make sense? You need to check whether the folder exists first because, if it doesn't, you can't get any files from it.
-
Dec 15th, 2015, 10:36 PM
#5
Thread Starter
Member
Re: Easy Q About Deleting Files
Ive definitely have not been thinking! I have it figured out. My minds been scrambled lately, LIFE ya know? Thanks a lot! Here is the code
Code:
If Directory.Exists("C:/somefolder") Then
For Each filePath In IO.Directory.GetFiles("c:/somefolder", "*.txt")
IO.File.Delete(filePath)
Next
Else
MsgBox("Folder Doesn't Exist")
End If
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|