Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

FindFirstFileA function (fileapi.h)

Feedback

In this article

Searches a directory for a file or subdirectory with a name that matches a specific name (or partialname if wildcards are used).

To specify additional attributes to use in a search, use theFindFirstFileEx function.

To perform this operation as a transacted operation, use theFindFirstFileTransacted function.

Syntax

HANDLE FindFirstFileA(  [in]  LPCSTR             lpFileName,  [out] LPWIN32_FIND_DATAA lpFindFileData);

Parameters

[in] lpFileName

The directory or path, and the file name. The file name can include wildcard characters, for example, an asterisk(*) or a question mark (?).

This parameter should not beNULL, an invalid string (for example, an empty stringor a string that is missing the terminating null character), or end in a trailing backslash (\).

If the string ends with a wildcard, period (.), or directory name, the user must have access permissions tothe root and all subdirectories on the path.

By default, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, prepend "\\?\" to the path. For more information, seeNaming Files, Paths, and Namespaces.

Tip

Starting with Windows 10, Version 1607, you can opt-in to remove the MAX_PATH limitation without prepending "\\?\". See the "Maximum Path Length Limitation" section ofNaming Files, Paths, and Namespaces for details.

[out] lpFindFileData

A pointer to theWIN32_FIND_DATA structure thatreceives information about a found file or directory.

Return value

If the function succeeds, the return value is a search handle used in a subsequent call toFindNextFile orFindClose, and thelpFindFileData parameter contains information about the first file or directoryfound.

If the function fails or fails to locate files from the search string in thelpFileName parameter, the return value isINVALID_HANDLE_VALUE and the contents oflpFindFileData areindeterminate. To get extended error information, call theGetLastError function.

If the function fails because no matching files can be found, theGetLastError function returnsERROR_FILE_NOT_FOUND.

Remarks

TheFindFirstFile function opens a search handle andreturns information about the first file that the file system finds with a name that matches the specifiedpattern. This may or may not be the first file or directory that appears in a directory-listing application (suchas the dir command) when given the same file name string pattern. This is becauseFindFirstFile does no sorting of the search results. Foradditional information, seeFindNextFile.

The following list identifies some other search characteristics:

  • The search is performed strictly on the name of the file, not on any attributes such as a date or a file type (for other options, seeFindFirstFileEx).
  • The search includes the long and short file names.
  • An attempt to open a search with a trailing backslash always fails.
  • Passing an invalid string,NULL, or empty string for thelpFileName parameter is not a valid use of this function. Results in this case are undefined.
Note  In rare cases or on a heavily loaded system, file attribute information on NTFS file systems may not be current at the time this function is called. To be assured of getting the current NTFS file system file attributes, call theGetFileInformationByHandle function.
 
After the search handle is established, you can use it to search for other files that match the same pattern by using theFindNextFile function.

When the search handle is no longer needed, close it by using theFindClose function, notCloseHandle.

As stated previously, you cannot use a trailing backslash (\) in thelpFileNameinput string forFindFirstFile, therefore it may not beobvious how to search root directories. If you want to see files or get the attributes of a root directory, thefollowing options would apply:

  • To examine files in a root directory, you can use "C:\*" and step through the directory by usingFindNextFile.
  • To get the attributes of a root directory, use theGetFileAttributes function.
Note  Prepending the string "\\?\" does not allow access to the root directory.
 

On network shares, you can use anlpFileName in the form of the following:"\\Server\Share\*". However, you cannot use anlpFileNamethat points to the share itself; for example, "\\Server\Share" is not valid.

To examine a directory that is not a root directory, use the path to that directory, without a trailingbackslash. For example, an argument of "C:\Windows" returns information about thedirectory "C:\Windows", not about a directory or file in"C:\Windows". To examine the files and directories in"C:\Windows", use anlpFileName of"C:\Windows\*".

Be aware that some other thread or process could create or delete a file with this name between the time youquery for the result and the time you act on the information. If this is a potential concern for your application,one possible solution is to use theCreateFile function withCREATE_NEW (which fails if the file exists) orOPEN_EXISTING(which fails if the file does not exist).

If you are writing a 32-bit application to list all the files in a directory and the application may be runon a 64-bit computer, you should call theWow64DisableWow64FsRedirection functionbefore callingFindFirstFile and callWow64RevertWow64FsRedirection after thelast call toFindNextFile. For more information, seeFile System Redirector.

If the path points to a symbolic link, theWIN32_FIND_DATA buffer contains information aboutthe symbolic link, not the target.

In Windows 8 and Windows Server 2012, this function is supported by the following technologies.

TechnologySupported
Server Message Block (SMB) 3.0 protocolYes
SMB 3.0 Transparent Failover (TFO)Yes
SMB 3.0 with Scale-out File Shares (SO)Yes
Cluster Shared Volume File System (CsvFS)Yes
Resilient File System (ReFS)Yes
 

Examples

The following C++ example shows you a minimal use ofFindFirstFile.

#include <windows.h>#include <tchar.h>#include <stdio.h>void _tmain(int argc, TCHAR *argv[]){   WIN32_FIND_DATA FindFileData;   HANDLE hFind;   if( argc != 2 )   {      _tprintf(TEXT("Usage: %s [target_file]\n"), argv[0]);      return;   }   _tprintf (TEXT("Target file is %s\n"), argv[1]);   hFind = FindFirstFile(argv[1], &FindFileData);   if (hFind == INVALID_HANDLE_VALUE)    {      printf ("FindFirstFile failed (%d)\n", GetLastError());      return;   }    else    {      _tprintf (TEXT("The first file found is %s\n"),                 FindFileData.cFileName);      FindClose(hFind);   }}

For another example, seeListing the Files in a Directory.

Note

The fileapi.h header defines FindFirstFile as an alias that automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that is not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, seeConventions for Function Prototypes.

Requirements

RequirementValue
Minimum supported clientWindows XP [desktop apps | UWP apps]
Minimum supported serverWindows Server 2003 [desktop apps | UWP apps]
Target PlatformWindows
Headerfileapi.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See also

File Management Functions

FindClose

FindFirstFileEx

FindFirstFileTransacted

FindNextFile

GetFileAttributes

SetFileAttributes

Symbolic Links

Using the Windows Headers

WIN32_FIND_DATA


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?