Copying a file

I'm currently using C++ Builder 6. How can I copy a file from 1 location to another specific one.
For example: the path name of a file is: C:C++Unit1.cpp
I want to copy this file to the location: C:
owin

Comments

  • : I'm currently using C++ Builder 6. How can I copy a file from 1 location to another specific one.
    : For example: the path name of a file is: C:C++Unit1.cpp
    : I want to copy this file to the location: C:
    owin
    :
    See http://codepedia.com/1/CppBuilderCopyFile (also for many other code snippets).
    Note that in C++, a backslash is an escape character. Therefore, the
    code will be something like:

    [code]
    const bool failIfExists = true;
    const String fileFrom = "Unit1.cpp"; //For example
    const String fileTo = "C:\rowin";
    CopyFile(fileFrom.c_str(),fileTo.c_str(), failIfExists);
    [/code]

    Good luck,
    bilderbikkel

  • : : I'm currently using C++ Builder 6. How can I copy a file from 1 location to another specific one.
    : : For example: the path name of a file is: C:C++Unit1.cpp
    : : I want to copy this file to the location: C:
    owin
    : :
    : See http://codepedia.com/1/CppBuilderCopyFile (also for many other code snippets).
    : Note that in C++, a backslash is an escape character. Therefore, the
    : code will be something like:
    :
    : [code]
    : const bool failIfExists = true;
    : const String fileFrom = "Unit1.cpp"; //For example
    : const String fileTo = "C:\rowin";
    : CopyFile(fileFrom.c_str(),fileTo.c_str(), failIfExists);
    : [/code]
    :
    : Good luck,
    : bilderbikkel
    :
    : Hi. Thanks for the code. But it is not working.

    const String fileFrom = "Unit1.cpp"; // But how does it know where the file is?? Can I write the filename here? (e.g: C:C++Unit1.cpp)

  • [b][red]This message was edited by bilderbikkel at 2005-12-14 3:39:16[/red][/b][hr]
    ([b]Edit: pressed 'Post Message' by accident when not being finished with the composing[/b])

    I guess you are a newbie (?), so I rewrite the code posted:
    [code]
    : : const bool failIfExists = true;
    : : const String fileFrom = "Unit1.cpp"; //For example
    : : const String fileTo = "C:\rowin";
    : : CopyFile(fileFrom.c_str(),fileTo.c_str(), failIfExists);
    [/code]
    to
    [code]
    CopyFile("Unit1.cpp","C:\rowin\MyFirstCopy.cpp", true);
    [/code]
    I guess you might got confused by [b]fileTo[/b]: this created a file called [b]rowin[/b], in the second example it created the file [b]MyFirstCopy.cpp[/b] in the folder [b]rowin[/b].

    you could also add:
    [code]
    #include //For ShowMessage
    #include //For FileExists
    if (FileExists("Unit1.cpp")==false) ShowMessage("File does not exist!");
    [/code]

    Also, if you were confused by [b]const[/b] or another keyword, check www.codepedia.com/1/CppGlossary or www.codepedia.com/1/CppBuilderGlossary

    Good luck,
  • : [b][red]This message was edited by bilderbikkel at 2005-12-14 3:39:16[/red][/b][hr]
    : ([b]Edit: pressed 'Post Message' by accident when not being finished with the composing[/b])
    :
    : I guess you are a newbie (?), so I rewrite the code posted:
    : [code]
    : : : const bool failIfExists = true;
    : : : const String fileFrom = "Unit1.cpp"; //For example
    : : : const String fileTo = "C:\rowin";
    : : : CopyFile(fileFrom.c_str(),fileTo.c_str(), failIfExists);
    : [/code]
    : to
    : [code]
    : CopyFile("Unit1.cpp","C:\rowin\MyFirstCopy.cpp", true);
    : [/code]
    : I guess you might got confused by [b]fileTo[/b]: this created a file called [b]rowin[/b], in the second example it created the file [b]MyFirstCopy.cpp[/b] in the folder [b]rowin[/b].
    :
    : you could also add:
    : [code]
    : #include //For ShowMessage
    : #include //For FileExists
    : if (FileExists("Unit1.cpp")==false) ShowMessage("File does not exist!");
    : [/code]
    :
    : Also, if you were confused by [b]const[/b] or another keyword, check www.codepedia.com/1/CppGlossary or www.codepedia.com/1/CppBuilderGlossary
    :
    : Good luck,
    :

    Thanks again for the help. Actually I'm very very new at C++ Builder.
    But this code is only for static files.
    I need to copy different files from 1 directory to another. (By using an Open Dialog box). So have you any idea how I should proceed?

  • : Thanks again for the help. Actually I'm very very new at C++ Builder.
    : But this code is only for static files.
    Static files??? You mean 'for single files'?

    : I need to copy different files from 1 directory to another. (By using an Open Dialog box). So have you any idea how I should proceed?
    In the [b]Object Inspector[/b], set the [b]Option ofAllowMultiSelect[/b] to [b]true[/b]

    Then use the [b]TStrings*[/b] accessable by [b]OpenDialog1->Files[/b]. If you don't know what [b]TStrings*[/b] means, read about arrays (www.codepedia.com/1/CppArray or www.codepedia.com/1/CppVector or www.codepedia.com/1/CppBoostArray or www.codepedia.com/1/CppPointer ).
    Then you should be able to figure the rest out yourself.

    Good luck,

    bilderbikkel

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories