Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying a list of files

Status
Not open for further replies.

cmsamcfe

Programmer
Dec 18, 2002
31
0
0
GB
I have a list of filenames (and paths) that I need to copy from one folder to another. I've set up a for-loop to go through all files but I can't find a filecopy procedure that seems to work?

Can anyone help with this? Specifically, which libraries I need to include etc

Thanks
 
I've done this a couple of ways. I've used CreateProcess to use a command line to copy a bunch of files in a directory.
Code:
    // Create command line
    AnsiString CommandIs = "xcopy " + OrgPath  + "\\*.* " + BUPath + " /i/s/e/h";
    CopyLabel->Caption = "Processing. . .";
    Repaint();
    //Application->MessageBox(CommandIs.c_str(), "Command", MB_OK);  // For testing

    STARTUPINFO StartInfo; // name structure
    PROCESS_INFORMATION ProcInfo; // name structure
    memset(&ProcInfo, 0, sizeof(ProcInfo)); // Set up memory block
    memset(&StartInfo, 0 , sizeof(StartInfo)); // Set up memory block
    StartInfo.cb = sizeof(StartInfo); // Set structure size
    StartInfo.dwFlags = STARTF_USESHOWWINDOW; // Necessary for wShowWindow to work
    StartInfo.wShowWindow = SW_HIDE; // Hide window
    bool Result = CreateProcess(NULL, CommandIs.c_str(), NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo);
    if (Result)
    {
        WaitForSingleObject(ProcInfo.hThread, INFINITE); // wait forever for process to finish
        SetFocus(); // Bring back focus
    }
        else
            Application->MessageBox("Could not copy directory.", "Process Error", MB_OK);

    // Finish up
    CRBUDirectoryListBox->Directory = "\\*.*";  // This forces the directory list box to work better
    CRBUDirectoryListBox->Repaint();
    DoDirectoriesExist();
    Screen->Cursor = crDefault;
    Repaint();

I've also used streams to copy each file. Somewhere in here is a thread about copying a file via binary mode. Somewhere I have some code for that, too but I'm pressed for time right now. Post here if you need an example of that code. James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
An example would be good thank-you. I found a function called "CopyFile" that looked as though it would work for me, but when I implement it in my code, the files are not copied to the directory. Are there any other routines like "copyfile" out there?
 
Ive made a program That I use for personal use that
copies entire directories including any subdirectories.
I used FileRead and FileWrite as the processes that does the copying. I tested it on copying the entire program files directory. The only problem I had was copying any files that were in use. I havent persued this problem yet.

The following is a quick cut and paste from that program.
I removed some junk that was specific to me and I hope it will helpful.

int SourceFileHandle;
int TargetFileHandle;
int filelength1;
int filelength2;
char *Buffer;
FILE *stream;
int filescopied; //can be global or passed as a pointer.

char sourcefile [256]; //can be global or passed as a
// pointer.
char targetfile [256]; //can be global or passed as a
// pointer.

SourceFileHandle = FileOpen (sourcefile, fmOpenRead|fmShareDenyNone);

// read
filelength1 = FileSeek(SourceFileHandle, 0, 2);
FileSeek(SourceFileHandle, 0, 0);
Buffer = new char[filelength1 + 1];
FileRead(SourceFileHandle, Buffer, filelength1);
FileClose(SourceFileHandle);

// write
TargetFileHandle = FileCreate(targetfile);
filelength2 = FileWrite(TargetFileHandle, Buffer, filelength1);
FileClose(TargetFileHandle);

filescopied++;
 
CopyFile should do the work perfectly , I use it often :


BOOL CopyFile(

LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);


Parameters

lpExistingFileName

Points to a null-terminated string that specifies the name of an existing file.

lpNewFileName

Points to a null-terminated string that specifies the name of the new file.

bFailIfExists

Specifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.

Wim Vanherp
Wim.Vanherp@belgacom.net
 
Wim,

Thanks for the advice. I had already tried to use CopyFile and found that it didn't work. Having gone back to it I found that it wasn't working because I was specifying my target directory as "C:\temp\test\" not as "C:\\temp\\test"

Having made the change it works perfectly!

Thanks to all for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top