Directory.GetFiles and extensions longer than 3 characters

This question pops up in various forums every once in a while. Essentially, someone would call Directory.GetFiles with something like “*.dat” and would be annoyed to find that *.dat matches .dat, .data, and even .date. Not what they wanted.

Unfortunately, this is by design. You have to do post-filtering to select just the files you want.

var files =  Directory.GetFiles(@"d:\tmp", "*.dat").Where(
    s => s.EndsWith(".dat"));

The reason it works this way is most likely for backward compatibility with the old 8.3 file system. And it’s really not a .NET restriction here, it’s an API restriction. Both FindFirstFile and FindNextFile have this behavior and it’s these API calls that are P/Invoked by Directory.GetFiles.

Advertisements

One thought on “Directory.GetFiles and extensions longer than 3 characters

  1. You can call me a Nigromant if you want, but I was trying to deal with Directory.Getfiles thing to get a Directory (and sub directory) size. I have a code like this:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim PDRFolder As New DirectoryInfo(“C:\InputMedia”)
    Dim filesInfo9() As FileInfo = PDRFolder.GetFiles(“*.*”, SearchOption.AllDirectories)
    Dim fileSizePDR As Long = 0
    Dim fileSize9 As Long = 0

    ‘Inicia el calculo de consumo para Playout PDR Folder

    For Each fileInfo9 As FileInfo In filesInfo9
    fileSize9 += fileInfo9.Length
    Next

    fileSizePDR = fileSize9

    lblBytes_PDR.Text = fileSizePDR.ToString
    lblSize_PDR.Text = FormatNumber(fileSizePDR / (1024 * 1024 * 1024), 2) + ” GB” + ” (” + lblBytes_PDR.Text + ” bytes)”

    lblFolder_PDR.Text = PDRFolder.ToString

    lblHours_PDR.Text = CStr(FormatNumber((fileSizePDR / (1024 * 1024 * 1024)) / 15, 2))

    ‘Conversion a formato de tiempo para Playout PDR Folder

    Dim value9 As Decimal = lblHours_PDR.Text
    Dim ts9 As TimeSpan = TimeSpan.FromHours(value9)
    Label5.Text = (String.Format(“{0}h {1}m {2}.{3}s”, ts9.Hours, ts9.Minutes, ts9.Seconds, ts9.Milliseconds))

    hor5 = fileSizePDR

    ‘Fin del calculo de consumo para Playout PDR Folder

    If Val(lblSize_PDR.Text) > 700 Then ‘value in GB
    pbPDR.Visible = True ‘Muestra la alarma en caso el tama;o sea mayor
    tbLog.Text &= System.DateTime.Now.ToString & vbCrLf
    tbLog.Text &= “Límite de almacenamiento superado en PDR” & vbCrLf
    tbLog.SelectionStart = tbLog.TextLength
    tbLog.ScrollToCaret()
    Else
    pbPDR.Visible = False
    End If
    End Sub

    But it has a poorly performance. I have to check 5 differents directories that has in total about 5 TB.

    This is a net framework 2 target project. Can you help me to improve this code?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s