0

I want to create a program that opens another .exe file in C++, I want the program to be able to run from any directory, so I will not know the full address of the file.

Eg-
The Program that I make will be present in a folder, which in turn has subfolders, which further contains javaportable.exe. I want the program to run the above mentioned .exe without getting the full path. Something like cd operator in normal DOS navigation. Also, if it is possible, I want to select another .exe, which will run through javaportable.exe, and is present in the previous folder.

Priyank
  • 165
  • 1
  • 8
Rajat Jain
  • 452
  • 1
  • 6
  • 16
  • 1
    Stop using TurboC++, ie. most likely 1993 software, **now**. And I don´t think you have DOS – deviantfan Apr 29 '15 at 05:05
  • This is what is taught in our school. I'm just 17! – Rajat Jain Apr 29 '15 at 06:00
  • Then please change school, because getting taught this crap (and believing it) is harming you. And it doesn´t matter if you´re 17 or 70 or anything. – deviantfan Apr 29 '15 at 06:14
  • @deviantfan I think it's inappropriate to propose changing the school. Not everybody is able to choose the school she wants. It would be more constructive to propose a better environment that can be used at school. Obviously they want to save costs for a commercial system and want a system that's easy to use and requires little resources. – harper Apr 29 '15 at 06:18
  • First of all install some other IDE at your home PC at least like netbeans or Eclipse CDT. Then show us what efforts you have put in for solving the problem because no one here would write the whole code for you. Most of use can help where you're stuck. Do read this too http://stackoverflow.com/questions/1961828/why-not-to-use-turbo-c?rq=1 – Priyank Apr 29 '15 at 07:06
  • @harper Of course not everybody can, but I´m not forcing him to. He just should know that this is completely bad. (Eg. another user said some days ago, he´d prefer hiring someone who never wrote any program and teaching him C++ from scratch instead of hiring someone with that much bad stuff learned, which is hard to getting rid of). And while TurboC++ is/was commercial, and just some parts of some old versions are available for free, GCC and clang are free completely and without any conditions. So that argument doesn´t count. – deviantfan Apr 29 '15 at 08:12
  • @deviantfan It's not about hiring but about pupils at school. The teacher choosed a way that was cheap for him in respect of (1) license costs, (2) requriements for the PC (RAM, CPU clock), (3) learning curve. The Turbo GUI is so limited that the pupils can't do much what is beyond his intend. They probably learn how it files to enter some C code but don't learn what the could reuse in real live. But changing the school isn't an option for most pupils. – harper Apr 29 '15 at 09:18
  • Well, @deviantfan all the schools in my country teach Turbo C++ at the moment. And I can't change my country, just to learn another programming language. It is prescribed syllabus from the government side, and nobody can do much in this. Though they are still "THINKING" to change the syllabus. Did you not read in the link that priyank left? The student is from a university in Mumbai, and they still use TC. I am from a school there, so do you think anyone will be using anything else in schools! – Rajat Jain Apr 29 '15 at 09:20
  • @priyank you don't have to write the whole program, just tell me the function that is used in this. I have heard of system(), but that simply uses full path name, not relative. I have learnt everything about system(), that I could find on stackexchange, but could not find anything pertaining to my problem. The purpose of my program is very different and this is just a part of it, where I am stuck at! – Rajat Jain Apr 29 '15 at 09:20
  • @harper again read my comment ^. Nothing is from the teacher, or the school. Its from the government, and each school must follow it, atleast in India, and all the schools affiliated to CBSE, which are abroad. If they teach something else, the pupils will surely fail in the board exams! – Rajat Jain Apr 29 '15 at 09:27
  • I am still unable to get what exactly you want to do with the program? How could you run a program in Google Cloud Drive? Do you want that your program should take an input for the file name without address and find it in the folder to execute it? – Priyank Apr 29 '15 at 10:13
  • Nope, Don't think of it running on the cloud drive @priyank. Think that it is running in a normal folder, which contains another folder which houses the .exe I want to run. I know the name of that folder, but not its full path. – Rajat Jain Apr 29 '15 at 10:17
  • Okay you want that the program should be running in a folder and should run the program you name which is also in the same folder? – Priyank Apr 29 '15 at 10:23
  • Not in the same folder, but in another folder, which is present in the same folder. @Priyank – Rajat Jain Apr 29 '15 at 10:24
  • Alright so you want that it should run a program present in some subfolder? – Priyank Apr 29 '15 at 10:26
  • Okay I will write a full answer to this question now. – Priyank Apr 29 '15 at 10:39

1 Answers1

1

First of all the standard C++ library does not provide any such functions to list the files of a directory. You can use boost libraries to get this done. Solution to this problem is not that easy. You will have to implement a lot of techniques to get this done. I can give you all the possible starting pointers.

To port this code to other OS you may need to add OS specific code using # preprocessor directives. Boost Liberay is already cross platform.

First of all you need to get the current path of the directory your program is present in:

For this you can use the following windows os specific code

#include <iostream>
#include <direct.h>

int main(){
char *path = NULL;
path = _getcwd(NULL, 0); // or _getcwd
if (path != NULL)
    std::cout << path;
}

Here the path variable contains the address of the current directory. You can pass this path to the next function which will further use it to list the files and directories for you. For listing the directories you can use Boost Liberaries http://www.boost.org/

Next you will need to get all the files and folders present in current directory. Use Boost library for this

Use this sample code from boost http://www.boost.org/doc/libs/1_37_0/libs/filesystem/example/simple_ls.cpp

Then make a class and store the address received and files names with path in its objects. You can store all the listed address in a object like dir[1].str[size], dir[2].str[size],...so on. Now again pass all the folder addresses you received to boost function and get further filenames. All of the above will require many passes. You can also get the list of files for specific file extension too:

#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED 
#include <boost/filesystem.hpp>

namespace fs = ::boost::filesystem;

// return the filenames of all files that have the specified extension
// in the specified directory and all subdirectories
void get_all(const fs::path& root, const string& ext, vector<fs::path>& ret)
{
    if(!fs::exists(root) || !fs::is_directory(root)) return;

fs::recursive_directory_iterator it(root);
fs::recursive_directory_iterator endit;

while(it != endit)
{
    if(fs::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());
    ++it;

}

}

Finally compare the filenames with the one you want to run and execute it.

The problem can be solved by many other methods too but I think it will be good to start and you can always improve your code.

References:

  1. How to get list of files with a specific extension in a given folder
  2. http://www.boost.org/
  3. How do I get a list of files in a directory in C++?

    Hope this helps. If you need any further help feel free to ask!

Community
  • 1
  • 1
Priyank
  • 165
  • 1
  • 8
  • Is this portable on other IDEs? If so, which one do you suggest? Since I cannot run programs by TC on a windows PC. – Rajat Jain Apr 29 '15 at 11:59
  • Yes you can use any IDE you like. But I would definately recommend you to use any of Code Blocks, Eclipse CDT or Netbeans if you're a beginner else Visual Studio Community edition. – Priyank Apr 29 '15 at 12:00
  • Boost only supports compilers from roughly the last decade, If you need support for VC++6, you might find an old Boost version somewhere. But TC++ and Boost is probably doomed to fail. – MSalters Apr 29 '15 at 13:29
  • @MSalters I fully agree. I don't now why our schools still teach this – Rajat Jain Apr 29 '15 at 13:55
  • I have also studied on TC++ till college level. They do it because they have to teach you the basics only. But i am still totally against it because now every school and college should adopt to C++11 standards and that can't be taught on TC. – Priyank Apr 29 '15 at 14:04
  • TC++ doesn't even support C++98. Then again, I suspect there are enough geography lessons still being taught worldwide from text books which still have the Soviet Union. – MSalters Apr 29 '15 at 22:36
  • @priyank, can't we use system (); looks much easier – Rajat Jain Apr 30 '15 at 02:12
  • For which purpose you want to use system ()? – Priyank Apr 30 '15 at 06:57