Hey, does anyone know which function/s i need to use to copy a file from one subdirectory to another, without changing the name? im on windows if that helps.

Recommended Answers

All 12 Replies

You can use system() command to do that.

system("//copy file.txt folder");

Its just telling me my syntax is incorrect.

system("//copy subdirectory/file.txt subdirectory2");

I meant system("copy file.txt folder");
// i gave that as a comment.
For example...

system("copy myfile.txt c:\my\location");

ok, but what if the file i want to copy is in a subdirectory. do i have to give the full path of the file? because i want this program to work if it is anywhere on the hard drive. also, what do i need to put if i want to copy it from the subdirectory to the program directory?

1) If you want the program to work from anywhere on the system. And the file is surely found at the same path. you should prefer writing down the whole path.

2) If the file is in a subdirectory of your program directory. Consider
progdir->subdir->abc.txt
And you want to copy it down into the progdir (Where your program is running from)
then ,

system("copy .\subdir\abc.txt abc.txt");

should do fine.

Use full paths. It reduces ambiguity and errors.

sorry, i dont want to sound anal, but could you be a little more specific? its telling me the file cannot be found.

system("copy .\subdir\abc.txt abc.txt"); /*copy to program directory from subdirectory?*/
system("copy .\subdir\abc.txt .\subdir2\abc.txt"); /*copy from subdirectory to subdirectory?*/

change the file name "abc.txt" to the file you want to copy.

sorry, i dont want to sound anal, but could you be a little more specific? its telling me the file cannot be found.

You don't know what a full path is?

And to fix your code

system("copy .\\subdir\\abc.txt abc.txt"); /*copy to program directory from subdirectory?*/
system("copy .\\subdir\\abc.txt .\\subdir2\\abc.txt"); /*copy from subdirectory to subdirectory?*/

i assumed that a full path was the full path to the file includng drive letter, eg c:\program\data\file.txt

But since you mention it, what is a full path?

yeah its working now, thanks. is it possible to get it to work silently and not output text to the console (1 file(s) copied)

cool thanks. Is there anyway to get it to run without outputting text? (1 file(s) copied)

system("command >NUL");
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.