|
-
Sep 9th, 2008, 03:49 PM
#1
Thread Starter
New Member
copyfile api help.
Whats wrong with the code below?
Code:
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Sub Form_Load()
Dim Path As String
Dim Path2 As String
Path = "C:\"
Path2 = "D:\"
Dim g
g = CopyFile(Path & "\tmp.txt", Path2 & "\temp.txt", True)
g = CopyFile(Path & "\tmp2.txt", Path2 & "\temp2.txt", True)
g = CopyFile(Path & "\tmp3.txt", Path2 & "\temp3.txt", True)
g = CopyFile(Path & "\tmp4.txt", Path2 & "\temp4.txt", True)
g = CopyFile(Path & "\tmp5.txt", Path2 & "\temp5.txt", True)
g = CopyFile(Path & "\tmp6.txt", Path2 & "\temp5.txt", True)
g = CopyFile(Path & "\tmp7.txt", Path2 & "\temp6.txt", True)
End Sub
It wont copy the file tmp.txt too the new location?
And the below code works but when i replace it with path & path2 above it doesnt work.
Code:
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Sub Form_Load()
'Dim g
g = CopyFile("C:\tmp.txt", "D:\tmp.txt", True)
End Sub
Whats wrong with the modified code?
Note: Yes i know i can use the standard filecopy method but the copyfile api copies much faster.
-
Sep 9th, 2008, 04:29 PM
#2
Re: copyfile api help.
Path has trailing "\" and "\tmp.txt" has leading "\" so that means there are 2 "\".
Use QualifyPath Function which adds a trailing "\" when necessary.
Code:
Option Explicit
Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Sub Form_Load()
Dim Path As String
Dim Path2 As String
Path = QualifyPath("C:\")
Path2 = QualifyPath("D:\")
Dim g
g = CopyFile(Path & "tmp.txt", Path2 & "temp.txt", True)
g = CopyFile(Path & "tmp2.txt", Path2 & "temp2.txt", True)
g = CopyFile(Path & "tmp3.txt", Path2 & "temp3.txt", True)
g = CopyFile(Path & "tmp4.txt", Path2 & "temp4.txt", True)
g = CopyFile(Path & "tmp5.txt", Path2 & "temp5.txt", True)
g = CopyFile(Path & "tmp6.txt", Path2 & "temp5.txt", True)
g = CopyFile(Path & "tmp7.txt", Path2 & "temp6.txt", True)
End Sub
Public Function QualifyPath(ByVal Path As String) As String
Dim Delimiter As String ' segmented path delimiter
If InStr(Path, "://") > 0 Then ' it's a URL path
Delimiter = "/" ' use URL path delimiter
Else ' it's a disk based path
Delimiter = "\" ' use disk based path delimiter
End If
Select Case Right$(Path, 1) ' whats last character in path?
Case "/", "\" ' it's one of the valid delimiters
QualifyPath = Path ' use the supplied path
Case Else ' needs a trailing path delimiter
QualifyPath = Path & Delimiter ' append it
End Select
End Function
Last edited by DrUnicode; Sep 9th, 2008 at 09:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|