Thread: Can't find all the files (GETTING FUSTERATED!!!)

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Angry Can't find all the files (GETTING FUSTERATED!!!)

    I have been going over this code for a day now with no prevail...

    Run this code, it says that there is less files in C:\WINNT\ than there really is. WHY!?!?

    Please help!
    August.

    Code:
    #define _WIN32_WINNT 0x0501
    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    #include <tchar.h>
    int FindFilesNow(HWND,char[],bool);
    int FindFilesNow_(bool,char[]);
     
    int end(int i)
    {
       return i;
    }
     
    int count_ion = 0;
     
    int FindFilesNow_(bool isFile, char fileName[])
    {
      count_ion++;
      return 0;
    }
     
    char title[] = "dhhg";
    int main(int argc, char *argv[])
    {
      FindFilesNow(0,"C:\\WINNT\\",false);
      return 0;
    }
    int FindFilesNow(HWND hwnd, char path[], bool DisplayErrors)
    {
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind = INVALID_HANDLE_VALUE;
       DWORD dwError;
       char DirSpec[MAX_PATH + 1];  // directory specification
       strcpy (DirSpec, path);
       strcat (DirSpec, "\\*");
       
       hFind = FindFirstFile(DirSpec, &FindFileData);
       if (hFind == INVALID_HANDLE_VALUE) 
       {
    	  if(DisplayErrors == true){
    	  char cab[MAX_PATH + 1];
    	  wsprintf(cab,"Invalid file handle. Error is %u\n",GetLastError());
    	  MessageBox(hwnd,cab,title,MB_OK|MB_ICONERROR);  }
    	  return end(-1);
       } 
       else 
       {
    	  int i;
    	  for(i = 0; DirSpec[i] != '\0'; i++){
    	  if(DirSpec[i] == '*'){
    	  DirSpec[i] = '\\' + 0; } }
    	  printf(DirSpec);
    		 
    	  char source[1000];
    	  lstrcpy(source,DirSpec);
    	  char tempVar1[MAX_PATH];
    	  DWORD dwAttrs = 0;
    	  
    	  lstrcpy(tempVar1,source);
    	  lstrcat(tempVar1,FindFileData.cFileName);
    	  lstrcat(tempVar1,"\\");
    	  if(rename(tempVar1,tempVar1) == 0)
    	  { 
    		 FindFilesNow_(false,tempVar1);
    		 FindFilesNow(hwnd,tempVar1,DisplayErrors);
    	  }
    	  else
    	  {
    		 FindFilesNow_(true,tempVar1);
    	  }
    	  
    	  while (FindNextFile(hFind, &FindFileData) != 0) 
    	  {	  
    		 lstrcpy(tempVar1,source);
    		 lstrcat(tempVar1,FindFileData.cFileName);
    		 lstrcat(tempVar1,"\\");
    		 if(rename(tempVar1,tempVar1) == 0)
    		 { 
    			 FindFilesNow_(false,tempVar1);
    			 FindFilesNow(hwnd,tempVar1,DisplayErrors);
    		 }
    		 else
    		 {
    			 FindFilesNow_(true,tempVar1);
    		 }
    	  }
    	
    	  dwError = GetLastError();
    	  FindClose(hFind);
    	  if (dwError != ERROR_NO_MORE_FILES) 
    	  {
    		 if(DisplayErrors == true){
    		 char cab[MAX_PATH + 1];
    		 wsprintf(cab,"FindNextFile error. Error is %u\n",dwError);
    		 MessageBox(hwnd,cab,title,MB_OK|MB_ICONERROR);  }
    		 return end(-1);
    	  }
       }
       printf("\n\n\nNumber of files in C:\\WINNT\\: %d",count_ion); 
       return end(0);
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    EDIT: Opps....read the code


    Is it a big difference?

    Why are you not testing that is a folder/file before recursively calling the FindFilesNow() function?

    ie FindFileData.dwFileAttributes==FILE_ATTRIBUTE_DIRE CTORY


    What is this supposed to do?

    DirSpec[i] = '\\' + 0;

    or this?

    if(rename(tempVar1,tempVar1) == 0)
    Last edited by novacain; 01-17-2006 at 01:59 AM.
    "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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(rename(tempVar1,tempVar1) == 0)
    What the hell is the point of this?

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> > if(rename(tempVar1,tempVar1) == 0)
    >> What the hell is the point of this?

    If you try to rename a folder with a slash after it to it's own name with a slash after it, it will return 0. But if you try that with a file, is will return -1. Try it, you'll see what I mean.

    >> ie FindFileData.dwFileAttributes==FILE_ATTRIBUTE_DIRE CTORY

    Now when I replace: "if(rename(tempVar1,tempVar1) == 0)" with "if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)", it locks the program up, and Maxes the CPU out.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    More like
    Code:
    if ( ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY )

  6. #6
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Okay, I try that, it will do somthing now, (now that iv'e added, so that the escape key will quit the looking and looping, but still save the resaults.) but look at one of it's resualts:

    E:\\\Documents\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\. \\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\ .\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\ \.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\. \\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\.\\ .\\..\\boot.ini

    "boot.ini" is under "E:\" yes, but not under "E:\Documents\". Also why is it making a lot of slashes and dots?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to make special cases of the two directory entries "." (aka yourself) and ".." (aka your parent).
    Otherwise you keep going up and down on the same spot, just like you're doing here.

    It's not like any of this is explained in the FAQ entry or anything...
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    You can lead a horse to water, but a pencil must be lead.

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Okay, the examples gave me what I needed to know, thanks for your help, Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM
  4. looking for files configuration
    By frigga in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-21-2002, 08:55 AM
  5. Wehre to find good header files for C?
    By SupremeOmnipotence in forum C Programming
    Replies: 1
    Last Post: 01-16-2002, 02:04 PM