Need to find out how to copy, paste, move files in c++ please!

Hi I'm in charge of creating a backup program, and being only a c++ novice this is proving very difficult. I just figured out how to get the current time and date to appear, and now I'm stuck with how I'm possibly supposed to get files to be moved and copied to diffrent directories, and diffrent computers on a network.

I've searched all over the net on c++ sites, and not a single one of them mentions a thing about copying files or moving them. Ive found some stuff on deleteing files, opening and closing them, and altering them, and also one discussion board that you had to pay for had about 10 messages with my same quesiton but I couldn't read any responses.

Anyways, please help me figure this out, I'd really appreciate it.
«1

Comments

  • [blue]There is no standard C or C++ way of doing that. It's entirely OS dependent.

    Windows: Win32 API function CopyFile() and MoveFile() -- see www.msdn.microsoft.com for details.

    Other operating systems: you could use system() command if your os supports copy and move functions. Or you could do all the work yourself in the program by opening source file for input, open destination file for output, then read source and write destination. Close both files. Finally, if move command, delete the source file.
    [/blue]

    : Hi I'm in charge of creating a backup program, and being only a c++ novice this is proving very difficult. I just figured out how to get the current time and date to appear, and now I'm stuck with how I'm possibly supposed to get files to be moved and copied to diffrent directories, and diffrent computers on a network.
    :
    : I've searched all over the net on c++ sites, and not a single one of them mentions a thing about copying files or moving them. Ive found some stuff on deleteing files, opening and closing them, and altering them, and also one discussion board that you had to pay for had about 10 messages with my same quesiton but I couldn't read any responses.
    :
    : Anyways, please help me figure this out, I'd really appreciate it.
    :

  • [b][red]This message was edited by Cow187 at 2004-7-15 12:30:49[/red][/b][hr]
    Ok, well lets say I'm a bit more specific, Windows 2000 and XP will be using it (hopefuly), and Im using visual c++.

    So far I've been able to figure out how to move single files using the rename command, but it cant move folders, and it always deletes the file afterwards which is bad.

    Also I see you posted in your response CopyFile() and MoveFile() , while I wait for another response I'm going to try these, but what is the header file needed for those commands? is stdio.h enough?

    I tried searching the msdn site but as always, I didnt get any useful information, could u help me out a bit? I need to move all the files from a folder into a diffrent directory, possible on to a diffrent PC (networked) and then to another directory again, and another one afterwards. Im not asking for the whole program (lol), but for some code lines maybe so I could try and figure this out? thanks for ur initial response, look forward to hearing from u and other ppl on this.



    : [blue]There is no standard C or C++ way of doing that. It's entirely OS dependent.
    :
    : Windows: Win32 API function CopyFile() and MoveFile() -- see www.msdn.microsoft.com for details.
    :
    : Other operating systems: you could use system() command if your os supports copy and move functions. Or you could do all the work yourself in the program by opening source file for input, open destination file for output, then read source and write destination. Close both files. Finally, if move command, delete the source file.
    : [/blue]
    :
    : : Hi I'm in charge of creating a backup program, and being only a c++ novice this is proving very difficult. I just figured out how to get the current time and date to appear, and now I'm stuck with how I'm possibly supposed to get files to be moved and copied to diffrent directories, and diffrent computers on a network.
    : :
    : : I've searched all over the net on c++ sites, and not a single one of them mentions a thing about copying files or moving them. Ive found some stuff on deleteing files, opening and closing them, and altering them, and also one discussion board that you had to pay for had about 10 messages with my same quesiton but I couldn't read any responses.
    : :
    : : Anyways, please help me figure this out, I'd really appreciate it.
    : :
    :
    :



  • : Also I see you posted in your response CopyFile() and
    : MoveFile() , while I wait for another response I'm going
    : to try these, but what is the header file needed for
    : those commands? is stdio.h enough?

    No, as stober already said, these are Window OS routines. 'stdio.h' is part of the C standard library, not Windows. You want to include 'windows.h'.

    On a side note: I'm fairly expert in C++, but if my boss came to me asked me to implement a backup system for some mission critical files, I would go to the store and buy backup software. It's cheaper than my time, and is going to be far more robust than any one-off tool I develop in a couple of hours.

    Anyway, if you're going to write this thing in C++, check out SHFileOperation.

  • [b][red]This message was edited by Cow187 at 2004-7-15 13:32:58[/red][/b][hr]
    lol, my "boss" asked me to right this program because he wants to use it for some specific files and folders, nothing critical about it. Also I think he just wants to keep me busy see cause I got this summer job at a small software development company, but only because my dad and the owner are good friends, cause Im a programming noob. So yea, I know very little but hopefully I'll know more by the end so anyways back to my problem.

    yea I figured out Id have to use but cant figure out the correct parameters for the CopyFile(), the c++ gives u this little description when ur trying it out but I cant understand exactly, so far I've put

    CopyFile("c:/Test/Testfile.txt","c:/Copytest/");

    but it gives me an error cause I think there needs to be something after the 2nd dir, in the litle hint box it says BOOL bFailIfExsits, but dont know what that means.

    btw I searched on msdn for SHFileOperation, and looks like that might be what I need but I cant understand what its trying to say there, could you simplify it for me?



    : No, as stober already said, these are Window OS routines. 'stdio.h' is part of the C standard library, not Windows. You want to include 'windows.h'.
    :
    : On a side note: I'm fairly expert in C++, but if my boss came to me asked me to implement a backup system for some mission critical files, I would go to the store and buy backup software. It's cheaper than my time, and is going to be far more robust than any one-off tool I develop in a couple of hours.
    :
    : Anyway, if you're going to write this thing in C++, check out SHFileOperation.
    :
    :



  • : yea I figured out Id have to use but cant figure out the
    : correct parameters for the CopyFile()

    You need to learn how to use documentation. Crucial skill for a programmer.

    : I searched on msdn for SHFileOperation, and looks like that might be what
    : I need but I cant understand what its trying to say there, could you
    : simplify it for me?

    You fill in the fields of a SHFILEOPSTRUCT structure and pass it to SHFileOperation. Pretty simple. For info on what the fields of the SHFILEOPSTRUCT structure mean, read the docs.

    For example, if you wanted to copy the folder 'c:windows emp' to 'c:foobar':
    [code=ffffff]
    SHFILEOPSTRUCT fileop;
    fileop.hwnd = [color=bb0000]NULL[/color];
    fileop.wFunc = FO_COPY;
    fileop.pFrom = [color=bb0000]"C:[/color][color=907050]\[/color][color=bb0000]windows[/color][color=907050]\[/color][color=bb0000]temp[/color][color=907050][/color][color=bb0000]"[/color]; [color=80a0b0][italic]// additional null needed[/italic][/color]
    fileop.pTo = [color=bb0000]"c:[/color][color=907050]\[/color][color=bb0000]foobar[/color][color=907050][/color][color=bb0000]"[/color]; [color=80a0b0][italic]// additional null needed[/italic][/color]
    fileop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR;
    [color=000000][b]if[/b][/color] (!SHFileOperation(&fileop))
    ; [color=80a0b0][italic]// error condition[/italic][/color]
    [/code]
  • [b][red]This message was edited by Cow187 at 2004-7-16 6:33:38[/red][/b][hr]
    wow thanks, thats the most helpful information I've recieved yet! (on 3 diffrent boards) thx man, I'll start testing it out right away. 3 questions though:

    1. what does the line fileop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR; mean?

    2. does that struct need like a function or something with it.

    3. what do u mean by documentation?

    once again thanks for ur help




    : : yea I figured out Id have to use but cant figure out the
    : : correct parameters for the CopyFile()
    :
    : You need to learn how to use documentation. Crucial skill for a programmer.
    :
    : : I searched on msdn for SHFileOperation, and looks like that might be what
    : : I need but I cant understand what its trying to say there, could you
    : : simplify it for me?
    :
    : You fill in the fields of a SHFILEOPSTRUCT structure and pass it to SHFileOperation. Pretty simple. For info on what the fields of the SHFILEOPSTRUCT structure mean, read the docs.
    :
    : For example, if you wanted to copy the folder 'c:windows emp' to 'c:foobar':
    : [code=ffffff]
    : SHFILEOPSTRUCT fileop;
    : fileop.hwnd = [color=bb0000]NULL[/color];
    : fileop.wFunc = FO_COPY;
    : fileop.pFrom = [color=bb0000]"C:[/color][color=907050]\[/color][color=bb0000]windows[/color][color=907050]\[/color][color=bb0000]temp[/color][color=907050][/color][color=bb0000]"[/color]; [color=80a0b0][italic]// additional null needed[/italic][/color]
    : fileop.pTo = [color=bb0000]"c:[/color][color=907050]\[/color][color=bb0000]foobar[/color][color=907050][/color][color=bb0000]"[/color]; [color=80a0b0][italic]// additional null needed[/italic][/color]
    : fileop.fFlags = FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR;
    : [color=000000][b]if[/b][/color] (!SHFileOperation(&fileop))
    : ; [color=80a0b0][italic]// error condition[/italic][/color]
    : [/code]
    :



  • : 1. what does the line fileop.fFlags =
    : FOF_ALLOWUNDO|FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR; mean?

    These set options for the file transfer. In this case, it tells the OS that you want to be able to undo this file operation, that it shouldn't prompt the user for confirmation to overwrite files, and that it shouldn't prompt the user for confirmation to create new directories.

    These flags and many others are described in detail, in the docs.

    : 2. does that struct need like a function or something with it.

    SHFileOperation() -- I posted an example.

    : 3. what do u mean by documentation?

    http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/functions/shfileoperation.asp

  • :
    : 3. what do u mean by documentation?
    :

    I'm astonished that anyone can get that far in programming and not know the answer to that question. All it means is that you need to learn how to look up the answers to your questions and do your own research. If after all that you still can't figure out a problem then feel free to post questions on this or other boards.

    You can find complete Win32 API function descriptions (documentation!) at www.msdn.microsoft.com and in various books at your local book store. Your compiler may also have it in it's on-line Help.

  • Well, thanks alot for your help guys ( and your criticism ), especialy eric , good man eric, good man. I can now continue with my program, I will post it up here when I'm done so you guys can use it/see it/criticize it lol.

    I'll probably be back soon, but hey, learning is a hard process
  • Another question about this struct, I've been trying a few things about the error condition, but none seem to work.

    one of the things I tried was:

    if(!SHFileOperation(&fileop)) printf( "Copying Failed... Try again" )

    ^ this just prints the copying failed thing everytime even when it works.

    if(!SHFileOperation(&fileop) printf( "Copying Failed... Try again" ))

    ^ this says im missing some ( chars

    I tried just the regular if stuff but those don't do anything, it just always prints the statement, I think there must be some diffrent way to do it with this struct?
  • If you would use the Win32 API function CopyFile() as I suggested originally it will return error codes to your application. And, you can save all that trouble of filling out that structure because all CopyFile requires is three parametes as shown in www.msdn.microsoft.com

  • : Another question about this struct, I've been trying a few things about the error condition, but none seem to work.
    :
    : one of the things I tried was:
    :
    : if(!SHFileOperation(&fileop)) printf( "Copying Failed... Try again" )
    :
    : ^ this just prints the copying failed thing everytime even when it works.
    :
    : if(!SHFileOperation(&fileop) printf( "Copying Failed... Try again" ))
    :
    : ^ this says im missing some ( chars
    :
    : I tried just the regular if stuff but those don't do anything, it just always prints the statement, I think there must be some diffrent way to do it with this struct?
    :


    ShFileOperation() returns 0 on success, read the docs.


    [code]
    if( (SHFileOperation(&fileop)) != 0)
    {
    printf( "Copying Failed... Try again
    " )
    }
    [/code]
    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • : If you would use the Win32 API function CopyFile() as I suggested originally it will return error codes to your application. And, you can save all that trouble of filling out that structure because all CopyFile requires is three parametes as shown in www.msdn.microsoft.com
    :
    :

    stober, I'm pretty sure that is only for copying single files? If im wrong then please fill me in , cause the simpler it is the better for me, but I need to copy whole folders, as well as groups of specific files ( creation time orientated).

    I got the other thing to work btw, thx for ur replies, I just had to take out the ! cause it returns 0 for success =)
  • : : Another question about this struct, I've been trying a few things about the error condition, but none seem to work.
    : :
    : : one of the things I tried was:
    : :
    : : if(!SHFileOperation(&fileop)) printf( "Copying Failed... Try again" )
    : :
    : : ^ this just prints the copying failed thing everytime even when it works.
    : :
    : : if(!SHFileOperation(&fileop) printf( "Copying Failed... Try again" ))
    : :
    : : ^ this says im missing some ( chars
    : :
    : : I tried just the regular if stuff but those don't do anything, it just always prints the statement, I think there must be some diffrent way to do it with this struct?
    : :
    :
    :
    : ShFileOperation() returns 0 on success, read the docs.
    :
    :
    : [code]
    : if( (SHFileOperation(&fileop)) != 0)
    : {
    : printf( "Copying Failed... Try again
    " )
    : }
    : [/code]
    : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    :
    :

    CopyFile() does only work with one file at a time, but there is no reason why you can't use iteration to enumerate all files in a folder and its sub-folders and copy them one at a time using this function inside a loop to achieve your task. You could also use a system to call to xcopy, or something similar, but I would not recommend rhat.
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