Hey there guys, I'm working on a program and I've run into some issues with which directoy things are happening in.
Basically the program is designed to provide the user with a list of programs, once the user selectes on it then provides the user with a list of all of the files with the related extension. Upon selecting a file it will then open the file with the desired program. My problem now is that my launch procedure and my scan procedure seem to operaterating in two seperate directories. Right now I simply have the folder containing the program on my desktop. When it runs it searches withing the folder, but when opening files it tries to open them out of the same folder's subfolder dir. I believe the problem is with the search, because when I copy the .exe to another location, for example the desktop, it will open folders from the desktop, if there happens to be a file with a matching name in my documents. However it won't open if there is no matching file on the desktop.
#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <Windows.h>
usingnamespace std;
usingnamespace std::tr2::sys;
bool ends_with(std::string& file, std::string& ext)
{
return file.size() >= ext.size() && // file must be at least as long as ext
// check strings are equal starting at the end
std::equal(ext.rbegin(), ext.rend(), file.rbegin());
}
void wScan( path f, unsigned i = 0 )
{
directory_iterator d( f );
directory_iterator e;
vector<string>::iterator it2;
std::vector<string> extMatch;
//iterate through all files
for( ; d != e; ++d )
{
string file = d->path();
string ext = ".docx";
//populate vector with matching extensions
if(ends_with(file, ext))
{
extMatch.push_back(file);
}
}
//counter to appear next to file names
int preI = -1;
//display all matching file names and their counter
for(it2 = extMatch.begin(); it2 != extMatch.end(); it2++)
{
preI += 1;
cout << preI << ". " << *it2 << endl;
}
//ask for file selection and assign choice to fSelection
cout << "Enter the number of your choice (or quit): ";
int fSelection;
cin >> fSelection;
cout << extMatch[fSelection] << endl;
//assign the selected file to a string, launch it based on that string and record and output time
string s = extMatch[fSelection];
unsignedlonglong start = ::GetTickCount64();
system(s.c_str());
unsignedlonglong stop = ::GetTickCount64();
double elapsedTime = (stop-start)/1000.0;
cout << "Time: " << elapsedTime << " seconds\n";
}
int main()
{
string selection;
cout << "0. Microsoft word \n1. Microsoft Excel \n2. Visual Studio 11 \n3. 7Zip \n4. Notepad \n Enter the number of your choice (or quit): ";
cin >> selection;
path folder = "..";
if (selection == "0")
{
wScan ( folder );
}
elseif (selection == "1")
{
eScan ( folder );
}
elseif (selection == "2")
{
vScan ( folder );
}
elseif (selection == "3")
{
zScan ( folder );
}
elseif (selection == "4")
{
tScan ( folder );
}
}
I'm also hoping to make changes so that I launch from the main, and have the file types as seperate classes. Including a while loop is also something I need to do so that I can request to open another file once the first has been closed. If you guys could offer any assistance with any of this it would be greatly appreicaited!