Solved

Copying Files

Posted on 2009-05-18
6
338 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
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now