Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I skip a file in use, while copying a directory, using SHFileOperation

127 views
Skip to first unread message

Magnus van Aardt

unread,
Jun 23, 2003, 5:45:48 AM6/23/03
to

Seems solving one problem leads to another. I'm new to API so please bear
with me.

I use SHFileOperation Function to copy all the files and sub directories
from a shared Folder across a network, to a local directory.

It works fine, except for the following problem:

- Cannot copy <FileName>. Process cannot Access the file because
another process has locked a portion of the file.

As soon as the above error occurs the entire copy procedure is canceled. How
can I just skip over the locked file and continue to copy the other files in
the directory. Does it mean that I need to check every file, to see if it is
in use, and then call the SHFileOperation to copy the file if not in use?

Can anyone help me to solve this problem?

Muhammad Momin Rashid

unread,
Jun 23, 2003, 7:13:59 AM6/23/03
to
Look into exception handling. In C++ Builder (sorry I don't have access to
Delphi right now), you can simply solve the problem by enclosing the File
Copy Function in "try ... catch" block. Let me try to explain the concept

***Your Function Starts***
// some code here

try
{
SHFileOperation
}
catch (Exception &E)
{
// error handling code here (if required), otherwise leave empty
}

// remaining code

***Your Function Ends***

Hope this helps
Regards,
Muhammad Momin Rashid.

"Magnus van Aardt" <mva...@srcc.co.za> wrote in message
news:3ef6...@newsgroups.borland.com...

Muhammad Momin Rashid

unread,
Jun 23, 2003, 7:29:13 AM6/23/03
to
Found some samples using google.

Procedure MyFileCopyOperation;
Begin

{some code here}

try
SHFileOperation( ... );
except
On Exception do
{ Exception Handling code here (if required) };
end;
End;

{ some code here}

End;

Search String I used:
http://www.google.com/search?num=100&hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&q=
exception+handling+delphi

Regards,
Muhammad Momin Rashid

"Muhammad Momin Rashid" <mo...@hotmail.com> wrote in message

Peter Below (TeamB)

unread,
Jun 23, 2003, 1:44:09 PM6/23/03
to
In article <3ef6...@newsgroups.borland.com>, Magnus van Aardt wrote:
> I use SHFileOperation Function to copy all the files and sub directories
> from a shared Folder across a network, to a local directory.
>
> It works fine, except for the following problem:
>
> - Cannot copy <FileName>. Process cannot Access the file because
> another process has locked a portion of the file.
>
> As soon as the above error occurs the entire copy procedure is canceled. How
> can I just skip over the locked file and continue to copy the other files in
> the directory. Does it mean that I need to check every file, to see if it is
> in use, and then call the SHFileOperation to copy the file if not in use?

TRy to add the FOF_NOERRORUI flag to the flags set. If that does not solve the problem
you may indeed have to copy file by file. Or use the good old XCOPY program via
ShellExecute, it has a command line option to continue after errors.


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Peter Below (TeamB)

unread,
Jun 23, 2003, 1:44:08 PM6/23/03
to
In article <3ef6...@newsgroups.borland.com>, Muhammad Momin Rashid wrote:
> try
> SHFileOperation( ... );
> except
> On Exception do
> { Exception Handling code here (if required) };
> end;
> End;

This is somewhat pointless since API functions, as a rule, do not raise
exceptions, they return result codes.

David B. Ferguson

unread,
Jun 24, 2003, 10:44:00 AM6/24/03
to
> - Cannot copy <FileName>. Process cannot Access the file because
> another process has locked a portion of the file.
>
> As soon as the above error occurs the entire copy procedure is canceled.
How
> can I just skip over the locked file and continue to copy the other files
in
> the directory. Does it mean that I need to check every file, to see if it
is
> in use, and then call the SHFileOperation to copy the file if not in use?
>

Look into implementing a SHFileOperation copy callback handler.

David-


0 new messages