PC Review


Reply
Thread Tools Rate Thread

copy file via Access VBA

 
 
Dan
Guest
Posts: n/a
 
      18th Mar 2010
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
 
Reply With Quote
 
 
 
 
Stefan Hoffmann
Guest
Posts: n/a
 
      18th Mar 2010
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 <--
 
Reply With Quote
 
Paolo
Guest
Posts: n/a
 
      18th Mar 2010
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

"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.
>
> Thanks,
> Dan

 
Reply With Quote
 
Stefan Hoffmann
Guest
Posts: n/a
 
      18th Mar 2010
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 <--
 
Reply With Quote
 
Daniel Pineault
Guest
Posts: n/a
 
      18th Mar 2010
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...00027#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.



"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.
>
> Thanks,
> Dan

 
Reply With Quote
 
Paolo
Guest
Posts: n/a
 
      18th Mar 2010
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:

> 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 <--
> .
>

 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      18th Mar 2010
"Paolo" <(E-Mail Removed)> wrote in message
news:73220BBD-F8E4-48C5-94AB-(E-Mail Removed)...
> 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:
>
>> 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.



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)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access File -Copy slh Microsoft Access Forms 3 5th Feb 2010 02:47 PM
How to copy a file in Access? =?Utf-8?B?QW1pdA==?= Microsoft Access Form Coding 4 14th Jul 2005 05:25 PM
How to have Access copy a .txt file to another folder Bucky Microsoft Access 1 24th Feb 2005 03:21 PM
File copy via ASP.NET access errors? Stu Microsoft ASP .NET 2 2nd Aug 2004 06:45 AM
Copy a File using MS Access Charles Panganiban Microsoft Access Getting Started 1 10th Sep 2003 03:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:25 AM.