Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a single .txt file if exist in Visual Basic?

i have been trying to figure out how to delete a .txt file that constantly change name exept the first 4 ex: THISTEXT-123-45.txt where THISTEXT stays the same but -123-45 changes.

I have found a way to detect it, but i don't know how to delete it.

Dim paths() As String = IO.Directory.GetFiles("C:\", "THISTEXT*.txt")
If paths.Length > 0 Then

Anyone knows the command line to delete that special .txt file? I am using Visual Basic on visual studio 2013 framework 3.5.

like image 768
Maxime.L Avatar asked Sep 21 '25 10:09

Maxime.L


1 Answers

Use the Delete method of System.IO.

Assuming you have write access to C:\

Dim FileDelete As String

FileDelete = "C:\testDelete.txt"

 If System.IO.File.Exists( FileDelete ) = True Then
   System.IO.File.Delete( FileDelete )
   MsgBox("File Deleted")
End If

Deleting a file is quite simple - but dangerous! So be very careful when you're trying out this code.

Edit To delete all file use *(asterisk) followed with the file extension

example C:\*.txt"

For multiple files

Dim FileDelete As String

FileDelete = "C:\"

For Each FileDelete  As String In IO.Directory.GetFiles(FileDelete & "THISTEXT*.txt")
    File.Delete(FileDelete)
Next
like image 135
0m3r Avatar answered Sep 23 '25 08:09

0m3r