Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    FilesystemObject (VB6)

    Having a brain lapse at the moment.

    Using FileSystemObject to copy a network folder and it's contents keeps running into err 70 permission denied.
    I've tried the overwrite statement after destination but the error is the same.
    Can anyone help:

    <pre>Private Sub Command1_Click()
    Dim fso As New FileSystemObject
    Dim LocF As String
    Dim NetF As String

    LocF = "C:PBackup"
    NetF = "Z:ICCM2"
    Text1.Text = "Please Wait !! Backing Up Paint Data"

    fso.DeleteFolder LocF
    fso.CreateFolder LocF

    fso.CopyFolder NetF, LocF
    Text1.Text = "Backup Complete"

    Unload Me

    End Sub</pre>


  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: FilesystemObject (VB6)

    Dave,
    That code looks fine to me and runs OK in a quick test I did (though not copying from a network drive). Which line does it error on? (I assume you can do all the steps manually OK so it's not a real folder permissions issue?)
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FilesystemObject (VB6)

    Rory

    It bombs out if the folder exists on LocF
    I could use the fso.folderexist feature but the folder has to be overwritten each time so there's not much point.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FilesystemObject (VB6)

    I have this problem occasionally; sometimes the network "locks" a folder without apparent cause.
    Try deleting all files in the destination folder, then copying all files from the source folder to the destination folder.

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FilesystemObject (VB6)

    Hans
    It looks like you were right.
    The code works ok on the target machine.

    Thanks Again

  6. #6
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: FilesystemObject (VB6)

    Hi Hans
    I have tried inserting Dave's code into a procedure, however, having input "Dim fso as New " the intellisense does not include "FileSystemObject" as one of the options. Currently, the project available references that are checked are only:
    Visual Basic For Applications
    Visual Basic runtime objects and procedures
    Visual Basic objects and procedures
    OLE Automation
    in that order.
    Any suggestions?
    Regards
    Don

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FilesystemObject (VB6)

    You must set a reference to the Microsoft Scripting Runtime library. FileSystemObject is part of this library.

  8. #8
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: FilesystemObject (VB6)

    Thank you Mentor. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>
    Regards
    Don

  9. #9
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: FilesystemObject (VB6)

    Hello Hans
    I will start by apologizing for an unclear message; because I am confused.
    Having made the reference which you recommended, the intellisense and all expected results performed on cue. Then today I came across the following code, which also works with no apparent flaws, yet there is no reference set to a scripting Library.
    <pre>Sub ClearReadOnly(filespec)
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    On Error GoTo NoFile
    Set f = fs.GetFile(fs.GetFileName(filespec))
    If f.Attributes And 1 Then
    f.Attributes = f.Attributes - 1
    End If
    NoFile:
    On Error GoTo 0
    End Sub
    </pre>


    Does the reference provide only the intellisense feature?
    Does the CreateObject command negate the need to set a reference to the specific library?
    I hope that I have given you sufficient insight into my misunderstanding that you can provide the key.

    TIA
    Regards
    Don

  10. #10
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FilesystemObject (VB6)

    Hi Don

    I'd be interested in what's happening here too. The code does generate an error for me at
    Set f = fs.GetFile(fs.GetFileName(filespec))

    if I specify a file on another drive, but works if I replace with:
    Set f = fs.GetFile(filespec)

    It does appear that the Scripting library is being accessed without an explicit reference to it ???

    Alan

  11. #11
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FilesystemObject (VB6)

    The code you posted is an example of "late binding".
    - There is no reference to the object library.
    - Objects are created using CreateObject or GetObject, not using the New keyword.
    - Object variables are declared as Variant (explicitly or implicitly by omitting the type) or as Object.
    - All symbolic constants from the object library have been replaced with their value.

    If you use a reference to the object library, it's called "early binding".

    Advantages of early binding:
    - You can use IntelliSense while writing the code.
    - If you run Debug | Compile <project>, you'll get warned if you used non-existing methods etc.
    - Code can be pre-compiled and thus run faster.

    Disadvantages of early binding:
    - Code may fail on other systems if the library is not installed in the same location.

    And conversely

    Advantages of late binding:
    - Will work on other systems if the library is installed in a different location.

    Disadvantages of late binding:
    - No IntelliSense while writing the code.
    - No syntax check.
    - Code may run slower.

    If I need to use late binding, I usually write and test the code using early binding, i.e. with a reference set.
    When the code has been tested to my satisfaction, I convert it to late binding:
    - Look up the values of symbolic constants in the Object Browser.
    - Use Edit/Replace to replace the constants with their values throughout the project.
    - Change all declarations of a specific type from the object library to As Object.
    - Don't use the New keyword, create the object with CreateObject or GetObject instead.
    This way, you still benefit from IntelliSense and syntax checking while reading the code.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FilesystemObject (VB6)

    GetFileName returns only the file name, not the path. So GetFile will fail if the file cannot be found in the current path. If filespec includes the path, there is no reason to use GetFilename here.

    For the rest, see my reply to Don Wells.

  13. #13
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: FilesystemObject (VB6)

    Hans
    Thank you for the clear answer. Unfortunately, the more I learn the more questions I have.
    Until now I visualized the object libraries being embedded within the application holding the code. Your guidance leaves me to believe that the various libraries are distributed throughout the Windows environment; their location dependent on choices made by the installer. Therefore, late binding will not be susceptible to misplaced (?) object libraries on the author's machine; but may fail on another machine.
    Have I captured the correct concept?
    Regards
    Don

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: FilesystemObject (VB6)

    The various object libraries are separate files. If you select Project | References... (in VB6) or Tools | References... (in VBA), and click on one of the references in the list, you'll see (part of) the path of the library file. There are several types of file you can set a reference to: .dll, .olb, .tlb, .exe, .ocx.

    Your application .exe would become VERY large if you would include all object libraries in it, moreover it would result in code duplication of gigantic proportions.

    Object libraries are registered in the Windows Registry. If you use late binding, and use code like this:

    Set fs = CreateObject("Scripting.FileSystemObject")

    the application looks up Scripting.FileSystemObject in HKEY_CLASSES_ROOT. Under this key, there is a value named CLSID. It then looks up this value in HKEY_CLASSES_ROOTCLSID, finds more info there, etcetera, eventually finding where the library is installed on the system.

    This is very useful when you're automating an Office application, say Word. If you have Word 2003, you'd set a reference to the Microsoft Word 11.0 Object Library. Your application would fail on a system with Word 2000, since the object library on that system is version 9.0. But if you use late binding, you'd omit the reference, and use code like

    Set appWord = CreateObject("Word.Application")

    In runtime, the code will look up Word.Application, find its CLSID, and this will refer to the version of Word installed on the system running the code, so your code is in effect version-independent (as long as you don't use features that are only available in Word 2003).

    Note: if you use early binding with Office applicaitons, references will be resolved automatically if the user has the same version or a newer version, but not if the user has an older version.

  15. #15
    Silver Lounger
    Join Date
    Jul 2001
    Location
    Ottawa, Ontario, Canada
    Posts
    1,609
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: FilesystemObject (VB6)

    Thank you Hans
    Regards
    Don

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •