Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Copying Multiple Files (VB6)

Posted on 2003-03-17
10
Medium Priority
?
5,277 Views
Last Modified: 2012-06-21
Hi, I'm trying to copy multiple files from one location to another.

I need to copy all files with the .mdl extension to /models/ .spr to /sprites/ etc, the source folder contains .spr .mdl and .wav files.

Here's what I've got at the moment

Private Sub btnTransfer_Click()
Dim Source As String
Dim Target As String
Dim Sprites As String
Dim Models As String
Dim Sounds As String

Source = dirSource.Path + "\"
Target = dirCS.Path + "\"
Sprites = Target + "sprites\"
Models = Target + "models\"
Sounds = Target + "sound\weapons\"
FileCopy Source & "*.spr", Sprites
FileCopy Source & "*.mdl", Models
FileCopy Source & "*.wav", Sounds
End Sub


When I try to use this all that happens is that I get an error 52.

I'm not too sure whether I can use wildcards with filecopy, but I don't know any other way of moving files from one place to another.
0
Comment
Question by:Th0r0n
10 Comments
 
LVL 2

Expert Comment

by:CD-Softy
ID: 8154491
Hi,

The most efficient way of moving files from VB is to use the FileSystemObject. You will not be able to beat it's time and speed in native VB code. Here's how to do it...

Set a reference to the Microsoft Scripting Runtime.
In a Sub or Func...

Dim FSO As New FileSystemObject
Dim Folder As Folder
Dim File As File
Dim Source As String
Dim Target As String
Dim Sprites As String
Dim Models As String
Dim Sounds As String

Source = dirSource.Path
Target = dirCS.Path
Sprites = Target & "\" & "sprites"
Models = Target & "\" & "models"
Sounds = Target & "\" & "sound\weapons"


Set Folder = FSO.GetFolder(Source)

For Each File In Folder
    Select Case FSO.GetExtensionName(File.Name)
    Case Is = ".mdl"
        File.Move Models
    Case Is = ".spr"
        File.Move Sprites
    Case Is = ".wav"
        File.Move Sounds
Next
Set FSO = Nothing
Set File = Nothing
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8154791
If you want to copy files

Private Sub btnTransfer_Click()
    Dim Source As String
    Dim Target As String
    Dim cnt as integer
    for cnt = 0 to 2
      Select case cnt
          case is = 0
             Source = dirSource.Path + "\sprites\"
             Target = dirCS.Path + "\sprites\"
             fname = Dir("*.mdl")
          case is = 1
             Source = dirSource.Path + "\models\"
             Target = dirCS.Path + "\models\"
             fname = Dir("*.spr")
          case is = 2
             Source = dirSource.Path + "\sound\weapons\"
             Target = dirCS.Path + "\sound\weapons\"
             fname = Dir("*.wav")
      end Select
      do while fname > ""
          filecopy Source & fname, Target & fname
          fname = dir
      loop
    next cnt
End Sub

If you want to move them then just replace

      filecopy Source & fname, Target & fname
with
      Name Source & fname as Target & fname

this will move them and rename them if you want.

Without the overhead of the Filesystemobject and it is faster, the filesystemobject is useful if you as in VBScript without the VB runtime libraries.
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 8154854
I forgot to add the Source to the Dir function
add the Source path to all of the lines as below

    fname = Dir(Source & "*.mdl")

I also got the extensions mixed up

(got interupted)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Th0r0n
ID: 8155626
Ok thanks, I will try both methods and see which one works out best before I award points, I will also add 50 to the point total as you've both gone in depth to explain and provided me with source, thanks a lot.
0
 
LVL 11

Expert Comment

by:supunr
ID: 8155765
Private Sub btnTransfer_Click()
     Dim Source As String
     Dim Target As String
     Dim Sprites As String
     Dim Models As String
     Dim Sounds As String

     Source = dirSource.Path + "\"
     Target = dirCS.Path + "\"
     Sprites = Target + "sprites\"
     Models = Target + "models\"
     Sounds = Target + "sound\weapons\"
     CopyFiles Source, "*.spr", Sprites
     CopyFiles Source, "*.mdl", Models
     CopyFiles Source, "*.wav", Sounds
End Sub

Private Sub CopyFiles(SourcePath as string, SourceFiles as String, DestPath as string)
     Dim SourceFile as String

     SourceFile = Dir(SourcePath & SourceFiles)
     Do while (SourceFile = "")
          FileCopy SourcePath & SourceFile, DestPath & SourceFile
          SourceFile = Dir
     Loop
End Sub

Good Luck!
0
 

Author Comment

by:Th0r0n
ID: 8155768
Will this overwrite though Hornet? It's not working.. I'll paste you what I've got.

Private Sub Command1_Click()
Select Case MsgBox("This will overwrite all Weapons, Sprites and Models that you are installing! Be sure to back them up before you install them. Proceed?", vbYesNo)
Case vbNo
Exit Sub
Case vbYes

 Dim Source As String
   Dim Target As String
   Dim cnt As Integer
   For cnt = 0 To 2
     Select Case cnt
         Case Is = 0
            Source = dirSource.Path + "\sprites\"
            Target = dirCS.Path + "\sprites\"
            fname = Dir(Source & "*.spr")
         Case Is = 1
            Source = dirSource.Path + "\models\"
            Target = dirCS.Path + "\models\"
            fname = Dir(Source & "*.mdl")
         Case Is = 2
            Source = dirSource.Path + "\sound\weapons\"
            Target = dirCS.Path + "\sound\weapons\"
            fname = Dir(Source & "*.wav")
     End Select
     Do While fname > ""
         FileCopy Source & fname, Target & fname
         fname = Dir
     Loop
   Next cnt


End Select
End Sub

Could be my case messing it up.
0
 

Author Comment

by:Th0r0n
ID: 8155825
With the FSO method I get "Next without For" as an error.
0
 
LVL 3

Accepted Solution

by:
Hornet241 earned 400 total points
ID: 8156964
No it won't overwrite, if you want that you can add this line just berfore the filecopy

     Kill Target & fname

this will delete the file in the target directory.

If you have Option Explict on then you will need
    Dim fname

also ensure that
    dirSource.Path
    dirCS.Path
don't already have a trailing backslash
    e.g.
          dirSource.Path + "\sprites\"
          "C:\GamePath\" + "\sprites\"

and change the plus sign '+' to the ampersand '&'
      dirSource.Path + "\sprites\"
   to
      dirSource.Path & "\sprites\"
the '&' is the VB concatination, the '+' is a mathmatical operator


You could also change this

     Select Case MsgBox("This will overwrite all Weapons, Sprites and Models that you are installing! Be sure to back them up before you install them. Proceed?", vbYesNo)
       Case vbNo
         Exit Sub
       Case vbYes

to this

     ans = MsgBox("This will overwrite all Weapons, Sprites and Models that you are installing! Be sure to back them up before you install them. Proceed?", 36,"File Transfer")
if ans = 6 then  ' 6 is the yes, 7 is the no buttons
    'the code
end if
using the number 36 will also give the question icon in the Msgbox

If this doesn't work step through the code and see where and what error is happening
0
 

Author Comment

by:Th0r0n
ID: 8159283
Cheers Hornet, Very helpful.

Thanks for your efforts everyone else.
0
 

Author Comment

by:Th0r0n
ID: 8159728
BTW, I stepped through the code and it's not working because fname always = "", any ideas?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

571 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