-
I cannot seem to find out how to copy a file from one directory to another.
I have used the NAME command but that actually removes the file from the original directory and I need to have that file available for other users to copy too.
I need to be able to copy a setup.exe file from one computer to another.
Can anyone help me on this one?
Thanks for reading this.
-
<?>
Code:
'copy or save a file to a different directory
Private Sub Command1_Click()
Dim SourcePath As String, DestPath As String
SourcePath = "c:\my documents\try.txt"
DestPath = "c:\download\whodoneit.txt"
FileCopy SourcePath, DestPath
End Sub
-
Or maybe:
Code:
Public Sub CopyTheFiles()
Dim FSO As Object
On Error GoTo NOFSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "D:\Folder\*.ack","C:\Folder2\" , True
Set FSO = Nothing
Exit Sub
NOFSO:
MsgBox"FSO copy failed"
End Sub
Add a reference to Microsoft Scripting Runtime.
-
Speaking about alternatives:
Code:
'>>>>>>>>In module<<<<<<
Option Explicit
Public Declare Function SHFileOperation Lib _
"shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As Any) As Long
Public Declare Sub SHFreeNameMappings Lib _
"shell32.dll" (ByVal hNameMappings As Long)
Public Declare Sub CopyMemory Lib "KERNEL32" _
Alias "RtlMoveMemory" (hpvDest As Any, hpvSource _
As Any, ByVal cbCopy As Long)
Public Type SHFILEOPSTRUCT
hwnd As Long
wFunc As FO_Functions
pFrom As String
pTo As String
fFlags As FOF_Flags
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String 'only used if FOF_SIMPLEPROGRESS
End Type
Public Enum FO_Functions
FO_MOVE = &H1
FO_COPY = &H2
FO_DELETE = &H3
FO_RENAME = &H4
End Enum
Public Enum FOF_Flags
FOF_MULTIDESTFILES = &H1
FOF_CONFIRMMOUSE = &H2
FOF_SILENT = &H4
FOF_RENAMEONCOLLISION = &H8
FOF_NOCONFIRMATION = &H10
FOF_WANTMAPPINGHANDLE = &H20
FOF_ALLOWUNDO = &H40
FOF_FILESONLY = &H80
FOF_SIMPLEPROGRESS = &H100
FOF_NOCONFIRMMKDIR = &H200
FOF_NOERRORUI = &H400
FOF_NOCOPYSECURITYATTRIBS = &H800
FOF_NORECURSION = &H1000
FOF_NO_CONNECTED_ELEMENTS = &H2000
FOF_WANTNUKEWARNING = &H4000
End Enum
Public Type SHNAMEMAPPING
pszOldPath As String
pszNewPath As String
cchOldPath As Long
cchNewPath As Long
End Type
Public Function SHFileOP(ByRef lpFileOp As SHFILEOPSTRUCT) As Long
'This uses a method suggested at MSKB to
'ensure that all parameters are passed correctly
'Call this wrapper rather than the API function directly
Dim result As Long
Dim lenFileop As Long
Dim foBuf() As Byte
lenFileop = LenB(lpFileOp)
ReDim foBuf(1 To lenFileop) 'the size of the structure.
'Now we need to copy the structure into a byte array
Call CopyMemory(foBuf(1), lpFileOp, lenFileop)
'Next we move the last 12 bytes by 2 to byte align the data
Call CopyMemory(foBuf(19), foBuf(21), 12)
result = SHFileOperation(foBuf(1))
SHFileOP = result
End Function
'>>>>>>>>in form<<<<<<<<<
Private Sub CopyFiles()
Dim fileop As SHFILEOPSTRUCT
With fileop
.hwnd = 0
.wFunc = FO_COPY
.pFrom = App.Path & "\OLD.exe" & vbNullChar & vbNullChar
.pTo = App.Path & "\NEW.exe" & vbNullChar & vbNullChar
.lpszProgressTitle = "Please wait, copying..."
.fFlags = FOF_SIMPLEPROGRESS Or FOF_ALLOWUNDO
End With
'lret = SHFileOP(fileop)
'If Module1.result <> 0 Then 'Operation failed
'MsgBox Err.LastDllError 'Show the error returned from the API.
'Else
'If fileop.fAnyOperationsAborted <> 0 Then
'MsgBox "Operation Failed"
'End If
'End If
End Sub
[Edited by Jop on 09-28-2000 at 10:47 AM]
-
HeSaidJoe
Short and Sweet!!! And it works too! Thanks a bunch.
I did all kinds of searches but could not find this in my help area. Thanks again for your assistance and to those who also posted other help ideas.