Thread: Showing Files

  1. #1
    drdroid
    Guest

    Question Showing Files

    Is there a c++ function that allows you to show all the txt files in a folder? I'm using the Borland C++ Compiler V4.52.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    You can use the WinAPI functions FindFirst, FindNext and FindClose

    Here's a sample function I wrote:
    Code:
    bool outputDirectory(ostream &outputFile, const char *pcszPath)
    {
    	WIN32_FIND_DATA FindFileData;
    	HANDLE hFind;
    
    	hFind = FindFirstFile(pcszPath, &FindFileData);
    
    	if (hFind == INVALID_HANDLE_VALUE) 
    	{
    		return 1;
    	} else {
    
    		do {
    
    			outputFile << FindFileData.cFileName << endl;
    
    		} while (FindNextFile(hFind, &FindFileData));
    	}
    	FindClose(hFind);
    	return 0;
    }

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I suppose I should show how to use the function...


    Code:
    #include <windows.h>
    #include <fstream.h>
    
    bool outputDirectory(ostream &outputFile, const char *pcszPath)
    {
    	WIN32_FIND_DATA FindFileData;
    	HANDLE hFind;
    
    	hFind = FindFirstFile(pcszPath, &FindFileData);
    
    	if (hFind == INVALID_HANDLE_VALUE) 
    	{
    		return 1;
    	} else {
    
    		do {
    
    			outputFile << FindFileData.cFileName << endl;
    
    		} while (FindNextFile(hFind, &FindFileData));
    	}
    	FindClose(hFind);
    	return 0;
    }
    
    int main (int argc, char *argv[])
    {
    	ofstream fout("c:\\list.txt", ios::trunc);
    
    	outputDirectory(fout, "c:\\*.txt");  // This will print out all files with the extension txt.
    	cout << "Done" << endl;
    	fout.close();
    	
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  4. Folders and text files
    By tim545666 in forum C++ Programming
    Replies: 11
    Last Post: 02-17-2002, 04:15 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM