3
Contributors
9
Replies
10
Views
9 Years
Discussion Span
Last Post by CodeBoy101
0

just write it multiple times using a different filename each time. Or write the first file then call os-specific command to make additional copies.

0

just write it multiple times using a different filename each time. Or write the first file then call os-specific command to make additional copies.

I'm trying to make multiple copes of the same file, like: filename[1].ext, filename[2].ext, etc... and windows is my platform. The only commmand that I know of is the "CopyFile" command of windows.h and I don't think I can make multiple file copes with that, can you help me?

0

why do you think CopyFile won't work for you? Its one of the easiest win32 api functions you can use.

for(i = 0; i < NumberFiles; i++)
{
    CopyFile(OriginalFilename, filename[i], FALSE);
}
0

why do you think CopyFile won't work for you? Its one of the easiest win32 api functions you can use.

for(i = 0; i < NumberFiles; i++)
{
    CopyFile(OriginalFilename, filename[i], FALSE);
}

I figured thought that the "CopyFile" function could only take certain arguments. And I tried
your suggestion and I was proven right. The error message said:

-- invalid conversion from `const char' to `const CHAR*'--

This is my code thus far,

//Multiple File copy module

#include<iostream>
#include <windows.h>

using namespace std;

int main (void)
{
    int n;//where "n" is the number of fies to be copied.
    

for (n=0; n<2; n++){
CopyFile("C:\\Dev-Cpp\\Programs\\MultipleFileCopyModule.exe","C:\\TestBox\\MultipleFileCopyModule.exe"[n],false);
//The file copies itself.
cout << "\t\t\t\tFile copied\n\n";
system ("pause");
}

return 0;
}
0

> -- invalid conversion from `const char' to `const CHAR*'--
The [n] subscript is doing that.

You need to generate the filename string, with n appended, before you pass it to CopyFile().
In C, I'd use say sprintf(), but in C++ I think a string stream would be more appropriate.

0

Lets say you want the file names to be
MultipleFileCopyModule1.exe
MultipleFileCopyModule2.exe
MultipleFileCopyModule3.exe

To accomplish that you have a couple options:
1:) call CopyFile three times

std::string filename = "C:\\Dev-Cpp\\Programs\\MultipleFileCopyModule";
CopyFile((std::string)(filename+".exe").c_str(),  (std::string)(filename+"1.exe").c_str(),false);

CopyFile((std::string)(filename+".exe").c_str(),  (std::string)(filename+"2.exe").c_str(),false);

CopyFile((std::string)(filename+".exe").c_str(),  (std::string)(filename+"3.exe").c_str(),false);

2) Use a loop if the number of names is unknown at compile time, something like this:

#include <sstream>
#include <string>
...
...
std::string filename = "C:\\Dev-Cpp\\Programs\\MultipleFileCopyModule";
for(int i = 0; i < n; i++)
{
     std::stringstream newname;
     // create a new filename
     newname << filename << i << ".exe";
    // copy the file
     CopyFile((std::string)(filename+".exe").c_str(), newname.str(), false);
}
0

Aww man!, I totally thought that would work, but now the compiler says:

--- cannot convert `std::string' to `const CHAR*' for argument `1' to `BOOL CopyFileA(const CHAR*, const CHAR*, int)' ---

Have any suggestions?

0

post code. you probably need to pass the c_str() method from the std::string object, as I illustrated in my previous post.

You were right I needed to used the ".c_str()". Thanks!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.