Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

DirectoryCopy in VB 6

Options
  • 21-06-2005 12:00pm
    #1
    Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭


    What's the easiest way to do either of the following

    a) copy a directory (and sub-directories) from one location to another
    b) copy the files from one directory into another

    in VB 6, given that the directory names will be given by the user as strings?


Comments

  • Closed Accounts Posts: 25 maxcherry


    Ok - I do not have .NET(only VB6) but this would work I guess.

    Hey you will to put Error trapping in. ie. does the dir already exist?, file already exist?, is the file still there that you want to copy? etc. etc.



    RecursiveCopy "C:\ZZZ\", "D:\ZZZ\", True



    Public Sub RecursiveCopy(ByVal strSource As String, ByVal strDest As String,
    ByVal boolRecursive As Boolean)
    Dim dirWork, dirIter As System.IO.Directory
    Dim filWork As System.IO.File
    Dim intCounter, intSlashPos As Integer
    Dim strDir, strFile As String
    Dim astrDirs(), astrFiles() As String

    'Check to make sure both strSource and strDest end with a slash.
    If (strSource.Substring(strSource.Length - 1, 1) <> "\") Then
    strSource = strSource + "\"
    End If
    If (strDest.Substring(strDest.Length - 1, 1) <> "\") Then
    strDest = strDest + "\"
    End If

    ' If this is a recursive copy start looking for sub-directories.
    If (boolRecursive = True) Then
    astrDirs = dirIter.GetDirectories(strSource)
    For intCounter = 0 To UBound(astrDirs)
    intSlashPos = astrDirs(intCounter).LastIndexOf("\")
    ' Grab the sub directory name from the string array
    ' which contains the full paths plus file names.
    strDir = astrDirs(intCounter).Substring((intSlashPos + 1), (astrDirs(intCounter).Length) - (intSlashPos + 1))
    dirWork.CreateDirectory (strDest + strDir), CopyDir(astrDirs(intCounter), strDest + strDir, True)
    Next

    End If

    astrFiles = dirIter.GetFiles(strSource)

    For intCounter = 0 To UBound(astrFiles)
    intSlashPos = astrFiles(intCounter).LastIndexOf("\")
    ' Grab the file name from the string array which contains the full paths plus file names.
    strFile = astrFiles(intCounter).Substring((intSlashPos + 1), (astrFiles(intCounter).Length) - (intSlashPos + 1))
    filWork.Copy astrFiles(intCounter), strDest + strFile

    Next

    End Sub


  • Closed Accounts Posts: 92 ✭✭tempest


    What's the easiest way to do either of the following

    a) copy a directory (and sub-directories) from one location to another
    b) copy the files from one directory into another

    in VB 6, given that the directory names will be given by the user as strings?

    GTFW


  • Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭pickarooney


    Cheers maxcherry, I'll test it and see id a) it works and b) I can understand it
    tempest wrote:
    GTFW

    TABTWAEHRAIFIFYA


  • Closed Accounts Posts: 25 maxcherry


    Let me know how you got on - Oh and one thing I just spotted is this

    dirWork.CreateDirectory (strDest + strDir), CopyDir(astrDirs(intCounter), strDest + strDir, True)

    You have to change the word CopyDir to RecursiveCopy above.

    As I said I have not got .net so I cannot even compile the code.

    If it works and you want me to explain any of the code let me know.


  • Closed Accounts Posts: 92 ✭✭tempest


    in VB 6, given that the directory names will be given by the user as strings?

    You asked for that in VB6....
    Dim dirWork, dirIter As System.IO.Directory
    Dim filWork As System.IO.File
    

    Fair play if you can get that to work on VB6...

    I still say:
    GTFW

    and complement it with 2nd link down..
    TABTWAEHRAIFIFYA
    You're welcome


  • Advertisement
  • Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭pickarooney


    Yeah, I should probably have specified this was for a Word macro, so none of the .net or Scripting commands are recognised. I've found lots of code examples, but they stetch up to several pages of code for something that's possible in one line of .net code, (or a dos, unix, perl... command) which seems insane.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Use FSO : FileScriptingObject, (which you'll find by adding a reference to Windows Scripting Control to your project, I believe).

    jc


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Yeah, I should probably have specified this was for a Word macro

    Word macros aren't written in VB6. Are you sure its VB6 you want? Or VBA, debeloped in the Visual Studio 6 IDE?
    , so none of the .net or Scripting commands are recognised.
    Can't you add references to your scripts, just like you would in a "real" VB6 project (Tools/References on the menu, IIRC).

    The problem is that you can't readily distribute these with the .doc file.

    I've found lots of code examples, but they stetch up to several pages of code for something that's possible in one line of .net code, (or a dos, unix, perl... command) which seems insane.
    Why is it insane? The one line of .net code exists because someone took what used to be an external object-model (the FSO) and incorporated a version of it into the core.

    At the end of the day, though, if you have a code example, why do you care how long it is? Copy/Paste is as easy on 1,000 lines of code as on 1.

    jc


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Incidentally, if DOS can do it in one line, can you use the Shell command in Word to run a one-line DOS statement?

    jc


  • Closed Accounts Posts: 8,264 ✭✭✭RicardoSmith


    VBA is so close to VB that most VB6 stuff works. Adding a reference to the project in the Word VBA editor is the same as VB and you have the same distrubution issues. You'll have to compile and installer to install the runtimes that are required. If you really want to avoid the runtimes, yes you can shell out and use the DOS commands or run a batch file and even send parameters into the batch file. Not very elegent but might work.

    I guess the question is why do you want to copy a directory in Word?


  • Advertisement
  • Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭pickarooney


    Sorry I asked now...
    OK, I suck at visual basic and don't know the difference between VBA and VB6 (it says VB 6.3 when I click on Help, About). I just thought there'd be a command like "call" or "system" or something which would allow me to use xcopy or similar.

    Anyway, thinking about it VB is not the tool I need for the job (not just this aspect of it) and i'd probably be better off learning python.

    Cheers,
    P


  • Closed Accounts Posts: 25 maxcherry


    Here ya go Mr Rooney:

    Sub Macro1()
    RetVal = Shell("XCOPY C:\ZZZ D:\ZZZ /E", 0)
    End Sub


    C:\ZZZ - Source
    C:\ZZZ - Destination
    /E - Copy subdirectories including empty ones
    0 - Hide the dos window - change this to 1 if you want to see it


  • Closed Accounts Posts: 8,264 ✭✭✭RicardoSmith


    ...Anyway, thinking about it VB is not the tool I need for the job (not just this aspect of it) and i'd probably be better off learning python....

    I'm curious as to what you were doing in Word that you can now do in Python? :confused:


  • Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭pickarooney


    Ah, shell, that was it. I'll know for next time, cheers.

    TBH, I've rethought this in about 4 languages so far and don't know if there's any reasonable way of doing the following:

    - read two paths (easy in any language)
    - create a couple of paths and copy files into them (any scripting language)
    - run a couple of existing word macros on the files in their new paths (VB something)
    - call a third-party application via its API and pass it the path to the macrified files (DOS)
    - check the log files of the third-party application for key words and if it finds them generate a report, otherwise copies the files to a second location and calls another part of the third-party application (bash/wingrep/DOS ?)
    - copy the files back to their original location

    I only tried VB-whatever as Id started learning it and couldn't think how else to call the macros in step 3.


Advertisement