Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File.Exists(), File.Delete() wildcards

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,

Does the function in subject takes wildcards?
I have the code

Code:
If File.Exists(lcSelectedDir & "*.*") Then
   ' Some code
End If

and it never gets inside If - End If structure even though there are a slew of files in that lcSelectedDir.
I have found that Directory.GetFiles(String, String) can be used instead (tested, it worked.)
However, I also need somethihng like Delete(sPath & "*.*"), that is again: wild cards.
Apparently, File object cannot work with wildcards.
Additionally, Directory object has no Delete() method, and I need something like that.
I could've roll-er-own function, e.g. DeleteFiles(sPath & "*.*"), but I hope there is already such thing, and I wouldn't be "re-inventing the wheel".
Hence teh Q.:

Is there, in .NET, a function Delete() files that takes wildcards?

TIA!

Regards,

Ilya
 
No, but use GetFiles (which does) instead, and check the count of files returned
 
Already done it.
You answered faster than I was able to finish editing the original post.

How about Delete(sPath & "*.*")? Is in .NET anything that does it? (I couldn't find it so far...)

TIA!

Regards,

Ilya
 
Just enumerate the result of getfiles, deleting each item as you go
 
IOW, roll-er-own...
OK, CAN-DO.
Thank you!

Regards,

Ilya
 
Yep!

And, upon reflection, if it were me, I'd actually use system.io.DirectoryInfo.EnumerateFiles rather than GetFiles. It is more flexible, lighter-weight and faster. But it was only introduced in .Net 4.0, so GetFiles remains the generic solution
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top