Results 1 to 8 of 8
  1. #1
    baiocco
    Guest

    FileCopy command

    I am struggling with what should be a relatively simple
    manipulation. I am trying to make a command within a VB
    subroutine to copy an unspecified number of files from one
    directory to another. The command would theoretically be
    something like:

    FileCopy "c:dir1*.*", "c:dir2*.*"

    VB doesn't seem to be very flexible with this syntax. Apparently the FileCopy function only likes to copy one
    file at a time, and the name of that one file must be written explicitly.

    Does any one have a solution whether it be a shell escape DOS command of some other VB command to do this fairly simple task?

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: FileCopy command

    If you can set up a reference to Microsoft Scripting Runtime, the following code will do what you want.

    Sub fsoCopyFiles()
    Dim fso As Scripting.FileSystemObject
    Set fso = New Scripting.FileSystemObject
    fso.CopyFile "Cir1*.*", "Cir2"
    End Sub

    As it stands, it wil overwrite any files with the same name that it encounters. If you use fso.CopyFile "Cir1*.*", "Cir2" , False, then an error message is generated if it is asked to copy a file over a file with the same name.

    You should ahve the Scripting Runtime (scrrun.dll) if you have Office 2000, and may even have it if you have Office 97. As far as I know, it can be downloaded from Microsoft's website.

    Hope it helps.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: FileCopy command

    If you have Office, you can set a reference to its object library and use late binding with the FileSystemObject without setting a reference to the scripting runtime. This is useful on networks where the scripting runtime is not allowed, but it requires dimming the variables are Object and then setting them to New FileSystemObject in the body of the code.
    Charlotte

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileCopy command

    Don't forget good ol'

    Wordbasic.copyfile Filename:=source fullpath, directory:=destination fullpath

    see <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/WordBasicCommands.htm>this link</A> for more info
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: FileCopy command

    I am just learning to use VB in Access 97. I need to accomplish exactly what your code attempts to complete---copy files from one directory to another; however, once I go to the debug window and type "run", I get the following error message " Compile error. Argument not optional". I did validate through the Project menu and references that Microsoft Scripting Runtime was selected or activated. I believe I have tracked the message down to the Sub statement. When I went to the help file, created a new module and typed sub procedure example from the VB help file, I get the same message. For reference, the VB sub statement example is below minus the comments:

    Sub SubComputeArea (Length, TheWidth)
    Dim Area as Double
    If Length = 0 or TheWidth = 0 Then
    Exit Sub
    End If
    Area = Length * TheWidth
    Debug.Print Area
    End Sub

    Needless to say, I'm really lost. Thanks in advance for anyone who takes a moment to help me.............take care. <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: FileCopy command

    Since your sub requires two arguments (length and width), you can not just run it. Enter this procedure and run it:

    <pre>Public Sub Test()
    Call SubComputeArea(2, 4)
    End Sub
    </pre>

    Legare Coleman

  7. #7
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: FileCopy command

    Thank you. I'll try it. Do I also need to pass arguments to the code posted by Andrew on post #45919? If so, what would they be?

  8. #8
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: FileCopy command

    Jim,

    The code for copying the files does not need any arguments passed to it as it is constructed, but you do need to alter the line

    fso.CopyFile "Cir1*.*", "Cir2"

    to refer to the actual directories you want to deal with.

    The following is an adaption to take the start and destination paths as arguments : <pre>Sub fsoCopyFiles(strPath1, strPath2)
    Dim fso As Scripting.FileSystemObject
    Set fso = New Scripting.FileSystemObject
    fso.CopyFile strPath1, strPath2
    End Sub</pre>

    You could use something like <pre>Call fsoCopyFiles("Cir1*.*", "Cir2")</pre>

    to call the code.

    Andrew C

Posting Permissions

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