Page 2 of 2 FirstFirst 12
Results 41 to 58 of 58

Thread: Shelling to cmd window Wish to wait until process finished and then return

  1. #41

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Well, I'm still trying...

    Code:
    Option Explicit
    
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    
    Private Type SHFILEOPSTRUCT
        hWnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAborted As Boolean
        hNameMaps As Long
        sProgress As String
    End Type
    
    Private Const FO_COPY = &H2
    
    Private Sub Command1_Click()
        CopyFolder("C:\Users\Alan\ATestDocuments", "C:\Users\Alan\BoohBah")
        '~~> Usage ~~> CopyFolder(From,To)
        '~~> Example ~~> CopyFolder("C:\Temp\1","C:\Temp\2")
    End Sub
    
    Private Function CopyFolder(ByVal sFrom As String, ByVal sTo As String) As Boolean
        Dim SHFileOp As SHFILEOPSTRUCT
        On Error GoTo CF_Err
        CopyFolder = False
        With SHFileOp
            .wFunc = FO_COPY
            .pFrom = sFrom
            .pTo = sTo
        End With
        SHFileOperation SHFileOp
        CopyFolder = True
        Exit Function
    CF_Err:
        MsgBox "Following error occurred while copying folder " & sFrom & vbCrLf & _
        Err.Description, vbExclamation, "Error message"
    End Function
    and I'm getting a syntax error in the CopyFolder line
    Thanks all !

  2. #42

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Well, I'm still trying...

    Code:
    Option Explicit
    
    Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    
    Private Type SHFILEOPSTRUCT
        hWnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAborted As Boolean
        hNameMaps As Long
        sProgress As String
    End Type
    
    Private Const FO_COPY = &H2
    
    Private Sub Command1_Click()
        CopyFolder("C:\Users\Alan\ATestDocuments", "C:\Users\Alan\BoohBah")
        '~~> Usage ~~> CopyFolder(From,To)
        '~~> Example ~~> CopyFolder("C:\Temp\1","C:\Temp\2")
    End Sub
    
    Private Function CopyFolder(ByVal sFrom As String, ByVal sTo As String) As Boolean
        Dim SHFileOp As SHFILEOPSTRUCT
        On Error GoTo CF_Err
        CopyFolder = False
        With SHFileOp
            .wFunc = FO_COPY
            .pFrom = sFrom
            .pTo = sTo
        End With
        SHFileOperation SHFileOp
        CopyFolder = True
        Exit Function
    CF_Err:
        MsgBox "Following error occurred while copying folder " & sFrom & vbCrLf & _
        Err.Description, vbExclamation, "Error message"
    End Function
    and I'm getting a syntax error in the CopyFolder line
    Thanks all !

  3. #43

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    I've been hours searching for a solution. The posts I have found all have problems, and no expert has been able to answer them!.
    Thanks all !

  4. #44
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Shelling to cmd window Wish to wait until process finished and then return

    No offense, with nearly 700 posts, we'd think your skills should have been improved a bit to answer at least that question... Remove the parentheses

    As far as "no expert been able to answer them"... I'd say there are many here that have tried everything short of writing your code for you. You have also changed your mind several times. Maybe you should take a bit of time off, regroup, and try again? Sometime that helps re-focus.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #45
    Addicted Member
    Join Date
    Jun 2018
    Posts
    189

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Quote Originally Posted by el84 View Post
    I didn't expect that. What I wanted was an idea of what I should insert in my own code (at #18). Suggestions so far have tended to use terms that are way above my head! What I've nutted out so far works very well apart from the lack of recursion. And I have already searched for other mentions of it here or elsewhere without success. The trouble with experts which all of you obviously are is that advice assumes knowledge, which is sparse on my part!
    Is this exactly what you need since you had preferred FSO?

    The approach is extremely simple and easy for your quick understanding and hope it is adjustable for any of your needs very easily.

    The attached sample includes,

    1. Recursive stuff for file listing.
    2. ~ character filtering as per your need.
    3. Copy files to Destination.
    4. Add Files to list.

    Hope this will help you...
    Attached Files Attached Files
    Last edited by PGBSoft; Aug 11th, 2018 at 04:30 AM.

  6. #46

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Thank you very much PGBSoft. This appeared to be exactly what I needed and the way you have laid it out really helps me. Unfortunately, however, the files are transferred OK but not the folder structure. I want the destination folder to be identical to the source folder in every way (Except that it should omit any files stating with "~" (ie files that have not been properly deleted).

    Could that be fixed?

    EDIT: have tried the program with the "~" filter omitted, so that I can compare the origin with the destination file.

    Now I get
    Origin: 5581 files 623 folders
    Destination: 4593 files 0 folders

    BTW all, we've moved far away from Shell now. Is there any way I can retitle the thread?
    Last edited by el84; Aug 11th, 2018 at 04:32 PM.
    Thanks all !

  7. #47
    Addicted Member
    Join Date
    Jun 2018
    Posts
    189

    Thumbs up Re: Shelling to cmd window Wish to wait until process finished and then return

    Quote Originally Posted by el84 View Post
    Thank you very much PGBSoft. This appeared to be exactly what I needed and the way you have laid it out really helps me. Unfortunately, however, the files are transferred OK but not the folder structure. I want the destination folder to be identical to the source folder in every way (Except that it should omit any files stating with "~" (ie files that have not been properly deleted).

    Could that be fixed?

    EDIT: have tried the program with the "~" filter omitted, so that I can compare the origin with the destination file.

    Now I get
    Origin: 5581 files 623 folders
    Destination: 4593 files 0 folders

    BTW all, we've moved far away from Shell now. Is there any way I can retitle the thread?
    Hi Alan,

    The modified sample attached herewith will create the folder structure of the source in the destination dynamically. Hence you will not be having any file duplication issues, where all the files filtered will be copied from source to destination. The procedure should work perfectly well. I have commented about the small modification I made for your easiness.

    However, I cannot guarantee the overall performance of the procedure.

    Please find the attachment.
    Attached Files Attached Files
    Last edited by PGBSoft; Aug 11th, 2018 at 05:28 PM.

  8. #48

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Once again PGBSoft many thanks for the help you've given me. I see that only one line (aprt from some comments) was changed. Can you explain how that change works?

    EDIT this next part seems OK after experiment #### >>>

    Also, do the files we are copying overwrite them if they are already there? I am hoping that happens. However, if I add a new file to a subfolder in the destination folder, don't want that whole subfolder to be overwritten by the source subfolder as it may have a new file in the destination. I may have verified this by experiment by the time you reply. If not, your reply would be very helpful.

    <<<< EDIT verified

    EDIT: I have now experimented and found that if there is an exiting subfolder in the destination folder,, the copy process will copy and overwrite any changed files but copying the subfolder does not actually overwrite the destination subfolder and so will not remove any destination hosted files. That is what I wanted. Thanks again!

    It would probably help if I explain the purpose of all this. I deliberately keep all my VB6 projects in a subfolder in My Documents. I am going to automate a process I do manually at the moment, which is to regularly copy the My Documents folder to two standby computers on the network. But, I will be placing this project on the destination computers and don't want it lost when next copying from the source My Documents.
    Last edited by el84; Aug 11th, 2018 at 07:54 PM.
    Thanks all !

  9. #49

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    < > This was an exact copy of the last post by me. Whenever I post I get a message to try again,. Then I have two. What am I doing wrong please
    Thanks all !

  10. #50
    Addicted Member
    Join Date
    Jun 2018
    Posts
    189

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Quote Originally Posted by el84 View Post
    I see that only one line (aprt from some comments) was changed. Can you explain how that change works?
    The change will dynamically setup the folders identical to the source location to create them dynamically in the destination, where the files are copied.

    Quote Originally Posted by el84 View Post
    Also, do the files we are copying overwrite them if they are already there? I am hoping that happens.
    Yes, the recursive procedure exactly does this.

    However, if you ever need not to happen this (not to overwrite the destination files and only copy the new files from source to destination excluding modified files in the source), may be for a faster operation, just change the CopyFile line in the sample as follows and turn on the Resume Next statement in error handler if not done so. So if you like, you can experiment this with the sample.

    Code:
    .CopyFile sPath, sCopyTo & sFile, False
    Quote Originally Posted by el84 View Post
    However, if I add a new file to a subfolder in the destination folder, don't want that whole subfolder to be overwritten by the source subfolder as it may have a new file in the destination.
    Yes. The procedure exactly complies with this as you have already experimented.
    Last edited by PGBSoft; Aug 11th, 2018 at 10:42 PM.

  11. #51

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    < >
    Thanks all !

  12. #52

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Hi PGBSoft

    Well, having got your code working well on the local machine, I ported it to the remote machine which was my original intention, to copy a folder on the main machine (known as BUILD2) with its subfolders and structure intact to a named folder on the remote machine. The only important stuff that has changed is the destination and origin and I've made a few other changes and additions which all worked originally. But now, I am once again losing the structure for some reason. Here is the code. I've searched hard for the reason why it is not now working and cannot make it out!

    Code:
    ' OutstationCopy in VB6Stuff\Vb6LocalNetworkCopy
    ' from Build2 PGBNetworkCopyFolder on Build2
    ' Modified version 2
    'This version modified for copying across network
    ' No need for Project > References > Microsoft scripting runtime?
    Option Explicit
    '----------------------------------------------------------------------------------------------------------------
    Private Sub cmdGo_Click() '### A changed
        Dim aSource As String, aDest As String
        ''#####################################
        aSource = "\\BUILD2\Users\Alan\ATestDocuments\"
        aDest = "C:\Users\Alan\ATestBoohBahCopy\"
        '#####################################
        
        cmdExit.Enabled = False '#### A added
        Call CopyFolder(aSource, aDest)
        cmdExit.Enabled = True '#### A added
        MsgBox "Operation done with file count: " & lstStatus.ListCount '#### A renamed
    End Sub
    '-----------------------------------------------------------------------------------------------------
    Private Sub CopyFolder(sCopyFrom As String, Optional sCopyTo As String = "")
        On Error GoTo Err
        
        Dim FSO As Object, SourceFolder As Object
        Dim SubFolder As Object, FileItem As Object
        Dim sFile As String, sPath As String
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = FSO.GetFolder(sCopyFrom)
        
        With FSO
            'Create destination folder if not exists
            '#### A do NOT want to delete live destination folder as
            ' will have this outstation project in it!
            If sCopyTo <> "" Then _
            If Not .FolderExists(sCopyTo) Then Call .CreateFolder(sCopyTo)
        
            For Each FileItem In SourceFolder.Files
                DoEvents
                If Not FileItem Is Nothing Then
                    With FileItem
                        sFile = CStr(.Name)
                        sPath = CStr(.Path)
                    End With
                    'added ~ character filtering as per your need ' ##### A out for tests
                    ' If (CStr(Mid(sFile, 1, 1)) <> "~") 'Miss deleted out for tests
                        If sCopyTo <> "" Then _
                        lstStatus.AddItem sPath 'add file to the list '#### A renames and reversed
                        lstStatus.ListIndex = lstStatus.ListCount - 1 '#### A inserted
                        .CopyFile sPath, sCopyTo & sFile 'this is where we copy files to the destination
                    ' End If '#### A out for tests
                End If
            Next FileItem
        End With
        
        'Recursive stuff for you
        For Each SubFolder In SourceFolder.SubFolders
            'I just modified the second argument here to have the folder structure from the source
            CopyFolder SubFolder.Path, Replace(SubFolder.Path, sCopyFrom, sCopyTo) & "\"
        Next SubFolder
        
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
    Exit Sub
    
    Err:
        Debug.Print Err.Number & ": " & Err.Description
        'Resume Next ' A #### Remove comment spick if continue required
    
    End Sub
    '---------------------------------------------------------------------------------------------------------
    Private Sub cmdExit_Click() '### A added
        Close
        Unload Me
    End Sub
    Last edited by el84; Aug 12th, 2018 at 06:44 AM.
    Thanks all !

  13. #53
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Quote Originally Posted by el84 View Post
    It would probably help if I explain the purpose of all this. I deliberately keep all my VB6 projects in a subfolder in My Documents. I am going to automate a process I do manually at the moment, which is to regularly copy the My Documents folder to two standby computers on the network. But, I will be placing this project on the destination computers and don't want it lost when next copying from the source My Documents.
    No offense, but there's no reason whatsoever to struggle with VB code for this at all. A carpenter doesn't need to use a hammer to take the garbage out, and doesn't need to use a saw to wash his own windows.

    What you are looking for can be done in a 2 line batch file using the "XCopy" command. No software to download, no recursive code to worry about. Open up a command prompt, type "XCopy /?" (without the quotes), read up on the parameters, and go from there. Then you can automate it by running it as a scheduled task as often as you like.

    It never hurts to take a step back from the "How can I do this with VB" point of view. There's more automation to be done outside of VB.

  14. #54
    Addicted Member
    Join Date
    Jun 2018
    Posts
    189

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Quote Originally Posted by el84 View Post
    But now, I am once again losing the structure for some reason. Here is the code. I've searched hard for the reason why it is not now working and cannot make it out!
    I assume the problem might be related to the string comparison approach in recursion call. Consider the code line in red in the Code block.

    Code:
    ' OutstationCopy in VB6Stuff\Vb6LocalNetworkCopy
    ' from Build2 PGBNetworkCopyFolder on Build2
    ' Modified version 2
    'This version modified for copying across network
    ' No need for Project > References > Microsoft scripting runtime?
    Option Explicit
    '----------------------------------------------------------------------------------------------------------------
    Private Sub cmdGo_Click() '### A changed
        Dim aSource As String, aDest As String
        ''#####################################
        aSource = "\\BUILD2\Users\Alan\ATestDocuments\"
        aDest = "C:\Users\Alan\ATestBoohBahCopy\"
        '#####################################
        
        cmdExit.Enabled = False '#### A added
        Call CopyFolder(aSource, aDest)
        cmdExit.Enabled = True '#### A added
        MsgBox "Operation done with file count: " & lstStatus.ListCount '#### A renamed
    End Sub
    '-----------------------------------------------------------------------------------------------------
    Private Sub CopyFolder(sCopyFrom As String, Optional sCopyTo As String = "")
        On Error GoTo Err
        
        Dim FSO As Object, SourceFolder As Object
        Dim SubFolder As Object, FileItem As Object
        Dim sFile As String, sPath As String
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set SourceFolder = FSO.GetFolder(sCopyFrom)
        
        With FSO
            'Create destination folder if not exists
            '#### A do NOT want to delete live destination folder as
            ' will have this outstation project in it!
            If sCopyTo <> "" Then _
            If Not .FolderExists(sCopyTo) Then Call .CreateFolder(sCopyTo)
        
            For Each FileItem In SourceFolder.Files
                DoEvents
                If Not FileItem Is Nothing Then
                    With FileItem
                        sFile = CStr(.Name)
                        sPath = CStr(.Path)
                    End With
                    'added ~ character filtering as per your need ' ##### A out for tests
                    ' If (CStr(Mid(sFile, 1, 1)) <> "~") 'Miss deleted out for tests
                        If sCopyTo <> "" Then _
                        lstStatus.AddItem sPath 'add file to the list '#### A renames and reversed
                        lstStatus.ListIndex = lstStatus.ListCount - 1 '#### A inserted
                        .CopyFile sPath, sCopyTo & sFile 'this is where we copy files to the destination
                    ' End If '#### A out for tests
                End If
            Next FileItem
        End With
        
        'Recursive stuff for you
        For Each SubFolder In SourceFolder.SubFolders
            'I just modified the second argument here to have the folder structure from the source
            CopyFolder SubFolder.Path, Replace(SubFolder.Path, sCopyFrom, sCopyTo, , , vbTextCompare) & "\"
        Next SubFolder
        
        Set FileItem = Nothing
        Set SourceFolder = Nothing
        Set FSO = Nothing
    Exit Sub
    
    Err:
        Debug.Print Err.Number & ": " & Err.Description
        'Resume Next ' A #### Remove comment spick if continue required
    
    End Sub
    '---------------------------------------------------------------------------------------------------------
    Private Sub cmdExit_Click() '### A added
        Close
        Unload Me
    End Sub

  15. #55

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    OK. Many thanks again for your patient help, PGBSoft. I'll try this later today. The problem seemed to arise when the only important change appeared to be that now I am trying to use the folder copy across the network.

    To OptionBase1, your point is valid, but I am intending to do much more with this program. Copying a folder and all its structure and files across the network is only part of it and is the part I'm trying to get to work first. So I'll be persisting with developing this using Vb6.

    Can anyone see why I always get the message that I am posting too quickly (within 30 secs) and so have to submit again, whereupon *both* posts appear (duplicate)?
    Thanks all !

  16. #56
    Lively Member
    Join Date
    Oct 2009
    Location
    Nijmegen, Netherlands
    Posts
    64

    Re: Shelling to cmd window Wish to wait until process finished and then return

    There is a KnowledgeBase Archive article: https://jeffpar.github.io/kbarchive/kb/129/Q129796/ that deals with using Shell and wait for it... (Haven't read all replies, but seems to answer your original question..)
    I used this method a long time ago to start a DOS text search program, and waiting for it to return....
    Last edited by _Wim_; Aug 13th, 2018 at 12:35 PM.

  17. #57
    Addicted Member
    Join Date
    Jun 2018
    Posts
    189

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Quote Originally Posted by _Wim_ View Post
    There is a KnowledgeBase Archive article: https://jeffpar.github.io/kbarchive/kb/129/Q129796/ that deals with using Shell and wait for it... (Haven't read all replies, but seems to answer your original question..)
    I used this method a long time ago to start a DOS text search program, and waiting for it to return....
    Same idea already discussed very early in #8, but el84 is not interested with it.

  18. #58

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Location
    Australia
    Posts
    1,162

    Re: Shelling to cmd window Wish to wait until process finished and then return

    Thanks for continuing ideas. I have had to catch up with work for the past couple of days but I hope to be trying PGBSoft's latest modification today.

    EDIT:

    Hi PGBSoft

    I've now implemented the change, and the program now appears to work fine across the network. I would love to know exactly how that changed line works, and why the original line worked fine locally but not across the network. I'm keen to learn and would like to understand the mechanism if possible.

    Many thanks to PGBSoft. And many thanks to all and for (usually) hiding any frustration!
    Last edited by el84; Aug 13th, 2018 at 03:57 PM.
    Thanks all !

Page 2 of 2 FirstFirst 12

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