Function Reference


Enumerates the first stream with a ::$DATA stream type in the specified file or directory

#include <WinAPIFiles.au3>
_WinAPI_FindFirstStream ( $sFilePath, $tData )


$sFilePath The fully-qualified file name.
$tData A $tagWIN32_FIND_STREAM_DATA structure or a pointer to it that receives information about the stream.

Return Value

Success: The search handle.
Failure: 0 and sets the @error flag to non-zero, @extended flag may contain the system error code.


This function function opens a search handle and returns information about the first stream in the specified file or directory.
For files, this is always the default data stream, "::$DATA".
After the search handle has been established, use it in the _WinAPI_FindNextStream() function to search for other streams in the specified file or directory.

If the function fails because no streams can be found, the @extended flag will contain ERROR_HANDLE_EOF (38) system error code.

When the search handle is no longer needed, it should be closed using the _WinAPI_FindClose() function.

This function requires Windows Vista or later.


_WinAPI_FindClose, _WinAPI_FindNextStream

See Also

Search FindFirstStreamW in MSDN Library.


#include <MsgBoxConstants.au3>
#include <WinAPIError.au3>
#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>
#include <WinAPIMem.au3>
#include <WinAPIMisc.au3>
#include <WinAPIShPath.au3>

Local Const $sTempFile = @TempDir & '\Test.txt'

; Check NTFS file system
If StringCompare(DriveGetFileSystem(_WinAPI_PathStripToRoot($sTempFile)), 'NTFS') Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'The file must be on an NTFS file system volume.')

Local $sName, $iBytes, $pData, $hFile
; Create text file with three alternative streams named AS1, AS2, and AS3 respectively
For $i = 0 To 3
    If $i Then
        $pData = _WinAPI_CreateString('Alternative stream ' & $i)
        $sName = ':AS' & $i
        $pData = _WinAPI_CreateString('Main stream')
        $sName = ''
    $hFile = _WinAPI_CreateFile($sTempFile & $sName, 1, 4)
    _WinAPI_WriteFile($hFile, $pData, _WinAPI_GetMemorySize($pData) - 2, $iBytes)

; Enumerate all existing streams in the file and read text data from each stream
$pData = _WinAPI_CreateBuffer(1024)

Local $tFSD = DllStructCreate($tagWIN32_FIND_STREAM_DATA)

Local $hSearch = _WinAPI_FindFirstStream($sTempFile, $tFSD)
Local $iSize
While Not @error
    $sName = DllStructGetData($tFSD, 'StreamName')
    $iSize = DllStructGetData($tFSD, 'StreamSize')
    $hFile = _WinAPI_CreateFile($sTempFile & $sName, 2, 2, 6)
    _WinAPI_ReadFile($hFile, $pData, $iSize, $iBytes)
    ConsoleWrite(StringFormat('%10s (%s bytes) - %s', $sName, $iSize, _WinAPI_GetString($pData)) & @CRLF)
    _WinAPI_FindNextStream($hSearch, $tFSD)

Switch @extended
    Case 38 ; ERROR_HANDLE_EOF

    Case Else
        MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), @extended, _WinAPI_GetErrorMessage(@extended))