Skip to content

Instantly share code, notes, and snippets.

@xandout
Last active May 24, 2019 17:25
Show Gist options
  • Save xandout/8443812 to your computer and use it in GitHub Desktop.
Save xandout/8443812 to your computer and use it in GitHub Desktop.
C++ directory lister
#include <stdio.h>
#include <string>
#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
//Simple struct to return from lsfiles
struct List {
vector<string> files;
vector<string> folders;
};
//All of the hard work
struct List lsfiles(string folder) //(c) http://stackoverflow.com/a/20847429/1009816
{
vector<string> files; //Will be added to List
vector<string> folders; //Will be added to List
char search_path[200];
sprintf(search_path, "%s*.*", folder.c_str());
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path, &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
// read all (real) files in current folder, delete '!' read other 2 default folder . and ..
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{
files.push_back(fd.cFileName);
} else //Put folders into vector
{
folders.push_back(fd.cFileName);
}
}while(::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
List me;
me.files = files;
me.folders = folders;
return me;
}
int main(int argc, char *argv[])
{
string where;
if(argc > 1){where = argv[1] + string("\\");}else{where = "";}
List you = lsfiles(where); //Get contents of directory
vector<string>::iterator files_begin = you.files.begin();
vector<string>::iterator files_end = you.files.end();
vector<string>::iterator folders_begin = you.folders.begin();
vector<string>::iterator folders_end = you.folders.end();
for(; folders_begin != folders_end; folders_begin++){
cout << "[D] " << *folders_begin << "\n";
}
for(; files_begin != files_end; files_begin++){
cout << "[F] " << *files_begin << "\n";
}
return 0;
}
/////////Example output
//PS C:\> mls
//
//[D] $Recycle.Bin
//[D] adobeTemp
//[D] AMD
//[D] android-sdk
//[D] apache-ant-1.9.3
//[D] Documents and Settings
//[D] FWBuilder51
//[D] inetpub
//[D] iSCSIVirtualDisks
//[D] Media
//[D] MinGW-4.8.1
//[D] MSOCache
//[D] PerfLogs
//[D] Program Files
//[D] Program Files (x86)
//[D] ProgramData
//[D] Python27
//[D] Sandbox
//[D] SWTOOLS
//[D] System Volume Information
//[D] TFTP-Root
//[D] UnitTest++-1.3
//[D] Users
//[D] vz
//[D] Windows
//[F] bootmgr
//[F] BOOTNXT
//[F] pagefile.sys
//PS C:\> mls AMD
//[D] .
//[D] ..
//[D] AMD_Catalyst_12.6_Legacy_Win8
//PS C:\>
@morningreis
Copy link

Works well for me. It's an interesting bit of code!

@asadw1
Copy link

asadw1 commented Jan 13, 2016

Just an update here in case anyone is trying to run this in Visual Studio 2013.
Before building a project, go to the Project Properties > General. Switch Character Set to "Use Multi-Byte Character Set". This will stop any errors related to "Cannot convert char[] to LPCWSTR" since it is by default looking for unicode and not multi-byte strings.

More info about this here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c1b08c0a-a803-41c3-ac8c-84eba3be1ddb/faq-cannot-convert-from-const-char-to-lpctstr

@khalili-mahdi
Copy link

khalili-mahdi commented Nov 28, 2018

without this code i get "Cannot convert char[] to LPCWSTR"

    wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
    {
        wchar_t* wString=new wchar_t[4096];
        MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
        return wString;
    }

and change "search_path" to this :
convertCharArrayToLPCWSTR(search_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment