VB icon

File Lister Pro

Email
Submitted on: 8/29/2016 12:21:06 PM
By: Ben  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 1015
 
     This code will show how to list all the files in a selected folder with what ever file type you give it lst c:\work\*.txt Hope you like this code.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: File Lister Pro
// Description:This code will show how to list all the files in a selected folder with what ever file type you give it lst c:\work\*.txt Hope you like this code.
// By: Ben
//**************************************

// By DreamVB
#include <iostream>
#include <Windows.h>
using namespace std;
using std::cout;
using std::endl;
int main(int argc, char *argv[]){
	HANDLE f = 0;
	WIN32_FIND_DATAA wfd;
	char *PathName = NULL;
	char lzPath[128];
	int pos = -1;
	int x = 0;
	if (argc != 2){
		cout << "Usage: <Folder>" << endl;
		exit(1);
	}
	strcpy(lzPath, argv[1]);
	//Convert char to wchar
	PathName = new char[strlen(lzPath) + 1];
	strcpy(PathName, lzPath);
	//Find last backslash
	for (x = 0; x < strlen(PathName); x++){
		if (PathName[x] == '\\'){
			pos = x;
		}
	}
	if (pos != -1){
		PathName[pos + 1] = '\0';
		//Output path
		cout << "Folder Listing For " << PathName << endl << endl;
	}
	else{
		cout << "Folder Listing For \\" << endl << endl;
	}
	//Get first file.
	f = FindFirstFileA(lzPath, &wfd);
	
	//Check for inaild path.
	if (f == INVALID_HANDLE_VALUE){
		cout << "Folder or files not found." << endl << lzPath << endl;
		exit(1);
	}
	//Gets the list of files
	while (FindNextFileA(f, &wfd) != false){
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
			//Left for folders
		}
		else{
			//Output filename.
			cout << wfd.cFileName << endl;
		}
	}
	//Clear up.
	memset(lzPath, 0, sizeof(lzPath));
	delete[]PathName;
	//Close file
	FindClose(f);
	return 0;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.