how to check if a directory contains any files or not?if files exist how to delete them in vb?

Recommended Answers

All 11 Replies

Private Sub RemoveDir(ByVal _path As String)
        For Each mDir As String In IO.Directory.GetDirectories(_path)
            My.Computer.FileSystem.DeleteDirectory(mDir, FileIO.DeleteDirectoryOption.DeleteAllContents)
        Next
    End Sub

if the given path has no content then ofc not get deleted

i want to delete files if files exist in directory

then u can do:

if IO.File.exist("path and file here") then
io.file.delete("path and file here")
end if
Private Sub RemoveDir(ByVal _path As String)
        For Each mDir As String In IO.Directory.GetDirectories(_path)
            My.Computer.FileSystem.DeleteDirectory(mDir, FileIO.DeleteDirectoryOption.DeleteAllContents)
        Next
    End Sub

if the given path has no content then ofc not get deleted

i want to delete files if files exist in directory

i dnt knw the filename in the directory

so now im confused....
what exactlly you want? just empty a directory without removing the directory? but then i dont understand why you wanna check if a file exist if u dont know the name o.O

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Dim strDir As String = "C:\testdir\"
		If (System.IO.Directory.Exists(strDir)) Then
			Dim dir As New System.IO.DirectoryInfo("C:\testdir")
			Dim files As System.IO.FileInfo() = dir.GetFiles()
			For Each file As System.IO.FileInfo In files
				System.IO.File.Delete(file.FullName)
			Next
		End If
	End Sub

when a file is uploaded by a user it is stored in a folder.. if he uploades other file in the same folder, the file which was already uploaded by him should be deleted and newly uploaded file must be saved.
files are uploaded by users.. so we dnt know the filename..

if System.IO.File.Exists("fileName") then System.IO.File.Delete("fileName") then save the new file just like you did before when it didn't exist.

ok.. thank you

You're welcome

Please mark this thread as solved if you have found the answer to your original question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.