0

I am working on a program which requires me to, at multiple points, import the contents of a subdirectory of my C++ Project, into a vector<string>. Assuming the project directory is called root\ and the directory I want to scan is root\userFiles. This folder only contains additional files and no further subfolders.

However the trick is I am restricted, I cannot use any of the boost, dirent.h header files. Just the basic ones.

One solution I was able to come up with was the use of the command system( "dir /b * > userFiles/fileList.txt" ); and then filter the results in by reading that file and importing it into the vector.

My issue is how do I "cd" to that folder, run that command while still in that folder, and then exit back to the root folder..

I have tried using the system("chdir userFiles/")command but I am still getting all the files in the root folder.

Any help would be appreciated.

shadoweye14
  • 773
  • 2
  • 7
  • 22
  • If you use `chdir` (or `_chdir` it may be under Windows) to move to another directory FIRST, then run `system`, it would work as you expect. Otherwise there are plenty of questions with good answers describing how you actually get the names of files of a directory on here. – Mats Petersson Feb 28 '15 at 23:46
  • As I have specified in the question. `chdir` does not seem to work. I get a list of all the files in the whole of root folder. As for the answers, most of them require me using some sort of third party header file which I cannot use. – shadoweye14 Feb 28 '15 at 23:51
  • I'm assuming you are using "windows", in which case you should be able to use `` right? If you can't use `windows.h`, then I would use fstream to build a batch file that contains the `"cd directory-you-want\ndir /b"`. – Mats Petersson Feb 28 '15 at 23:57
  • Like this one: http://stackoverflow.com/questions/6320573/how-to-get-list-of-files-in-a-directory-programatically – Mats Petersson Feb 28 '15 at 23:58
  • I cannot use `windows.h` haha. Fortunately I have worked out a way to run all those commands in the same system command. `chdir userFiles && dir /b * > fileList.txt && chdir ../`. Works like a charm. Thanks a lot for the help :) – shadoweye14 Mar 01 '15 at 00:02
  • Who sets these bizarre requirements - and more importantly, to what purpose? Pure sadism or misguided belief that the student will actually learn something more than that the teacher is a sadist? – Mats Petersson Mar 01 '15 at 00:07
  • Haha, tell me about it. This whole project I can have it done in a week if only the req were removed haha :'( – shadoweye14 Mar 01 '15 at 01:14

1 Answers1

0

So I worked out an easier way of doing this.

int main()
{
system("chdir userFiles && dir /b * > fileList.txt");
}

I am using the && operator to join together multiple strings of commands. Since I am only using the system() function once all these commands will be part of the same process.

shadoweye14
  • 773
  • 2
  • 7
  • 22
  • No point in doing `chdir ...` - the "new" directory is only active whilst the shell process is still active, so when you the command completes, the parent process will still have its current working directory where it used to be. – Mats Petersson Mar 01 '15 at 00:09