Funkce Dir

Vrátí jméno souboru, adresáře nebo všech souborů a adresářů jednotky nebo adresáře, který splňuje zadanou vyhledávací podmínku.

Syntaxe:


Dir [(PathName As String [, Attributes As Integer])]

Návratová hodnota:

Řetězec

Parametry:

PathName: Any string expression that specifies the search path, directory or file. This argument can only be specified the first time that you call the Dir function. If you want, you can enter the path in URL notation.

Attributes:Any integer expression that specifies bitwise file attributes. The Dir function only returns files or directories that match the specified attributes. You can combine several attributes by adding the attribute values:

0 : Normální soubory

16 : Vrátí pouze název adresáře.

Tímto atributem ověříte, zda soubor nebo adresář existuje, nebo určíte všechny soubory a složky v určitém adresáři.

Chcete-li zjistit, zda soubor existuje, zadejte kompletní cestu a název souboru. Pokud soubor nebo adresář neexistuje, funkce Dir vrátí prázdný řetězec ("").

Chcete-li vygenerovat seznam všech existujících souborů v určitém adresáři, postupujte takto: poprvé zavolejte funkci Dir s kompletní vyhledávací cestou pro soubory, např. "D:\Files\*.ods". Je-li cesta správná a vyhledávání nalezne alespoň jeden soubor, funkce Dir vrátí název prvního souboru, který odpovídá cestě. Další názvy souborů získáte opětovným voláním funkce Dir, ovšem bez argumentů.

Chcete-li vrátit jen adresáře, použijte parametr atributů. To stejné platí, pokud chcete určit název jednotky (např. pevného disku).

Chybové kódy:

5 Neplatné volání procedury

53 Soubor nenalezen

Příklad:


Sub ExampleDir
' Zobrazí všechny soubory a adresáře
Dim sPath As String
Dim sDir As String, sValue As String
    sDir="Adresáře:"
    sPath = CurDir
    sValue = Dir$(sPath + getPathSeparator + "*",16)
    Do
        If sValue <> "." And sValue <> ".." Then
            If (GetAttr( sPath + getPathSeparator + sValue) And 16) >0 Then
                ' Načtení adresářů
                sDir = sDir & chr(13) & sValue
            End If
        End If
        sValue = Dir$
    Loop Until sValue = ""
    MsgBox sDir,0,sPath
End Sub