?
Solved

Copying Files

Posted on 2009-05-18
6
Medium Priority
?
364 Views
Last Modified: 2013-11-25
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
Comment
Question by:Whing Dela Cruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Assisted Solution

by:game-master
game-master earned 450 total points
ID: 24417562

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
 
LVL 6

Assisted Solution

by:nirojexpert
nirojexpert earned 450 total points
ID: 24418227
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
 

Author Comment

by:Whing Dela Cruz
ID: 24427804
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!

 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 600 total points
ID: 24428836
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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 24428853
Separator :)
0
 
LVL 6

Expert Comment

by:nirojexpert
ID: 24438205
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question