Rick
Rick

Reputation: 2308

VB6: Copy file within network shares

I am tring to copy a file in the current folder of the executable to a networkshare.

I used the following code, but if the file exists, it won't replace it. It be nice for it to ask if the user want to replace or not.

 Set oFSO = CreateObject("Scripting.FileSystemObject")
 oFSO.GetFile("c:\file.txt").Copy "\\server\sharename\", True

Upvotes: 0

Views: 4988

Answers (2)

James Johnston
James Johnston

Reputation: 9492

Your code sample works for me for copying files. Your network share may be denying access.

FYI, there is an alternative - use FileCopy like this:

FileCopy <source file>, <destination file>

It will overwrite the destination without prompting. If you want to prompt, I can think of two ways:

  1. Use FileExists like Gavin suggests, and ask the user if they want to overwrite.
  2. Use functionality built into Windows for this. It sounds like this might be your best choice. The SHFileOperation API will display a progress dialog with a Cancel button while the file copies. It will also prompt for overwrite. The dialogs used are the same as used by Windows Explorer. Use FO_COPY for wFunc; I don't think you need to set any flags in fFlags but there might be some of interest. Set pFrom / pTo to your source/destination files/directories, and hwnd to the owner window (or 0 if none). Everything else in the structure can be NULL / 0 / empty.

Upvotes: 2

Gavin
Gavin

Reputation: 9994

You'll have to build your own logic for checking that the file exists and raise the message box manually based on that. You can use the FileExists function to check if the file exists.

Upvotes: 2

Related Questions