How to copy a file from within the folder the c++ program was executed from?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 14
Reputation: Helgso is an unknown quantity at this point 
Solved Threads: 0
Helgso Helgso is offline Offline
Newbie Poster

How to copy a file from within the folder the c++ program was executed from?

 
0
  #1
Nov 2nd, 2008
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:

  1. 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?:

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

Thanks for your time!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #2
Nov 2nd, 2008
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().
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #3
Nov 2nd, 2008
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":
  1. // Windows Platform SDK needed:
  2. #include <windows.h>
  3. ...
  4. 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)...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 14
Reputation: Helgso is an unknown quantity at this point 
Solved Threads: 0
Helgso Helgso is offline Offline
Newbie Poster

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #4
Nov 2nd, 2008
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:

  1. pathSz = GetModuleFileName(0,pathBuf,bufSz);

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

Some extra help here, fellows?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #5
Nov 2nd, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 14
Reputation: Helgso is an unknown quantity at this point 
Solved Threads: 0
Helgso Helgso is offline Offline
Newbie Poster

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #6
Nov 2nd, 2008
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'."
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #7
Nov 2nd, 2008
This seems to work just fine:
  1. #include <stdlib.h>
  2.  
  3. int main()
  4. {
  5. system("move \"folder 1\\test.txt\" \"c:\\test.txt\"");
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #8
Nov 2nd, 2008
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?..
Last edited by ArkM; Nov 2nd, 2008 at 4:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How to copy a file from within the folder the c++ program was executed from?

 
0
  #9
Nov 3rd, 2008
Originally Posted by Helgso View Post
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
  1. void main() //change this
  2.  
  3. //to this
  4. int main()

it should always be int main.

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 14
Reputation: Helgso is an unknown quantity at this point 
Solved Threads: 0
Helgso Helgso is offline Offline
Newbie Poster

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,

  1. #include <direct.h> // for getcwd
  2. #include <stdlib.h>// for MAX_PATH
  3. #include <iostream> // fyor cout and cin
  4. #include <string> // For the use of string
  5.  
  6. using namespace std;
  7.  
  8. // function to return the current working directory
  9. // this is generally the application path
  10. void GetCurrentPath(char* buffer)
  11. {
  12. getcwd(buffer, _MAX_PATH);
  13. }
  14.  
  15. int main()
  16. {
  17.  
  18. // _MAX_PATH is the maximum length allowed for a path
  19. char CurrentPath[_MAX_PATH];
  20. // use the function to get the path
  21. GetCurrentPath(CurrentPath);
  22.  
  23. // display the path for demo purposes only
  24. char temp[_MAX_PATH];
  25. cout << CurrentPath << endl;
  26. cout << "Press Enter to continue";
  27. cin.getline(temp,_MAX_PATH);
  28. return 0;
  29. }

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:

  1. #include <direct.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void GetCurrentPath(char* buffer)
  9. {
  10. getcwd(buffer, _MAX_PATH);
  11. }
  12.  
  13. int main()
  14. {
  15. char CurrentPath[_MAX_PATH];
  16. GetCurrentPath(CurrentPath);
  17.  
  18. string path;
  19. path = CurrentPath;
  20.  
  21. cout << path;
  22. }

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC