copying files

This is a discussion on copying files within the C Programming forums, part of the General Programming Boards category; I am trying to copy files from one directory to another. What I don't know how to do is get ...

  1. #1
    gls
    gls is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    Question copying files

    I am trying to copy files from one directory to another. What I don't know how to do is get a file name from a directory using ansi C.

    Any help would be appreciated.

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    It depends on what compiler you are using. So what compiler are you using? If you are using VC++6 than you can do whatever you might want to do from the command prompt with the system command, for example run this (if you have VC++6):
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    	system("dir c:");
    	return 0;
    }
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    gls
    gls is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    copying files

    I am using MS VC++ 6. I tried the system function but all it does is print the directory to the screen. I still don't see how I can get the file names into a variable.

    Thanks for your response.

    Garth

  4. #4
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	system("dir c:\\ >> c:\\log.txt");
    	return 0;
    }
    Put it into a text file first. Can you take it from there?
    I don't want to show you how to create a computer virus. I hope thats not what you want the information for.
    Last edited by Witch_King; 09-05-2001 at 10:11 AM.
    I compile code with:
    Visual Studio.NET beta2

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Ohh come on. Just use a switch

    dir c:\\ /B
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    gls
    gls is offline
    Registered User
    Join Date
    Sep 2001
    Posts
    6
    Witch_King,

    I already got your previouse response and I'm doing fine. I didn't post the last response you just replied to.

    gls

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	system("dir /B c:\\ >> c:\\log.txt");
    	return 0;
    }
    This could save some time. Notice the /B switch.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    That way you just get the filenames. Nice and clean. Yup I got your email.
    I compile code with:
    Visual Studio.NET beta2

  9. #9
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    	system("dir /B c:\\\"Program Files\" >> c:\\log.txt");
    	return 0;
    }
    Also say you want to get into a folder with some spaces in the name. Than put a backslash before the quote like this.
    I compile code with:
    Visual Studio.NET beta2

  10. #10
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want to avoid using the slow system routine, since your using MSVC you could do something like this -

    Code:
    #include <windows.h>
    
    int main() 
    { 
       char filename1[]="sourcedirectory\\*.*";
       char filename2[255]="targetdirectory\\";
       BOOL exists=0;
       HANDLE filehandle;
       WIN32_FIND_DATA find_data;
    
       filehandle = FindFirstFile(filename1,&find_data);
       strcat(filename2,find_data.cFileName);
       CopyFile(find_data.cFileName,filename2,exists);
       strcpy(filename2,"targetdirectory\\");
    
       while(FindNextFile(filehandle,&find_data))
       {
            strcat(filename2,find_data.cFileName);
            CopyFile(find_data.cFileName,filename2,exists);
            strcpy(filename2,"targetdirectory\\");
       }
    
     
        return 0;
    } 

  11. #11
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    How does it work? What is the input?
    I compile code with:
    Visual Studio.NET beta2

  12. #12
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    filename1 - the source directory + *.* (to indicate to FindNextFile() that all files are required).

    filename2 - the target directory (which is then appended with the filename that FindFirstFile/FindNextFile produces).

  13. #13
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    int main() 
    { 
    	char filename1[]="c:\\Temp\\*.txt";
       char filename2[255]="c:\\";
    ...
    If I had a .txt file in the c:\temp directory isn't it supposed to copy it to the c:\ directory? Just trying to figure it out... Where did you go? I'm trying to get this thing to work...
    Last edited by Witch_King; 09-05-2001 at 01:58 PM.
    I compile code with:
    Visual Studio.NET beta2

  14. #14
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Sorry, FindFirstFile() doesn't return the full path name you'd have to something like this -

    Code:
    #include <windows.h>
    
    
    int main() 
    { 
       char filename1[]="c:\\temp\\*.txt";
       char filename2[255]="c:\\";
       char fullpath[255]={0};
       BOOL exists=0;
       HANDLE filehandle;
       WIN32_FIND_DATA find_data;
    
       SetCurrentDirectory("c:\\temp\\");
       
       filehandle = FindFirstFile(filename1,&find_data);
       strcat(filename2,find_data.cFileName);
       GetFullPathName(find_data.cFileName,sizeof(fullpath),fullpath,0);
       CopyFile(fullpath,filename2,exists);
       strcpy(filename2,"c:\\");
    
       while(FindNextFile(filehandle,&find_data))
       {
            strcat(filename2,find_data.cFileName);
            GetFullPathName(find_data.cFileName,sizeof(fullpath),fullpath,0);
            CopyFile(fullpath,filename2,exists);
            strcpy(filename2,"c:\\");
       }
    
     
        return 0;
    } 

  15. #15
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Yeah, that son of a ***** worked. Good job. I'll keep this program.
    I compile code with:
    Visual Studio.NET beta2

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using mmap for copying large files
    By rohan_ak1 in forum C Programming
    Replies: 6
    Last Post: 05-13-2008, 08:12 AM
  2. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  3. Copying Files
    By HLA91 in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2007, 03:24 AM
  4. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  5. copying files
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2002, 02:41 AM

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