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

copy file via Access VBA

86 views
Skip to first unread message

Dan

unread,
Mar 18, 2010, 6:10:01 AM3/18/10
to
How can I make a copy of C:\Source\test.txt to C:\CopyLoc\
but if test.txt already exist in C:\CopyLoc\ not to do anything.

Thanks,
Dan

Stefan Hoffmann

unread,
Mar 18, 2010, 6:19:14 AM3/18/10
to
hi Dan,

On 18.03.2010 11:10, Dan wrote:
> How can I make a copy of C:\Source\test.txt to C:\CopyLoc\
> but if test.txt already exist in C:\CopyLoc\ not to do anything.

Use this Win32 API function:

Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" ( _
ByVal AExistingFileName As String, _
ByVal ANewFileName As String, _
ByVal AFailIfExists As Boolean _
) As Boolean

Copy it in a standard module and simply use it as:

CopyFile "C:\Source\test.txt", "C:\CopyLoc\test.txt", True


mfG
--> stefan <--

Paolo

unread,
Mar 18, 2010, 6:45:01 AM3/18/10
to
Hi Dan,
this is one way

Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists("C:\CopyLoc\text.txt") Then
msgbox "Test.txt already present in c:\copyloc"
else
fs.copyfile "C:\Source\test.txt" "c:\copyloc\"
End If
set fs=nothing

this is another way to do the same

if dir("c:\copyloc\test.txt")="" then ''the file doesn't exist in c:\copyloc
filecopy "C:\Source\test.txt" "c:\copyloc\test.txt"
endif

all this is aircode

HTH Paolo

Stefan Hoffmann

unread,
Mar 18, 2010, 7:20:20 AM3/18/10
to
hi Paolo,

On 18.03.2010 11:45, Paolo wrote:
> this is one way


> this is another way to do the same

Not really, both solutions have the same problem: Timing.

Right after checking for the non-existence of the file any other process
may create this file. Then your FileCopy will still fail.


mfG
--> stefan <--

Daniel Pineault

unread,
Mar 18, 2010, 7:38:01 AM3/18/10
to
Based on the same function as mentioned by Stefan, take a look at my
CopyFile() function as it incorporates some basic error handling.

http://www.devhut.net/index.php?lang=en&pid=0000000027#CopyFile
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.

Paolo

unread,
Mar 18, 2010, 8:46:02 AM3/18/10
to
Good point Stefan, I didn't think about timing...
In some cases that could create some problems that using the API function
could avoid. Thanks for the feedback

"Stefan Hoffmann" wrote:

> .
>

Dirk Goldgar

unread,
Mar 18, 2010, 4:21:03 PM3/18/10
to
"Paolo" <Pa...@discussions.microsoft.com> wrote in message
news:73220BBD-F8E4-48C5...@microsoft.com...


On the other hand, the FileSystemObject.CopyFile method does have an
<overwrite> argument that can be specified as False to prevent overwriting
an existing file. If you try, an error will be raised, that one would
presumably want to trap in the code.

--
Dirk Goldgar, MS Access MVP
Access tips: www.datagnostics.com/tips.html

(please reply to the newsgroup)

0 new messages