FindNextFile()

This is a discussion on FindNextFile() within the C++ Programming forums, part of the General Programming Boards category; I learned this function from adrianxw, but it simply skips the names with spaces. What should I do? Is there ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    FindNextFile()

    I learned this function from adrianxw, but it simply skips the names with spaces. What should I do? Is there any other function to do it?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    it will not do that if you give FindFirstFile() the right parameters.
    Code:
    FindFirstFile("*.*" ...)
    Or, post your code.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    obvious questions --
    1. what is the purpose of opening the file then closing it right away if open succeeds other than testing if the user running the program has permissions to open file file. Testing for mere existance of the file is unnecessary.

    2. is there code somewhere you did not post that calls FindNextFile()?

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    This program vaerifies the files. It is a file checker. Now I'll give you whole the function.
    Code:
    int checkfiles(char path[], char Folder[][MAXDIRLEN],DWORD index, DWORD &nFolder, DWORD &nBad)
    {
    	HANDLE hFind;
        WIN32_FIND_DATA FindData;
    	int ErrorCode;
    	nBad=nFolder=0;
    	bool Continue= true;
    	char fpat[MAXFPATH]="";
    	
    	ifstream fIn;
    	
    	strcat(path, "\\");
    	strcpy(fpat,path);
    	strcat(path, "*.*");
    
    	for(int i =0;i<index;i++) Folder[i][0]='\0';
    	
    // Find the first file
    
        hFind = FindFirstFile(path, &FindData);
        
    	if(hFind == INVALID_HANDLE_VALUE)
        {
            ErrorCode = GetLastError();
            if (ErrorCode == ERROR_FILE_NOT_FOUND)
                cout << "There are no files matching that path/mask\n" << endl;
            
            else
                cout << "FindFirstFile() returned error code " << ErrorCode << endl;
            
    		Continue=false;
        }
        else
        {
    		
    		path[strlen(path)-4]='\0';
    		
    		
            if(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    		{
    			strcpy(fpat,path);
    			strcat(fpat,"\\");
    			strcat(fpat, FindData.cFileName);
    		
    			cout << fpat;
    			fIn.open(fpat);
    			cout.width(5);
    			if(fIn.is_open()){
    				cout<<cout.fill('.')<< "OK.";
    				fIn.close();
    			}
    				
    			else{
    				nBad++;
    				cout<<cout.fill('.')<< "Failed.<<<\a";
    			}
    			cout<<'\n';
    			
    		}
    		else{
    			strcpy(Folder[nFolder], FindData.cFileName);
    			nFolder++;
    		}
    		
    		
    		
        }
    	if(Continue)
    	{
    		while (FindNextFile(hFind, &FindData) )
    		{
    			cout<<  FindData.cFileName  <<endl;
    			if(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    			{
    				strcpy(fpat,path);
    				strcat(fpat,"\\");
    				strcat(fpat, FindData.cFileName);
    				cout << fpat;
    				fIn.open(fpat);
    				cout.width(5);
    				if(fIn.is_open()){
    					fIn.close();
    					cout<<cout.fill('.')<<"OK.";
    				}
    				else{
    					nBad++;
    					cout<<cout.fill('.')<< "Failed.<<<\a";
    				}
    				cout<<'\n';
    				
    			}
    			else{
    				if(nFolder < index) {
    					strcpy(Folder[nFolder], FindData.cFileName);
    					nFolder++;
    				}
    			}
    
    		}
    
    		ErrorCode = GetLastError();
    
            if (ErrorCode == ERROR_NO_MORE_FILES)
                cout << endl << "All files checked: "<<nBad<<" files could not be opened!" << endl;
    		
            else
                cout << "FindNextFile() returned error code " << ErrorCode << endl;
    
    		if (!FindClose(hFind))
    		 {
    		   ErrorCode = GetLastError();
    		    cout << "FindClose() returned error code " << ErrorCode << endl;
    		}
    	}   
    	
    	return ErrorCode;
    }
    
    int main()
    {
    	INPUT_RECORD InRec;
    	DWORD nFolder[3], nBad, NumRead;
    	
    	char Folder[10][MAXnFOL][MAXDIRLEN];
    	char path[2][MAXDPATH]={{'\0'}};
    	cout.width(45);
    	COORD nsize={100,30000};
    	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),nsize);
    	cout <<"--File Tester--"<<endl;
        cout << "Input Folder Path: " ;
    	cin >> path[0];
    	checkfiles(path[0], Folder[0],MAXnFOL, nFolder[0],nBad);
    
    		for(int i=2;i<nFolder[0];i++) 
    		{
    			strcpy(path[1],path[0]);
    			strcat(path[1],"\\");
    			strcat(path[1],Folder[0][i]);
    			cout<<"Checking Sub Directory: "<<path[1]<<"\n\n";
    			checkfiles(path[1], Folder[1],MAXnFOL, nFolder[1],nBad);
    
    		}
    	
    	if(nBad>0) cout<<'\a';
        cout<<"Press a key to exit..." <<endl;
    	do{
    		ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE),
    					        &InRec,
    					        1,
    					        &NumRead);
    	}while (InRec.EventType != KEY_EVENT);
        return 0;
    }
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Please help, I need it.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Bumping == a bad idea;
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I compiled and ran your program using VC++ 6.0 compier. Only problem I found was in the line
    Code:
    	SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT  _HANDLE),nsize);
    the STD_OUTUT _HANDLE has a space in the middle of the macro.

    Otherwise, it runs ok for me, as long as I don't enter any spaces to the initial prompt "Input Folder Path:"

    I also had to generate my own values for macros used in your program -- you didn't post them so maybe they are set too small values. I put this at the top of the program, after the includes.
    Code:
    const int MAXDIRLEN = _MAX_PATH;
    const int MAXFPATH = _MAX_PATH;
    const int MAXnFOL = 5;
    const int MAXDPATH = _MAX_PATH;

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Code:
    #define MAXDIRLEN 400
    #define MAXFPATH 556
    #define MAXDPATH 500
    #define MAXnFOL 400
    the STD_OUTUT _HANDLE has a space in the middle of the macro.
    It is corrent in mine.

    Create a folder with the name "Aa dd" in C: then enter C: in prompt. It wont look at it.

    Bumping == a bad idea;
    It is not bumping. Dragon told me to send the code, but he didn't gave an answer.
    Last edited by siavoshkc; 03-02-2006 at 04:17 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you are probably missing it on the screen.
    Code:
                                  --File Tester--
    Input Folder Path: c:\temp
    ..
    ab cd
    tempvt.exe
    c:\temp\tempvt.exe.... OK.
    
    All files checked: 0 files could not be opened!
    Checking Sub Directory: c:\temp\ab cd
    
    ..
    
    All files checked: 0 files could not be opened!
    Press a key to exit...

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Very confusing. I've done it twice on two seperate machines.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FindFirstFile and FindNextFile
    By scwizzo in forum Windows Programming
    Replies: 15
    Last Post: 03-12-2008, 04:50 PM
  2. Msdn example code (FindFirstFile, FindNextFile
    By Probose in forum Windows Programming
    Replies: 2
    Last Post: 09-22-2006, 04:16 PM
  3. m_dir = system("dir c:")
    By Ray Schmidt in forum Windows Programming
    Replies: 4
    Last Post: 03-09-2003, 08:39 AM
  4. directories, lccwin32 and findnextfile
    By ggs in forum Windows Programming
    Replies: 6
    Last Post: 07-23-2002, 10:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21