Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy File that is currently open

Status
Not open for further replies.

123help1234

Programmer
Jun 1, 2005
10
US
I use the SHFILEOPSTRUCT to copy an access database file from one folder to another. The only problem is if I step through this code while the database is open, I get error "Cannot copy file: Cannot read from the source file or disk." This only happens if I use F8 to step through the code in the SHFileOP function. If I use F5 and just run it, it works fine. While this is weird, it normally wouldn't be a problem, except that my customer gets this same error on their machine when running the exe. Here is the code that is being used, the error happens on the result = SHFileOperation(foBuf(1)) line. Can you think of what would be causing this problem?

Private Function SHFileOP(ByRef lpFileOp As SHFILEOPSTRUCT) As Long
'This uses a method suggested at MSKB to
'ensure that all parameters are passed correctly
'Call this wrapper rather than the API function directly

Dim lenFileop As Long
Dim foBuf() As Byte

lenFileop = LenB(lpFileOp)
ReDim foBuf(1 To lenFileop) 'the size of the structure.

'Now we need to copy the structure into a byte array
Call CopyMemory(foBuf(1), lpFileOp, lenFileop)

'Next we move the last 12 bytes by 2 to byte align the data
Call CopyMemory(foBuf(19), foBuf(21), 12)
result = SHFileOperation(foBuf(1))

SHFileOP = result
End Function
 
Have you tried inserting a "doevents" after each "call.." statement? Perhaps one line of code is attempting to execute before the previous is finished.

ciao for niao!

AMACycle

American Motorcyclist Association
 
I changed the code as follows and still have the same problem. Is there any other code I should be using to copy a file that may be opened by someone? The client gets the error using the code below, but has no problem doing a copy and paste using explorer.

Private Function SHFileOP(ByRef lpFileOp As SHFILEOPSTRUCT) As Long
'This uses a method suggested at MSKB to
'ensure that all parameters are passed correctly
'Call this wrapper rather than the API function directly

Dim lenFileop As Long
Dim foBuf() As Byte

lenFileop = LenB(lpFileOp)
ReDim foBuf(1 To lenFileop) 'the size of the structure.
DoEvents
'Now we need to copy the structure into a byte array
Call CopyMemory(foBuf(1), lpFileOp, lenFileop)
DoEvents
'Next we move the last 12 bytes by 2 to byte align the data
Call CopyMemory(foBuf(19), foBuf(21), 12)

result = SHFileOperation(foBuf(1))

SHFileOP = result
End Function
 
Here's the code that calls the SHFileOP:

Dim lRet As Long
Dim fileop As SHFILEOPSTRUCT

With fileop
.hwnd = 0
.wFunc = FO_COPY
.pFrom = fromLoc
.pTo = toLoc
.lpszProgressTitle = strMessage
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION
End With

lRet = SHFileOP(fileop)
 
No, we need more than that ... that's just setting up the structure. We need to know what else your program is doing before the call is made. For example, is it opening a file, dumping data to it and then closing it?
 
It's a database utitlity of sorts. First it tries to compact an access 2000 database by making a shell call to JetComp.exe. If it fails (presumably because it's locked by someone on the network) it tries to copy the file from the network to a local temp folder (this is where the error is occuring) so you can attempt to run compaction locally. Then you can copy the local file back over the network file.

Performing these steps manually (copying and pasting via control c and control v in explorer, running JetComp, and copying back over using explorer) usually works perfectly both on mine and a client site. When I test it out on my local machine (not over a network and a smaller file) it works fine. However when I step through locally using F8, or try to run this on a client site, I get the error.

Does this help at all?
 
Do you think it's a good idea to be copying and compacting a database that is being locked by someone over the network and then replacing the locked one with the compacted one? Don't you think you might loose data changes doing so?

Greetings,
Rick
 
This is only for when the database has become corrupted and no one can access it anyways. It's sort of a last resort to repair the database.
 
Would it be possible to emulate this behaviour using the clipboard object? This seems like a pretty week solution, but it works when you use the clipboard manually as opposed to using file copying code. If I could copy the file to the clipboard and then paste it to the destination folder, do you think this might help? I still can't figure out why the filecopy function isn't working though. Any other thoughts?

Thanks.
 
can you not open the file binary for reading and then read the bytes and open another file to dump those bytes?

Greetings,
Rick
 
I guess that's an option I could try. I'm still curious why I'm having this problem in the first place though.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top