CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2005
    Posts
    106

    Question How to copy file from one place to another....

    Hi !!

    In my application, I am downloading one file to my application path.Some times it is not downloading total file.In that my application is crashing.

    For that I want to download that file to another place.If it is dowloaded correctly,then it is to copy to my application path.
    For downloading,I wrote the code as follows....

    int createFileMask = CFile::modeCreate | CFile::modeWrite | CFile::typeBinary;
    CFile destFile(strDestFile,createFileMask );
    while(byteswrite = pTargFile->Read(filebuf,512)){
    destFile.Write(filebuf, byteswrite);
    }
    // Download ended, close file
    destFile.Close();
    Can any one please give me the solution for this problem..
    And can I use the CopyFileEx method to solve this??If, How can I use this function..

    Thanks & Regards,
    klvin.

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: How to copy file from one place to another....

    Where does it crash, which line ?

    Are you asking how to copy file to new destination after download is complete ?
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Aug 2005
    Posts
    106

    Question Re: How to copy file from one place to another....

    Hi krishnaa,

    My application is crashing when it is not downloading file fully because of server problems..

    So that I want to download the file to another path.After file is completely downloded then i want to copy the downloaded file to my application path.
    Can you give me the solution for this....

    Thanks & regards,
    klvin

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: How to copy file from one place to another....

    I didn’t get it, from where you are downloading the file? if its from the internet simply use ::URLDownloadToFile ().

    if its from a mapped network drive on your server you can use ::SHFileOperation ()

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to copy file from one place to another....

    FYI: the best way to copy files in Windows - using ::CopyFile API

  6. #6
    Join Date
    Aug 2005
    Posts
    106

    Question Re: How to copy file from one place to another....

    Hi !!
    Thanks for the reply.
    I tried with ::CopyFile(...).It is working.It is copying the downloaded file to my application path.
    But, How Can I check the condition whether the file is fully downloaded or not before copying the file??
    If the file is fully download, then only it should copy that file to application path.

    Can you please give me solution, How to check the condition?????
    Code:
    CStdioFile *pTargFile = NULL;
    pTargFile = 
    netSession.OpenURL(......)
    CString strCurrentDir,str;
    mapURLs.Lookup(_T("app_cwd"), strCurrentDir);
    str = strCurrentDir + "Downloads\\";
    ::CreateDirectory(str,NULL);
    strDownldFile.Format(_T("%s%s"),
    		str,
    		fl);
    strDestFile.Format (_T("%s%s"),
    		strCurrentDir,
    		fl);
    int createFileMask = CFile::modeCreate | CFile::modeWrite | CFile::typeBinary;
    CFile destFile(strDownldFile,createFileMask );
    while(byteswrite = pTargFile->Read(filebuf,512)){
    destFile.Write(filebuf, byteswrite);
    }
    // Download ended, close file
    destFile.Close();
    After destFile.Close()..I have to check condition and copythe file....


    Thanks & Regards,
    klvin.
    Last edited by klvin; August 3rd, 2006 at 01:53 AM.

  7. #7
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: How to copy file from one place to another....

    Check out For DownLoadComplete() and DocumentComplete Event. Have a look in MSDN for More details. for More detail have a look on this Thread.or you can use InternetQueryDataAvailable()
    Thanx
    Last edited by humptydumpty; August 3rd, 2006 at 02:16 AM.

  8. #8
    Join Date
    Aug 2005
    Posts
    106

    Question Re: How to copy file from one place to another....

    Hi humptydumpty,

    Thanks for the reply.
    But I don't know how to use DownloadComplete() and all..
    Can you give me idea,how can I use them relatedly to my project.

    Thanks&Regards,
    klvin.

  9. #9
    Join Date
    Oct 2000
    Location
    India
    Posts
    4,620

    Thumbs up Re: How to copy file from one place to another....

    Hi,

    Since you have finished reading the bytes and have written them to the destfile, it means that the download is done. You dont need to check any
    condition after calling destFile.Close(). You can copy it right away.

    Or am i missing something in your question ?
    All luck and have a great day.

    Regards,
    V.Girish

    Visit www.geocities.com/contactgirish for Source code, Tutorials, FAQs and Downloads.

  10. #10
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: How to copy file from one place to another....


  11. #11
    Join Date
    May 2005
    Posts
    4,954

    Re: How to copy file from one place to another....

    Quote Originally Posted by klvin
    Hi humptydumpty,

    Thanks for the reply.
    But I don't know how to use DownloadComplete() and all..
    Can you give me idea,how can I use them relatedly to my project.

    Thanks&Regards,
    klvin.
    Very simple:

    Code:
      ::URLDownloadToFile(0,"http://www.yahoo.com/index.html","c:\\index.html",0,0);
    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  12. #12
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: How to copy file from one place to another....

    Quote Originally Posted by humptydumpty
    Check out For DownLoadComplete() and DocumentComplete Event.
    Not applicable to projects which do not use WebBrowser control or Html/Dhtml controls or Internet explorer automation.

    @klvin

    URLDownloadToFile is best, and then CopyFile can copy the downloaded file, why write a lot of code for functionality which is already there.
    Regards,
    Ramkrishna Pawar

  13. #13
    Join Date
    Aug 2005
    Posts
    106

    Question Re: How to copy file from one place to another....

    Hi !!
    Thanks for the reply Krishnaa..
    But, my doubt is ..
    How can I know Whether the file was fully downloaded or not????

    Thanks&Regards,
    klvin.

  14. #14
    Join Date
    May 2005
    Posts
    4,954

    Re: How to copy file from one place to another....

    Quote Originally Posted by klvin
    Hi !!
    Thanks for the reply Krishnaa..
    But, my doubt is ..
    How can I know Whether the file was fully downloaded or not????

    Thanks&Regards,
    klvin.
    If you using ::URLDownloadToFile() the function wont return until the download finished.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured