How do I use a program that is not in the source folder?

For example: I'm on MS DOS, I have source code in C: \ Documents and Settings \ Programs folder. Can I use source code for a program (like gnuplot) that is in a random folder?

0


source to share


5 answers


Here are some options:

  • Search your PATH for the executable you want to run
  • Allow user to specify location on command line
  • Save the location in a config file and allow the user to specify it during install (if you have an install process) or by manually editing the file


Ideally, you would do all 3

0


source


http://www.codeproject.com/KB/system/newbiespawn.aspx

ShellExecute will consider the PATH environment variable, so you don't need to specify the full PATH. Now, if this is indeed a random location, and it's not even in your PATH environment variable, then I guess you're out of luck.

If they are not even in PATH, you need to find them in the candidates folder. Here's a sample code on how to traverse the filesystem in C ++.

And an example using Boost:

directoryList.h



#ifndef DIRECTORYLIST_H_INCLUDED
#define DIRECTORYLIST_H_INCLUDED
#define BOOST_FILESYSTEM_NO_DEPRECATED

#include <iostream>
#include <list>
#include <string>


class directoryList {

    public:
        directoryList();
        ~directoryList();
        std::list<std::string> getListing(std::string path);
};
#endif // DIRECTORYLIST_H_INCLUDED

      

directoryList.cpp

#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/convenience.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"

#include "directoryList.h"

using namespace std;
namespace fs = boost::filesystem;

directoryList::directoryList() {}
directoryList::~directoryList() {}

list<string> directoryList::getListing(string base_dir) {

    list<string> rv;
    fs::path p(base_dir);

    for (fs::recursive_directory_iterator it(p); 
         it != fs::recursive_directory_iterator(); ++it) {

        string complete_filename = it->path().string();
        rv.insert(rv.begin(),complete_filename);

    }

    return rv;

}

      

Usage example:

directoryList *dl = new directoryList();
filenames = dl->getListing("C:\\Program Files");
//search for the file here, or modify the getListing to supply a filter

      

+2


source


There are also some basic functions of _ exec / exec and its modifications. Similar functionality is available for Linux.

0


source


The location of the source code has nothing to do with how programs are located by the system () call (I assume you are using this call). The only relevant consideration is the location of the compiled executable.

Take note of the PATH environment variable on Windows - this is how programs are found. This is a semicolon-separated list of directories where Windows looks for executables and BAT files and DLLs. In that list, the current directory and (I think) the location where your EXE is located.

PATH is set in Windows XP from the Control Panel widget. Advanced button. It's more complicated for Vista - you need to do it as an administrator.

0


source


As Vinko said, the PATH environment variable determines where Windows will look for program files.

It is usually best to avoid hard-coding the executable path into your compiled program. Even if gnuplot is in a specific folder on your computer, then it may not be in one folder on another computer. This will cause the call to another program to fail. You can save it to the registry and let the user customize the location of the program, or provide the installer that was looking for it.

0


source







All Articles