ADO DOT NET Posted February 9, 2007 Posted February 9, 2007 Kill(My.Application.Info.DirectoryPath & "\*.txt") I am deleting all text files with the above command. but if there is no file I will get an error I want to check first, and if there's at least one file with .txt ext. then run that command. but don't know how to check if there is a file with a specific ext. ? the name maybe anything... Quote
Administrators PlausiblyDamp Posted February 9, 2007 Administrators Posted February 9, 2007 System.IO.Directory.GetFiles can be used to check if any files match your criteria. You may just want to catch there error though rather than checking first if there is the potential for new files to be created between the check and then the deletion... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 11, 2007 Author Posted February 11, 2007 It simply returns an error: System.IO.Directory.GetFiles("c:\*.txt") I want to check if there is at least one .txt file in the specified path, possible? Quote
Administrators PlausiblyDamp Posted February 11, 2007 Administrators Posted February 11, 2007 http://msdn2.microsoft.com/en-us/library/wz42302f.aspx Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted February 11, 2007 Author Posted February 11, 2007 If Directory.GetFiles(My.Application.Info.DirectoryPath, "*.txt") IsNot Nothing Then ... End If Always occurs! :confused: Quote
*Experts* mutant Posted February 11, 2007 *Experts* Posted February 11, 2007 That will not work because even if there are no files matching the pattern, the method will still return an array (an empty one). Check the size of the returned array, if it is 0 then there are no files that match the requirement, if > 0 then you got some files. Quote
SonicBoomAu Posted February 11, 2007 Posted February 11, 2007 Could you use: If File.Exists (My.Application.DirectoryPath, "*.txt") then File.Delete End If or Do While File.Exists (My.Application.DirectoryPath, "*.txt") = True File.Delete Loop Quote Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -- Rick Cook, The Wizardry Compiled
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.