delete only *.PDF in code given

Ernesto
Ernesto used Ask the Experts™
on
This kind of code delete all files in directory if cheked

          If chkDeleteSourceFiles.Checked Then
                For i As Integer = 0 To oFiles.Length - 1
                    Dim sFromFilePath As String = oFiles(i)
                    If IO.File.Exists(sFromFilePath) Then
                        Try
                            IO.File.Delete(sFromFilePath)
                        Catch ex As Exception
                            txtOutput.Text += "No se puede borrar " & sFromFilePath &
                             ", " & ex.Message & vbCrLf
                        End Try
                    End If
                Next
            End If

Open in new window



That is a mess...
I need if is cheked only delete PDF files.
could you help me ?
regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Robb HillSenior .Net Full Stack Developer

Commented:
Check file type = to pdf.

You could split the file extension and examine for a match that way.
Robb HillSenior .Net Full Stack Developer

Commented:
check this console which has the .net you need.

string fileName = @"C:\mydir.old\myfile.ext";
string path = @"C:\mydir.old\";
string extension;

extension = Path.GetExtension(fileName);
Console.WriteLine("GetExtension('{0}') returns '{1}'", 
    fileName, extension);

extension = Path.GetExtension(path);
Console.WriteLine("GetExtension('{0}') returns '{1}'", 
    path, extension);

Open in new window


Now you have the extenstion..just check against .pdf
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Ernesto,

How about this?

          If chkDeleteSourceFiles.Checked Then
                For i As Integer = 0 To oFiles.Length - 1
                    Dim sFromFilePath As String = oFiles(i)
                    If IO.File.Exists(sFromFilePath) AndAlso (sFromFilePath.EndsWith(".pdf")) Then
                        Try
                            IO.File.Delete(sFromFilePath)
                        Catch ex As Exception
                            txtOutput.Text += "No se puede borrar " & sFromFilePath &
                             ", " & ex.Message & vbCrLf
                        End Try
                    End If
                Next
            End If

Open in new window


Regards,
Chinmay.

Author

Commented:
Oh you guys!!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial