FileSystemObject? Is This VB6.0's Object for File Manipulation?


I'm a beginning VB6.0 learner and I'm trying to write a small VB program which copies/backsup VB files I'm working on into a directory/folder on a FAT16 drive (WIN98 is FAT32 and NT is NTFS). This will enable me to work with these files when I run NT ( NTFS ).

I understand (I think enough) objects in that everything in the VB world (as in the C++ world) is an object, which has properties and methods. All of the help documentation describes the methods associated with file manipulation(i.e. file I/O, writing to, reading from, appending, COPYING) as being part of an object known as: FileSystemObject.

Yet, when I try to create (instantiate) an instance of this object,
as in:

Option Explicit
'Dim fso As FileSystemObject

Private Sub cmdStart_Click()
    MsgBox "Copying files. . . ."
    FileCopy "d:\Program Files\Microsoft Visual     Studio\VB98\Works_In_Progress\*.*",     "c:\VB_Shared_Backup\"
    MsgBox "All done!"

End Sub

It didn't like the fso object declaration, which is why it's
commented out. Using FileCopy as above seems to do something until I click "OK" and then I get error #52, "Bad file name or number".

This seems to me to be somewhat straightforward, but it's not.
If I try to define an object of type FileSystemObject as in:

Option Explicit
Dim fileObject As FileSystemObject

I receive a "User-defined type not defined." error.

Which method associated with which object(s) is/are needed here?


Who is Participating?
VbmasterConnect With a Mentor Commented:
If you read the VB helpfile you ought to see what is the problem with your FileCopy statement.

"FileCopy Statement

Copies a file."

This means that the two parameters MUST be single files, using tokens won't work. You will have to scan thru the directories and copy the files that are inside.

Listing the files in a directory is really simple, you can check out for a couple of solutions.

In most cases using the FileSystemObject means unnecessary large overhead and you depend on M$ not screwing things up with version-compatibility stuff etc.
did you, in project references  add "microsoft scrpting runtime" (scrrun.dll) ?

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "source", "dest"
jnowlinAuthor Commented:
Thanks jbil. I had *not* added Microsoft
Scripting Runtime to the project, as this is still fairly new to me. When I added it, SCRRUN.DLL, the error message regarding the user-defined type being undefined went away.

Thanks to hes, also. Between your code and jbil's comment, I now have a basic, working program. This is all I needed to do at this early stage. I agree with
VBmaster that there are more efficient and effective ways to code this. Now, I can work up to the challenge, I can now
"tweak" this program and modify it to suit different file copying needs and get it to run with minimal overhead.
I'll now go to the Web site that you've suggested VBmaster.

jnowlinAuthor Commented:
Thanks VBmaster.

is a tremendous resource for VB. It's really going to be helpful.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.