Trying to copy txt files with system()

This is a discussion on Trying to copy txt files with system() within the C Programming forums, part of the General Programming Boards category; Hi There! I'm trying to copy all the txt files in a directory and move to another directory. But I ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    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. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    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.
    Last edited by frktons; 07-28-2010 at 07:17 PM.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    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.
    Last edited by acidblue; 07-28-2010 at 07:42 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When you issue the DIR command in the dos box do you use quotes around the path?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Yes, I put the code into my program and ran it.
    That part of the program runs fine, returns list of 5 txt files.
    Last edited by acidblue; 07-28-2010 at 08:15 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    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. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Quote Originally Posted by quzah View Post
    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??
    Last edited by acidblue; 07-28-2010 at 08:54 PM.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Instead of "my_user_name", try "%USERNAME%".
    Microsoft Corporation
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Damn! that was easy.
    What about copying different file types.
    Code:
    system("copy \"c:\\Documents and Settings\\%USERNAME%\\My Documents\\*.txt\" "\\*.jpg\ c\\"
    Last edited by acidblue; 07-28-2010 at 09:03 PM. Reason: spelling mistake

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well it depends how you identify the file "type" I'm assuming that this is done using the extension. If so, just write a function that takes a string as parameter ("txt", for example) and makes that system call for the specific type.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    If you're trying to make this work everywhere bear in mind that the folder isn't necessarily called My Documents, and on more recent versions the 'Documents and Settings' folder is now called Users.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy files in to a different folder help
    By TaiL in forum C Programming
    Replies: 4
    Last Post: 10-15-2009, 02:45 PM
  2. Replies: 12
    Last Post: 10-17-2005, 07:49 AM
  3. Copying system files problems
    By Dark Nemesis in forum Tech Board
    Replies: 11
    Last Post: 03-24-2004, 01:06 AM
  4. Altering files in another file system??
    By Raavin in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2001, 10:18 AM
  5. System Files
    By Thantos in forum C Programming
    Replies: 5
    Last Post: 09-23-2001, 09:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21