• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

Copying Files

How can I join the code below to make the code shorten?
Private Sub Data1()
Screen.MousePointer = vbHourglass
        lFileOp = FO_COPY
        lFlags = lFlags Or FOF_SILENT
        lFlags = lFlags Or FOF_NOCONFIRMATION
        
        With SHFileOp
        .wFunc = lFileOp
        .pFrom = "C:\MSSQL7\Data\Mydb_Data.MDF"
        .pTo = App.Path & "\Data"
        .fFlags = lFlags
        End With
lFlags = lFlags Or FOF_SILENT
lFlags = lFlags Or FOF_NOCONFIRMATION
lFlags = lFlags Or FOF_RENAMEONCOLLISION
lFlags = lFlags Or FOF_NOCONFIRMMKDIR
lFlags = lFlags Or FOF_FILESONLY
 
lresult = SHFileOperation(SHFileOp)
 
Screen.MousePointer = vbDefault
If lresult <> 0 Or SHFileOp.fAborted Then Exit Sub
'Data.Value = 0
End Sub
Private Sub Data2()
Screen.MousePointer = vbHourglass
        lFileOp = FO_COPY
        lFlags = lFlags Or FOF_SILENT
        lFlags = lFlags Or FOF_NOCONFIRMATION
        
        With SHFileOp
        .wFunc = lFileOp
        .pFrom = "C:\MSSQL7\Data\Mydb_Log.LDF"
        .pTo = App.Path & "\Data"
        .fFlags = lFlags
        End With
       
lFlags = lFlags Or FOF_SILENT
lFlags = lFlags Or FOF_NOCONFIRMATION
lFlags = lFlags Or FOF_RENAMEONCOLLISION
lFlags = lFlags Or FOF_NOCONFIRMMKDIR
lFlags = lFlags Or FOF_FILESONLY
 
lresult = SHFileOperation(SHFileOp)
 
Screen.MousePointer = vbDefault
If lresult <> 0 Or SHFileOp.fAborted Then Exit Sub
End Sub

Open in new window

0
Whing Dela Cruz
Asked:
Whing Dela Cruz
3 Solutions
 
game-masterCommented:

good morning!

u can use the filesystemobject or FSO to do the copying...
on the prject references, just select the "Microsoft Scripting Runtime"

sumthing like this fso.copyfile <sourcefile>,<destination>,<overwrite true or false>



i hope i can give u idea..


game-master
0
 
nirojexpertCommented:
visual basic already has the built in function for filecopy
it's syntax is
filecopy src, destination
also common functions like mkdir, name (for rename), are there.
0
 
Whing Dela CruzAuthor Commented:
I'm sorry, I was not able to explain my question correctly.
My question is like: How can i copy two files in one command? My code below will copy two files but it seems too long procedure. Can I make that shorten? Can I join Data1 and Data2?
Thanks!
 

Dim F As Folder
Dim fpath As String
Dim fsys As FileSystemObject
 
Const FO_COPY = &H2
Const FO_DELETE = &H3
Const FO_MOVE = &H1
Const FO_RENAME = &H4
Const FOF_ALLOWUNDO = &H40
Const FOF_SILENT = &H4
Const FOF_NOCONFIRMATION = &H10
Const FOF_RENAMEONCOLLISION = &H8
Const FOF_NOCONFIRMMKDIR = &H200
Const FOF_FILESONLY = &H80
Private Type SHFILEOPSTRUCT
    hwnd      As Long
    wFunc     As Long
    pFrom     As String
    pTo       As String
    fFlags    As Integer
    fAborted  As Boolean
    hNameMaps As Long
    sProgress As String
End Type
 
Private Declare Function SHFileOperation Lib "shell32.dll" _
    Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Dim lFileOp  As Long
Dim lresult  As Long
Dim lFlags   As Long
Dim SHFileOp As SHFILEOPSTRUCT
 
Private Sub Data1()
Screen.MousePointer = vbHourglass
        lFileOp = FO_COPY
        lFlags = lFlags Or FOF_SILENT
        lFlags = lFlags Or FOF_NOCONFIRMATION
        
        With SHFileOp
        .wFunc = lFileOp
        .pFrom = "C:\MSSQL7\Data\Mydb_Data.MDF"
        .pTo = App.Path & "\Data"
        .fFlags = lFlags
        End With
lFlags = lFlags Or FOF_SILENT
lFlags = lFlags Or FOF_NOCONFIRMATION
lFlags = lFlags Or FOF_RENAMEONCOLLISION
lFlags = lFlags Or FOF_NOCONFIRMMKDIR
lFlags = lFlags Or FOF_FILESONLY
 
lresult = SHFileOperation(SHFileOp)
 
Screen.MousePointer = vbDefault
If lresult <> 0 Or SHFileOp.fAborted Then Exit Sub
End Sub
Private Sub Data2()
Screen.MousePointer = vbHourglass
        lFileOp = FO_COPY
        lFlags = lFlags Or FOF_SILENT
        lFlags = lFlags Or FOF_NOCONFIRMATION
        
        With SHFileOp
        .wFunc = lFileOp
        .pFrom = "C:\MSSQL7\Data\Mydb_Log.LDF"
        .pTo = App.Path & "\Data"
        .fFlags = lFlags
        End With
       
lFlags = lFlags Or FOF_SILENT
lFlags = lFlags Or FOF_NOCONFIRMATION
lFlags = lFlags Or FOF_RENAMEONCOLLISION
lFlags = lFlags Or FOF_NOCONFIRMMKDIR
lFlags = lFlags Or FOF_FILESONLY
 
lresult = SHFileOperation(SHFileOp)
 
Screen.MousePointer = vbDefault
If lresult <> 0 Or SHFileOp.fAborted Then Exit Sub
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nffvrxqgrcfqvvcCommented:
Yes. Think of it as a very long string where each file you want to copy is seperated by a null character and then at the end of the list just add two null characters.
You must also have FOF_MULTIDESTFILES for flags members.
http://msdn.microsoft.com/en-us/library/bb759795(VS.85).aspx
Here is a small example that I wrote that will simply use a seperator and just replace that with a null character. In the example the separator is simply ";" between each file you wish to copy.
Option Explicit
 
Private Const FO_COPY               As Long = &H2
Private Const FOF_MULTIDESTFILES    As Long = &H1
Private Const FOF_SIMPLEPROGRESS    As Long = &H100
 
Private Type SHFILEOPSTRUCTW
 hwnd                   As Long
 wFunc                  As Long
 pFrom                  As Long     ' LPCTSTR
 pTo                    As Long     ' LPCTSTR
 fFlags                 As Long
 fAnyAborted            As Boolean
 hNameMappings          As Long
 lpszProgressTitle      As Long     ' LPCTSTR
End Type
 
Private Declare Function SHFileOperationW Lib "shell32" (lpFileOp As SHFILEOPSTRUCTW) As Long
 
Public Sub DoCopy(ByVal szFrom As String, ByVal szTo As String, Optional Seperator As String = vbNullString)
  
  Dim sh      As SHFILEOPSTRUCTW
  
  szFrom = Replace$(szFrom, Seperator, vbNullChar)
  szTo = Replace$(szTo, Seperator, vbNullChar)
  
  With sh
    .wFunc = FO_COPY
    .fFlags = FOF_SIMPLEPROGRESS Or FOF_MULTIDESTFILES
    .pFrom = StrPtr(szFrom & vbNullChar & vbNullChar)
    .pTo = StrPtr(szTo & vbNullChar & vbNullChar)
  End With
  
  Call SHFileOperationW(sh)
  
End Sub
 
 
Private Sub Command1_Click()
 
 Call DoCopy("c:\windows\system32\calc.exe;c:\windows\system32\notepad.exe", "d:\calc.exe;d:\notepad.exe", ";")
 
End Sub

Open in new window

0
 
nffvrxqgrcfqvvcCommented:
Separator :)
0
 
nirojexpertCommented:
see the sample code below
it will copy the myfile1 to newfile1 in the same directory and also you can try netwrok paths.


strFilesList =  "myfile1,newfile1;myfile2,newmyfile2;\\192.168.5.40\c$\abc.txt,d:\def.txt"
 
CopyFILES strFileList
 
Sub CopyFILES(strFileName)
   ss = split(strFileName, ";")
  for a = lbound(ss) to ubound(ss)
    fset = split(ss(a), ",")
    filecopy fset(0), fset(1)
  next a
End Sub

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now