Thread: Scanning Directories

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    19

    Scanning Directories

    I am trying to traverse through a directory lets say C:/TestDirectory/, within this TestDirectory there is a group of folders and within these folders are files. I am trying to output a text file that prints the following information
    Folder1
    File1.txt
    File2.txt
    Folder2
    File1.txt
    .
    .
    .

    Im on windows XP OS and have searched this on google but haven't found out exactly how to do this ... all help would be appreciated!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    19
    That section on WIN32 seems a bit more complex than what I am trying to do... here is what I have so far
    Code:
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind;
       if( argc != 2 )
       {
          printf("Usage: %s [target_file]\n", argv[0]);
          return;
       }
    
       printf ("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 
       {
          printf ("The first file found is %s\n", FindFileData.cFileName);
          FindClose(hFind);
       }	
       return 0;
    After setting a directory in the command arguments it typically just returns FindFirstFile failed (2). What do I need to alter in this code in order for it to return the file names in this directory.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes WIN32 makes everything complex. I assume you are using some family of Windows Visual Studio 2005 or 2008.

    Are you sure the command argument came in in WIDE form? My guess is that it did not. What does your main header look like? Does it have char* or _TCHAR* in front of argv[]?

    FindFirstFile() takes wide chars. You may need to convert the incoming simple string argv[1] to wide.
    Code:
    WCHAR bufw[500];
    ...
    void convert_to_wide(char *buf, WCHAR *crap) {
    while (*crap++ = *buf++); }
    
    ...
    
    convert_to_wide(argv[1], bufw);
    ...
    hFind = FindFirstFile(bufw, &FindFileData);
    Then to display the returned file name you need to convert back to regular strings...
    Code:
    char buff[500];
    ...
    void convert_to_string(WCHAR *crap, char *buf) {
    // convert wide-char to normal char
    while (*buf++ = (char)*crap++); }
    ...
    convert_to_string(FindFileData.cFileName, buff);
    Or you can keep everything as wide strings throughout - saving all that to-and-from conversions. But I haven't seen a smooth integration of the standard library functions to go with that.
    Last edited by nonoob; 08-13-2009 at 09:14 AM.

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Error 2 is File not found. Don't put a final backslash on the directory string, and you must include a wildcard to get data for anything other than the file/directory you specify.

    Quote Originally Posted by http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx
    To examine a directory that is not a root directory, use the path to that directory, without a trailing backslash. For example, an argument of "C:\Windows" returns information about the directory "C:\Windows", not about a directory or file in "C:\Windows". To examine the files and directories in "C:\Windows", use an lpFileName of "C:\Windows\*".
    FindFirstFile() takes wide chars.
    Only if you're compiling for UNICODE.
    Last edited by adeyblue; 08-13-2009 at 09:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't change directories via system("cd");
    By Frosty in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 10:45 AM
  2. VC6 directories
    By Magos in forum Tech Board
    Replies: 0
    Last Post: 03-11-2005, 05:52 PM
  3. scanning in with gets() but not over inputing
    By stephanos in forum C Programming
    Replies: 1
    Last Post: 09-09-2002, 03:38 PM
  4. stopping some scanning
    By aoe in forum C Programming
    Replies: 4
    Last Post: 06-09-2002, 01:50 PM
  5. Working with directories...
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 04-20-2002, 11:45 AM