I just want to say "Thanks again" to everyone who helped me get past my roadblocks with C++ ... I don't think I'm there yet, but I did manage my first working program today. Couldn't a done it without ya!
Now the hard part begins....Code:#include <iostream> #include <algorithm> #include <vector> #include <fstream> #include <string> #include <windows.h> #include <shlwapi.h> using namespace std; ///////////////////////////////////////////////////////////////////// // Directory listing in a class // class DirList { private: vector <string> Files; string Path; public: DirList(); DirList(string Folder); ~DirList(); bool GetFiles(string Folder); void ListFiles(); void Sort(); void Randomize(); bool SaveList(string Target); }; bool DirList::GetFiles(string Folder) { Files.clear(); Path = Folder; Folder += "\\*.*"; WIN32_FIND_DATA fd; HANDLE ff = FindFirstFile(Folder.c_str(),&fd); if (ff == INVALID_HANDLE_VALUE) return false; do { if (fd.cFileName[0] != '.') Files.push_back(Path + "\\" + fd.cFileName); } while ( FindNextFile(ff,&fd) ); FindClose(ff); return true; } void DirList::ListFiles(void) { for (unsigned int i = 0; i < Files.size(); i++) cout << Files[i] << endl; } void DirList::Sort(void) { sort(Files.begin(),Files.end()); } void DirList::Randomize(void) { random_shuffle(Files.begin(),Files.end()); } bool DirList::SaveList(string Target) { ofstream outfile(Target.c_str()); if (outfile.fail()) // file not open return false; for (unsigned int i = 0; i < Files.size(); i++) outfile << Files[i] << endl; outfile.close(); return true; } // structors DirList::DirList() { cout << "cold init" << endl; } DirList::DirList(string Folder) { GetFiles(Folder); } DirList::~DirList() { cout << endl << "Finished!" << endl; } //////////////////////////////////////////////////////////////////// // Help splash void ShowHelp(void) { cout << "DirToFile -- Save directory lists in files" << endl; cout << endl; cout << "USAGE : DirToFile -R <TARGET FILE> <SOURCE FOLDER>" << endl; cout << endl; cout << "-R Randomize file list (for playlists)" << endl; cout << endl; cout << "Enclose file and path names with spaces in quotes." << endl; cout << endl; cout << "Example: DirToFile -r Playlist.m3u \"d:\\My Files\\Music\"" << endl; cout << endl << endl; exit(0); } ///////////////////////////////////////////////////////////////////// // Program entry point // int main(int argv, char* argc[]) { // helper splash if (argv < 3) ShowHelp(); // parse command line bool Shuffle = false; string Target = "test.txt"; string Folder = ""; for (int x = 1; x < argv; x++) PathUnquoteSpaces(argc[x]); if (!strcmp(argc[1],"-R") || (!strcmp(argc[1],"-r"))) { Shuffle = true; if (argc[2]) Target = argc[2]; if (argc[3]) Folder = argc[3]; } else { if(argc[1]) Target = argc[1]; if (argc[2]) Folder = argc[2]; } cout << Target << " from " << Folder << endl; if (Target.length() < 3) ShowHelp(); if (Folder.length() < 1) ShowHelp(); // sorted or scrambled DirList Dir(Folder); if (Shuffle) Dir.Randomize(); else Dir.Sort(); // show list on screen Dir.ListFiles(); // write the file Dir.SaveList(Target); return 0; }![]()



LinkBack URL
About LinkBacks




