Solved

Copying Files

Posted on 2009-05-18
6
349 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 150 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 150 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
Technology Partners: 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 200 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

763 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