# Thread: Trying to copy txt files with system()

1. ## Trying to copy txt files with system()

Hi There!
I'm trying to copy all the txt files in a directory and move to another directory.
But I keep getting "System cannot locate file" error.
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
system("copy c:\\Documents and Settings\\my_user_name\\My Documents *.txt c:\\");
system("pause");

return 0;
}
I'm a bit new to C and trying to learn so any help would be appreciated.

Also in this instance I would use my own user name for this code, but what if I didn't know the user name, could I pass a variable instead?
Something like this pseudo code'
Code:
get(system user name from system) = %username;
Then pass it like this:
Code:
path\\to\\%username\\directory

2. I think this line needs some corrections:
Code:
    system("copy c:\\Documents and Settings\\my_user_name\\My Documents *.txt c:\\");
1] after My Documents you should have \\ and moreover no space between \\ and *.txt
2] if the path is correct, but I don't know what version of Windows you are using, you
should get your result. I think you should enclose the path inside double quotes as well.
3] to be sure there is some *.txt file in your path/folder, try to display first its content:

Code:
    system("dir \"c:\\Documents and Settings\\my_user_name\\My Documents\\*.txt\"");
of course my_user_name should be replaced with a valid user.

Start from here.

3. Code:
system("dir \"c:\\Documents and Settings\\my_user_name\\My Documents\\*.txt\"");
Funny, that works, I have 5 txt files in that folder.
But I still get the "file not located" error when it tries to copy them.
BTW this is on a WINXP machine

Code:
int main()

{
system("dir \"c:\\Documents and Settings\\rocko\\My Documents\\*.txt\"");

system("copy \"c:\\Documents and Settings\\rocko\\My Documents\\*.txt\ c:\\"");
system("pause");

return 0;
}
When I try double quotes I get a couple of "missing termination" errors
I could be putting the double quotes in the wrong way.

4. When you issue the DIR command in the dos box do you use quotes around the path?

5. Yes, I put the code into my program and ran it.
That part of the program runs fine, returns list of 5 txt files.

6. You're not understanding. DOS does not support long file names. The newer Windows shells (cmd), but not DOS. You need to use the 8.3 version of the file names, or you need to use the full path in quotes at the shell, assuming it actually understands long file names.

Quzah.

7. Not sure what you're saying quzah, the path is in quotes.
Code:
system("copy \ "c:\\Documents and Settings\\rocko\\My Documents\\*.txt c:\\"");

8. Yes, but once it hits the shell, it's no longer in quotes. Actually, you've totally got that wrong.

system( "copy \"c:\\Documents and Settings\\rocko\\My Documents\\*.txt\" c:\\" );

That's more like it.

Quzah.

9. Originally Posted by quzah
Yes, but once it hits the shell, it's no longer in quotes. Actually, you've totally got that wrong.

system( "copy \"c:\\Documents and Settings\\rocko\\My Documents\\*.txt\" c:\\" )

Quzah.
I new I had it wrong, just needed some one like you to show me the light
That totally works now.

Now on to the second part of my quest.
What if I don't know the user name, is there some way of using a variable to pass the user name?>>check my first post
Also How would I copy different file types?
Say I want to copy both txt and jpg files.

Code:
 "\\*.txt\" \\*.jpg\"
Would I have to include another set of quotes??

system("copy \"c:\\Documents and Settings\\%USERNAME%\\My Documents\\*.txt\" "\\*.jpg\ c\\"