?
Solved

Copying Multiple Files (VB6)

Posted on 2003-03-17
10
Medium Priority
?
5,196 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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