I'll try to explain. I've got a C++ program that is going to copy files from it's folder which it is located in to another location. So, I'd say f.x. I had a directory called "Flash". Then, inside that directory would be "myprogram.exe" and 4 other directories. Within those 4 directories would be .txt files which I'd want that myprogram.exe to copy to another location.

Because I want the program to be able to be executed anywhere, I want to make the program clear that it is supposed to look in it's self directory. So, how is the command? I know that to copy a file from f.x. C:\program files\test.txt all I have to do is to do:

system("copy C:\\Program Files\\test.txt C:\\Program Files\\output\\test.txt");

So, I'd copy test.txt from "program files" to "program files\\output". But I want the program to read from it's folder, copying from whatever location that folder "Flash" was located in the system, (Flash was the folder that included "myprogram.exe"). Would the command be something similar to?:

system("copy ..\\folder 1\\test.txt C:\\Program files\\test");

Thanks for your time!

If you are using MS-Windows then use FindFirstFile() and FindNextFile() to get the names of all subdirectories, then recursively call the function to process the files in each subdirectory. I have posted a code snipped that may help you here. It will get a list of the files that you need to copy. From main() just pass "." as the first argument to TransverseDirectory().

You can then call win32 api CopyFile() to copy each of the files in the vector that is returned by TransverseDirectory().

It seems we can't "just pass "." as the first argument": the current directory does not bear a relation to the executing module directory.
Use GetModuleFileName Windows API function to "retrieve the full path and filename for the executable file containing the specified module":

// Windows Platform SDK needed:
#include <windows.h>
...
pathSz = GetModuleFileName(0,pathBuf,bufSz); // get path to my exe

Be careful with some "good advices": argv[0] contains a module name but it's not a full path (except when calling from VS IDE)...

Sorry guys but I'm just confused. @ Ancient Dragon: This sounds logic and I tried that command, FindFirstFile() and FindNextFile but I don't know what to do with it xD Are you saying that I should type in the name of the txt file in there, like: FindFirstFile(bla.txt), and it will search the directory the exe is located in, find it, and then what? When I did this, my compiler, CodeBlocks, said: "bla.txt was not declared in this scope." Can you give me an example?

@ ArkM: I tried adding that header, windows.h, and I tried that line:

pathSz = GetModuleFileName(0,pathBuf,bufSz);

But CodeBlocks just continued saying: "pathSz, pathBf, bufSz was not declared in this scope."

Some extra help here, fellows? :-/

The problem is that always when I try new codes from msdn.microsoft.com, my compiler CodeBlocks always returns errors while compiling. I tried those codes but Codeblocks says: "Error: 'main' must return 'int'."

This seems to work just fine:

#include <stdlib.h>

int main()
{
    system("move \"folder 1\\test.txt\" \"c:\\test.txt\"");
}

To Helgso:
Stop absolutely fruitless "copy/paste only" approach to programming as soon as possible.
For example, did you really think that pathSz = GetModuleFileName(0,pathBuf,bufSz); in my previous post was a C++ idiom or a spell? Another case: if your compiler said that main must return int (it's true in standard C++), is it so hard job to change erroneous void main header and try to compile before waste your time and bandwidth on DaniWeb help request?..

The problem is that always when I try new codes from msdn.microsoft.com, my compiler CodeBlocks always returns errors while compiling. I tried those codes but Codeblocks says: "Error: 'main' must return 'int'."

When you use sample code from msdn, you will have to change

void main() //change this

//to this
int main()

it should always be int main.

Chris

Ok thank you people for your patiency and I'm very greatful. I'm a rookie to C++ at them moment, and I didn't know that I had to change void main to int main because I just thought this was a different entrance to the program.

Anyhow, I found a good code on the net,

#include <direct.h> // for getcwd
#include <stdlib.h>// for MAX_PATH
#include <iostream> // fyor cout and cin
#include <string> // For the use of string

using namespace std;

// function to return the current working directory
// this is generally the application path
void GetCurrentPath(char* buffer)
{
    getcwd(buffer, _MAX_PATH);
}

int main()
{

    // _MAX_PATH is the maximum length allowed for a path
    char CurrentPath[_MAX_PATH];
    // use the function to get the path
    GetCurrentPath(CurrentPath);

    // display the path for demo purposes only
    char temp[_MAX_PATH];
    cout << CurrentPath << endl;
    cout << "Press Enter to continue";
    cin.getline(temp,_MAX_PATH);
    return 0;
}

Then I simply could delete the lines, ("Cout << Current..."), ("cout << "press...""), ("cin.getline...") amd ("return 0;"). Then, to convert the char to string to make it executable, all I did was:

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

using namespace std;

void GetCurrentPath(char* buffer)
{
    getcwd(buffer, _MAX_PATH);
}

int main()
{
    char CurrentPath[_MAX_PATH];
    GetCurrentPath(CurrentPath);

    string path;
    path = CurrentPath;

    cout << path;
}

Then the program found out the directory and printed it out. This really helped me :D

If I'm not killing you of boredom xD I'd like to ask another question. How would you actually bind a folder into an .exe? So when I'd execute the exe, it would copy files from itself rather than searching it's directory for files to copy. Thank you again!

P.s. Sorry for the bother

New problem, I've retreived the folders path and added new information to it, now I want the program to copy files from there to another directory which is contained in another string:

int main()
{
    string inputdir;
    string outputdir;
    string inputdir = "C:\\bla.txt\\";
    string outputdir = "C\\test\\";

    system("copy inputdir.c_str outputdir.c_str");
}

Now, what do I have to fix in the system line to make it get the information contained in those strings and look in that path for the file to copy it to the location the second string contains?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.