CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    How to copy files

    How do I copy a file from one directory to another? It seems that the SHELL command only execute ".exe" type of files. Thus, copy does not work for me.
    Thanks in advance.


  2. #2
    Join Date
    Feb 2000
    Posts
    5

    Re: How to copy files

    Use the VB FileSystemObject method CopyFile

    Dim fso as New FileSystemObject (note that the Microsoft Scripting Runtime library must be referenced in your project)

    fso.CopyFile "from path" "to path"

    hope that helps

    PSH

  3. #3
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: How to copy files

    There are a couple of ways to do this:
    1)

    Sub CopyFiles(SourceFile as string, TargetFile as string)
    FileCopy SourceFile, TargetFile
    End Sub



    That will work in native VB without setting any references. SourceFile and Target must be full paths to the file.
    2) Set a reference to the Microsoft Scripting Runtime

    Sub CopyFiles(SourceFile as string, TargetFile as string)
    Dim FSO as Scripting.FileSystemObject

    set FSO = new Scripting.FileSystemObject
    'CopyFile (Source, Target, Overwrite Existing as Boolean)
    FSO.CopyFile SourceFile, TargetFile, true

    set FSO = nothing

    End Sub



    I like the second way better because I can specify if I want the file overwritten if it already exists, you don't have that option in the first way.

    Hope this helps,
    John

    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured