Thread: FindFirstFile()

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    FindFirstFile()

    What would be the first parameter to this function if I want to find the first file in the directory where my program is? This directory will never be the same. Thanks.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    GetModuleFileName will get the complete path and filename to the current module. PathRemoveFileSpec will remove the filename and last trailing backslash. Finally we use PathIsRoot to check if it is a root directory. If it is, we must add "\\*" before passing the path to FindFirstFile.
    Code:
    #include <shlwapi.h>
    
    TCHAR szPath[MAX_PATH + 10];
    GetModuleFileName(NULL, szPath, MAX_PATH);
    PathRemoveFileSpec(szPath);
    if (PathIsRoot(szPath))
        lstrcat(szPath, TEXT("\\*"));
    
    FindFirstFile(szPath, ...);

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Code:
    #include <stdio.h>
    #include <string.h>
    #include "shlwapi.h"
    #include <windows.h>
    
    int main()
    {
        FILE *out;
        char path[MAX_PATH];
    
        WIN32_FIND_DATA file;
        HANDLE fhandle;
    
        GetModuleFileName(NULL, path, MAX_PATH);
        PathRemoveFileSpec(path);
    
        if(PathIsRoot(path))
            strcat(path, "\\*");
    
        out = fopen("client_list.txt", "w");
        if(out == NULL){
            printf( "Error creating file\n");
            exit(1);
        }
    
        fhandle = FindFirstFile(path, &file);
        if(fhandle == INVALID_HANDLE_VALUE){
            printf("Invalid File Handle.\n");
            exit(1);
        } 
    
        while(FindNextFile(fhandle, &file) != 0){
            if(!file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                fprintf(out, "%s\n", file.cFileName);
        }
    
        fclose(out);
    
        return 0;
    }
    Why isn't this compiling?

    Errors:
    linked list error LNK2019: unresolved external symbol __imp__PathIsRootA@4 referenced in function _main
    c:\Documents and Settings\kao\My Documents\Visual Studio Projects\linked list\linked list\linked list.cpp(33): warning C4806: '&' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant
    linked list error LNK2019: unresolved external symbol __imp__PathRemoveFileSpecA@4 referenced in function _main
    linked list fatal error LNK1120: 2 unresolved externals

    Also, if you have any suggestions about this code, please don't hesitate to post. Thanks

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Unresolved external symbol errors typically mean that you have to link with the correct library. Down the bottom of the documentation page for a Windows function is the library you need to link to. In this case it is shlwapi.lib. In MSVC, you can link to a library using a pragma (usually placed just below the includes):
    Code:
    #pragma comment(lib, "shlwapi.lib")
    The other error relates to this line:
    Code:
    if(!file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    which should be:
    Code:
    if (!(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    try..

    Code:
    //find where app is running
    GetCurrentDirectory(MAX_PATH, szDirectory); //or GetWorkingDirectory()
    //add file name or SetCurrentFolder()
    sprintf(szBuffer, "%s\\%s", szDirectory, szFile);
    //find
    hFindData = FindFirstFile(szBuffer, &FindData);
    include windows.h and link to Kernel32.lib
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    
    int main()
    {
    	FILE *out;
    	char path[MAX_PATH];
    
    	WIN32_FIND_DATA file;
    	HANDLE fhandle;
    	DWORD error;
    
    	error = GetCurrentDirectory(MAX_PATH, path);
    	if(error == 0){
    		printf( "Error\n");
    		exit(1);
    	}
    
    	SetCurrentDirectory(path);
    
    	out = fopen("client_list.txt", "w");
    	if(out == NULL){
    		printf( "Error creating file\n");
    		exit(1);
    	}
    
    	fhandle = FindFirstFile(path, &file);
    	if(fhandle == INVALID_HANDLE_VALUE){
    		printf("Invalid File Handle.\n");
    		exit(1);
    	} 
    
    	while(FindNextFile(fhandle, &file) != 0){
    		if(!(file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    			fprintf(out, "%s\n", file.cFileName);
    	}
    
    	fclose(out);
    
    	return 0;
    }
    This compiles but nothing gets written to the text file (there are files in the directory). What's the reason? Thanks.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    which line fails?
    what happens when you watch the code with a debugger?


    test that the file can be written too, ie print something to the file before and after the filename write loop.

    maybe look at the WIN32 file functions ie CreateFile()
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You need to pass a search mask to FindFirstFile(), not just a directory. If you want all files you need to say *.*. FindFirstFile() returns the first file in the directory, you are discarding it, (although it is usually . and therefore doesn't matter).

    Look at the examples in my tutorial starting here. They are C++ but if you change the headers and cout's to printf's they should work.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FindFirstFile() Always Returns INVALID_HANDLE_VALUE
    By ThLstN in forum Windows Programming
    Replies: 10
    Last Post: 09-05-2008, 12:12 PM
  2. FindFirstFile() and std::string
    By Mostly Harmless in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2008, 06:27 PM
  3. FindFirstFile
    By Coding in forum C# Programming
    Replies: 8
    Last Post: 02-13-2008, 04:49 AM
  4. how do i use FindFirstFile()
    By knight543 in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2002, 08:00 PM
  5. Searching for a file using FindFirstFile() ?
    By Brian in forum Windows Programming
    Replies: 7
    Last Post: 01-27-2002, 02:43 PM