Sunday, March 30, 2008

Copy function with API and VB

Here we go, another article about API function and VB. For VB developer, maybe you all familiar with Filecopy function. Yes, that function is to use copy and paste file we want into the directory we want.

Now, I want to make my own Filecopy with API function. In API, we can use CopyFile function, where the statement is:

CopyFile(lpExistingFileName, lpNewFileName, bFailIfExists)

lpExistingFileName = Source of our file
lpNewFileName = Destination of our file
bFailIfExists = checked first if we gave value 1, and replace it if we gave value 0


Now here the code:

Declare Function CopyFile Lib “kernel32.dll” Alias “CopyFileA” (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long)

Application I want to make is copy file from source directory into destination directory, but checking first the existing of the file and return the message whatever success or not.

Private Sub CopyData()

Dim ErrVal as Byte

ErrVal = CopyFile (“c:\autoexec.bat”, “d:\autoexec.bat”, 1)

If ErrVal = 0 then

Msgbox “Operation Not Success. File already exist”

Else

Msgbox “Operation Success.”

End If

End Sub

Now we can modify our copy function with the rules we want. Enjoy it…




2 comments:

Home Theater said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Home Theater, I hope you enjoy. The address is http://home-theater-brasil.blogspot.com. A hug.

Layong Lim said...

Thx. already add yours too. :)

Google

Microsoft News for ADO .NET:

Our Email Address:

blog.programming@gmail.com