| | |
How to copy a file from within the folder the c++ program was executed from?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 14
Reputation:
Solved Threads: 0
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:
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?:
Thanks for your time!
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:
C++ Syntax (Toggle Plain Text)
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?:
C++ Syntax (Toggle Plain Text)
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().
You can then call win32 api CopyFile() to copy each of the files in the vector that is returned by TransverseDirectory().
Last edited by Ancient Dragon; Nov 2nd, 2008 at 11:44 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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":
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)...
Use GetModuleFileName Windows API function to "retrieve the full path and filename for the executable file containing the specified module":
C++ Syntax (Toggle Plain Text)
// Windows Platform SDK needed: #include <windows.h> ... pathSz = GetModuleFileName(0,pathBuf,bufSz); // get path to my exe
•
•
Join Date: Sep 2008
Posts: 14
Reputation:
Solved Threads: 0
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:
But CodeBlocks just continued saying: "pathSz, pathBf, bufSz was not declared in this scope."
Some extra help here, fellows?
@ ArkM: I tried adding that header, windows.h, and I tried that line:
C++ Syntax (Toggle Plain Text)
pathSz = GetModuleFileName(0,pathBuf,bufSz);
But CodeBlocks just continued saying: "pathSz, pathBf, bufSz was not declared in this scope."
Some extra help here, fellows?
Google -- http://msdn.microsoft.com/en-us/libr...18(VS.85).aspx
Always read up on functions before you try and use them.
well i think you forgot to declare the variables pathSz, pathBf & bufSz or at least in that scope.
Chris
Always read up on functions before you try and use them.
well i think you forgot to declare the variables pathSz, pathBf & bufSz or at least in that scope.
Chris
Knowledge is power -- But experience is everything
This seems to work just fine:
C++ Syntax (Toggle Plain Text)
#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
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?.. Last edited by ArkM; Nov 2nd, 2008 at 4:01 pm.
•
•
•
•
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'."
C++ Syntax (Toggle Plain Text)
void main() //change this //to this int main()
it should always be int main.
Chris
Knowledge is power -- But experience is everything
•
•
Join Date: Sep 2008
Posts: 14
Reputation:
Solved Threads: 0
Re: How to copy a file from within the folder the c++ program was executed from?
0
#10 Nov 3rd, 2008
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,
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:
Then the program found out the directory and printed it out. This really helped me
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
Anyhow, I found a good code on the net,
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
#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

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
![]() |
Similar Threads
- DNS ERROR>>>CANNOT CONNECT TO SECURE SITES (Viruses, Spyware and other Nasties)
- Highjack file Ie wont download (Viruses, Spyware and other Nasties)
- Kuang2 Virus (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Deleteing end node
- Next Thread: While loop issue
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






