Page 1 of 2 12 LastLast
Results 1 to 40 of 58

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

  1. #1

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

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

    I am using

    Shell("cmd.exe /k Copy "ThisFolder ThatFolder")

    The folders have many files in and these are displayed in the cmd window as the process continues, as you would expect. But all my attempts to close the cmd window and return to my vb6 program fail to wait until the copy process is finished. Is there a *simple* way to do this using the existing shell syntax (with something added)? If I use exit the window immediately closes although the copying is still going on; I want to wait until it is finished.
    Thanks all !

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

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

    search on shell and wait
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

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

    Errrr.... why do you use the shell instead of SHFileOperation or CopyFile-API?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

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

    Hi ,

    I agree with Zvoni, better use what he said, but is there a reason you use CMD ?

    well anyway here a few samples (was fun going back in Time)

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    'Delete all Files, not the Folders::
    Dim command As String
    command = "del C:\AAChris /s /f /q "
    'write to Textfile what was deleted
    Shell "cmd.exe /c " & command & "> ""C:\LogDeletedFiles.txt"""
    End Sub
    
    Private Sub Command2_Click()
    'Copy the Files::
    Dim command As String
    command = "xcopy c:\ChrisTest\*.* c:\AAChris\"
    'write to Textfile what was copied
    Shell "cmd.exe /c" & command & "> ""C:\CopiedFiles.txt"""
    
    Dim ff As Long
        Dim line As String
        ff = FreeFile
        Open "C:\CopiedFiles.txt" For Input As #ff
        Do While Not EOF(ff)
               Line Input #ff, line
               If Len(line) Then List1.AddItem line
        Loop
        Close #ff
    End Sub
    
    Private Sub Command3_Click()
    Dim command As String
    'Move Files::
    command = "move c:\ChrisTest\*.* c:\AAChris\"
    'write to Textfile what was moved
    Shell "cmd.exe /c " & command & "> ""C:\MovedFiles.txt"""
    End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    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. I was rather hoping someone would point me at a way of using my *existing code* and achieving what I wanted, rather than using a completely different way of doing things. But, having said that, post #4 has quite a few interesting ideas. the idea at Command 3 is intriguing, but how would that text file get shown on the vb6 form?

    EDIT: See post 7
    Last edited by el84; Aug 8th, 2018 at 03:11 PM.
    Thanks all !

  6. #6

    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

    < see below >
    Last edited by el84; Aug 8th, 2018 at 03:11 PM.
    Thanks all !

  7. #7

    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

    Many thanks Chris! After initial problems with file not found due to my stupidity, I have got your Command 2 version running, and now need to refine it.
    Last edited by el84; Aug 8th, 2018 at 04:06 PM.
    Thanks all !

  8. #8
    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 am using

    Shell("cmd.exe /k Copy "ThisFolder ThatFolder")

    The folders have many files in and these are displayed in the cmd window as the process continues, as you would expect. But all my attempts to close the cmd window and return to my vb6 program fail to wait until the copy process is finished. Is there a *simple* way to do this using the existing shell syntax (with something added)? If I use exit the window immediately closes although the copying is still going on; I want to wait until it is finished.
    See the attachment...
    Attached Files Attached Files
    Last edited by PGBSoft; Aug 9th, 2018 at 01:01 PM.

  9. #9

    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

    Hello again all

    The problem I had with writing file names to a text file and then reading that into the listbox was that the text file was being read after the copy had finished, whereas I wanted to show each file as it was copied.

    So I decided to use the file system object method instead, with the loop 'For each file...' Here is the code

    Code:
    Private Sub cmdEACH_Click()
        Dim oFolder As Folder
        Dim oFiles As Files
        Dim oFile As File
        
        'have already set Set FSys
        Set oFolder = FSys.GetFolder("C:\Users\Alan\ATestDocuments")
        Set oFiles = oFolder.Files
        
            For Each oFile In oFiles
            'copy routine here
            
            FSys.CopyFile oFile, "C:\Users\Alan\Documents\ATestCopyofTestDocs"
            '####### PERMISSION DENIED PROBLEM IN ABOVE LINE
            lstStatus.AddItem oFile
            lstStatus.ListIndex = lstStatus.ListCount - 1 'move 'lines up to display latest
        Next oFile
        
        Set FSys = Nothing
        Set oFolder = Nothing
        Set oFiles = Nothing
        'Set oFile = Nothing '?
        
    End Sub
    Obviously in my ignorance I am doing something wrong in the copy line, getting error 70. Can I please have ideas to correct this (rather than some completely different method)?
    Thanks all !

  10. #10
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

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

    And with the CopyFileEx-API you can use a callback-function to move a progressbar, write a progress/status to a file/textbox/listbox/wherever etc. etc.....
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  11. #11
    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

    Quote Originally Posted by el84 View Post
    Obviously in my ignorance I am doing something wrong in the copy line, getting error 70. Can I please have ideas to correct this (rather than some completely different method)?
    Try appending the file name to the target, i.e.,
    -- "C:\Users\Alan\Documents\ATestCopyofTestDocs\" & oFile.Name

    Edited. Your routine can be downsized a bit
    Code:
    Private Sub cmdEACH_Click()
        Dim oFile As File
        
        'have already set Set FSys
        For Each oFile In FSys.GetFolder("C:\Users\Alan\ATestDocuments").Files
            'copy routine here
            FSys.CopyFile oFile, "C:\Users\Alan\Documents\ATestCopyofTestDocs\" & oFile.Name
            lstStatus.AddItem oFile
            lstStatus.ListIndex = lstStatus.ListCount - 1 'move 'lines up to display latest
        Next oFile
        
        Set FSys = Nothing ' << if you are setting it from outside, you should release it from outside
        Set oFile = Nothing 
        
    End Sub
    P.S. Wouldn't it be more informative to show which file is being copied instead of which file was just copied? Move the listbox updating higher?
    Last edited by LaVolpe; Aug 9th, 2018 at 05:30 PM.
    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}

  12. #12

    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 that LaVolpe

    Using just your code I'm now getting error 424 Object required
    Thanks all !

  13. #13
    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

    It's not actually my code is it? Anyway, for us to help, we need to know what line the error occurs on
    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}

  14. #14

    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

    Sorry. I'm in a right mess. I plugged in the code you suggested but I must have left something vital out. I will post the whole code this time. Error is actually path not found

    Code:
    'Build2CopyExpmnt
    'Project > References > Microsoft Scripting Runtime
    
    Dim FSys As New FileSystemObject
    
    Private Sub cmdEACH_Click()
        Dim oFile As File
        
       For Each oFile In FSys.GetFolder("C:\Users\Alan\ATestDocuments").Files
            'copy routine here
            FSys.CopyFile oFile, "C:\Users\Alan\Documents\ATestCopyofTestDocuments\" & oFile.Name
            
            lstStatus.AddItem oFile
            lstStatus.ListIndex = lstStatus.ListCount - 1 'move 'lines up to display latest
        Next oFile
        
        Set FSys = Nothing ' << if you are setting it from outside, you should release it from outside
        Set oFile = Nothing
       
        
    
        
    End Sub
    
    Private Sub cmdExit_Click()
        Close
        Unload Me
    End Sub
    Thanks all !

  15. #15
    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

    I'm going to assume the error occurs on the .CopyFile line?

    before that line, insert this and verify no typos in the source or destination and also that the target path really does exist. Open your immediate window (Ctrl+G) to see the print-out.
    Code:
    Debug.Print "Source: "; oFile
    Debug.Print "Target: C:\Users\Alan\Documents\ATestCopyofTestDocuments\" & oFile.Name
    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}

  16. #16

    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 should explain that if the target folder is not there, I will want it created. I think that will be something to with If exists... Also (I should have mentioned this) my source folder has a number of subfolders in it!
    Thanks all !

  17. #17

    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 solved the original problem by testing whether folder exists

    Code:
    Private Sub cmdEACH_Click()
        
        Dim FSys As New FileSystemObject
        Dim oFile As File
        
       If Not FSys.FolderExists("C:\Users\Alan\Documents\ATestCopyofTestDocuments\") Then _
            FSys.CreateFolder "C:\Users\Alan\Documents\ATestCopyofTestDocuments\"
       
       For Each oFile In FSys.GetFolder("C:\Users\Alan\ATestDocuments").Files
            'copy routine here
            
            FSys.CopyFile oFile, "C:\Users\Alan\Documents\ATestCopyofTestDocuments\" & oFile.Name
            
            lstStatus.AddItem oFile
            lstStatus.ListIndex = lstStatus.ListCount - 1 'move 'lines up to display latest
        Next oFile
        
        Set FSys = Nothing ' << if you are setting it from outside, you should release it from outside
        Set oFile = Nothing
    End Sub
    Now I have to fix the Recursive issue!
    Thanks all !

  18. #18

    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 solved the original problem by testing whether folder exists

    Code:
    Private Sub cmdEACH_Click()
        
        Dim FSys As New FileSystemObject
        Dim oFile As File
        
       If Not FSys.FolderExists("C:\Users\Alan\Documents\ATestCopyofTestDocuments\") Then _
            FSys.CreateFolder "C:\Users\Alan\Documents\ATestCopyofTestDocuments\"
       
       For Each oFile In FSys.GetFolder("C:\Users\Alan\ATestDocuments").Files
            'copy routine here
            
            FSys.CopyFile oFile, "C:\Users\Alan\Documents\ATestCopyofTestDocuments\" & oFile.Name
            
            lstStatus.AddItem oFile
            lstStatus.ListIndex = lstStatus.ListCount - 1 'move 'lines up to display latest
        Next oFile
        
        Set FSys = Nothing ' << if you are setting it from outside, you should release it from outside
        Set oFile = Nothing
    End Sub
    Now I have to fix the Recursive issue!
    Thanks all !

  19. #19

    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

    And while waiting in case La Volpe is not gnashing his teeth too much at my apparent changing of the goalposts (!) I have further refined what I have to a) place the listbox lines before the copies, as suggested and b) test for files that begin with "~" and don't copy those. Also I have also placed both Set statements within the sub. I feel I just need to master the Recursive part now as my origin file contains many Subfolders and even some sub-sub folders! If there is a single command that will do that (much like copy does from the command window in DOS) -- xcopy? -- then I'll be happy.
    Thanks all !

  20. #20
    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
    And while waiting in case La Volpe is not gnashing his teeth too much at my apparent changing of the goalposts (!) I have further refined what I have to a) place the listbox lines before the copies, as suggested and b) test for files that begin with "~" and don't copy those. Also I have also placed both Set statements within the sub. I feel I just need to master the Recursive part now as my origin file contains many Subfolders and even some sub-sub folders! If there is a single command that will do that (much like copy does from the command window in DOS) -- xcopy? -- then I'll be happy.
    Will this help you in whatever manner?


    Code:
    Option Explicit
    
    Private Sub CopyFolder(sFrom As String, sTo As String)
    
      Dim FSO
      Set FSO = CreateObject("Scripting.FileSystemObject")
      FSO.copyFolder sFrom, sTo
      MsgBox "copying Done"
    
    End Sub
    
    Private Sub Command1_Click()
    
      'eg:
      Call CopyFolder("c:\Songs", "c:\Temp\songs")
    
    End Sub

  21. #21
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

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

    Hi El,

    try the API version, it will show a progressbar

    put this in a Modul....
    Code:
    'Module
    
    Option Explicit
    
    Private Const shl_COPY          As Long = 2
    Private Const shl_CopyStandard  As Long = &H250
    
    Private Type shl_FileOperation
        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 Declare Function API_GetActiveWindow Lib "user32" _
                                  Alias "GetActiveWindow" () As Long
    
    Private Declare Function API_SHFileOperation Lib "shell32" Alias _
                                  "SHFileOperationA" _
                                  (lpFileOp As shl_FileOperation) As Long
    
    'FileCopy über API mit Fortschrittsanzeige
    '
    Public Function FileCopyApi(Source As String, _
                                Destination As String) As Boolean
    
       Dim fop As shl_FileOperation
       Dim rcd As Long
       Dim flg As Boolean
        
          On Error Resume Next
          
          With fop
             .hwnd = API_GetActiveWindow
             .wFunc = shl_COPY
             .pFrom = Source & vbNullChar & vbNullChar
             .pTo = Destination & vbNullChar & vbNullChar
             .fFlags = shl_CopyStandard
          End With
          
          rcd = API_SHFileOperation(fop)
          flg = ((rcd Or fop.fAborted) = 0)
          
          FileCopyApi = flg
    End Function
    and this in a Form
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
     Dim Source As String
       Dim Destination As String
       
          Source = "C:\ChrisTest"
          Destination = "C:\AAChris"
          
          FileCopyApi Source, Destination
    End Sub
    that all

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  22. #22

    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 Chris

    I don't want to be awkward but I want to show successive filenames in my listbox. Not a progress bar.

    The latest code I posted works but is not recursive. That's all...
    Thanks all !

  23. #23
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

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

    Hi,

    here a sample with Listview, you have the option to select which file(s) are to be copied
    the current File copied is shown in a Textbox with a simple progressbar
    after copied it will show in a Listbox

    a few things there so pick what you need

    I will leave the Folder creation/checking to you

    Code:
    'Controls you need:
    '1x Listview
    '1x Picturebox
    '1x Listbox
    '1x Textbox
    '3x Commandbutton
    
    
    
    Option Explicit
    
    Private Sub Command1_Click()
    
       Dim Folder As String
       Dim sArray() As String
       Dim i As Long
          
          Folder = "C:\ChrisTest\"
          
          FileNamesFromFolder Folder, "*.*", sArray()
          ShowFolderInListView ListView1, Folder, sArray()
    End Sub
    
    Private Sub Command2_Click()
    Dim FolderFrom As String
    Dim FolderTo As String
    
    FolderFrom = "C:\ChrisTest\"
    FolderTo = "C:\AAChris\"
    
     Dim i As Long
          For i = ListView1.ListItems.Count To 1 Step -1
             If ListView1.ListItems(i).Checked Then
             Text1.Text = "copying : " & FolderFrom & ListView1.ListItems(i) 'copy in progress
                CopyFile FolderFrom & ListView1.ListItems(i), FolderTo & ListView1.ListItems(i)
                  List1.AddItem FolderFrom & ListView1.ListItems(i) & " copied !" 'after copy
             End If
          Next
    End Sub
    
    Private Sub Command3_Click()
    'select all Files to be copied
     Dim i As Long
          With ListView1
             For i = 1 To .ListItems.Count
                .ListItems(i).Checked = True
             Next
          End With
    End Sub
    
    
    Public Sub ShowFolderInListView(Lvw As ListView, Folder As String, sArray() As String)
       Dim i As Long, j As Long
       Dim Path As String
       Dim FileName As String
       Dim Li As ListItem
       
          Path = Folder
          If Right(Path, 1) <> "\" Then
             Path = Path & "\"
          End If
       
          With Lvw
             'Header ggf anlegen
             If .ColumnHeaders.Count = 0 Then
                .View = lvwReport
                .LabelEdit = lvwManual
                .HideSelection = False
                .Checkboxes = True
                .GridLines = True
                
                .ColumnHeaders.Add , , "Filename", 2500
                .ColumnHeaders.Add , , "LastUpdate", 1800
                .ColumnHeaders.Add , , "Lenght", 1200, vbRightJustify
             Else
                .ListItems.Clear
             End If
             
             For i = LBound(sArray) To UBound(sArray)
                Set Li = .ListItems.Add
                Li.Text = sArray(i)
                FileName = Path & Li.Text
                Li.SubItems(1) = Format(FileDateTime(FileName), "dd.mm.yyyy hh:nn:ss")
                Li.SubItems(2) = Format(FileLen(FileName) / 1024, "#,###") & " KB"
             Next
             Set Li = Nothing
          End With
    End Sub
    
    Public Function FileNamesFromFolder(Folder As String, _
                                        Pattern As String, _
                                        sArray() As String, _
                                        Optional Hidden As _
                                        Boolean = False) As Long
    'ein Verzeichnis auslesen, Beispiel
    'FileNamesFromFolder "c:\test", "*.bmp;*.jpg", s()
    
       Dim s As String, s1() As String
       Dim F As String
       Dim i As Long, j As Long
       Dim z As Long
       Dim p() As String
    
          ReDim sArray(0)
    
          'Pattern aufteilen auf Array, reine Endung
          p = Split(UCase(Replace(Pattern, "*.", "")), ";")
    
          F = Folder
          If Right(F, 1) <> "\" Then
             F = F & "\"
          End If
          'alle Dateien suchen
          If Hidden Then
             'auch versteckte
             s = Dir(F & "*.*", vbHidden)
          Else
             'keine versteckten
             s = Dir(F & "*.*")
          End If
          z = 0
          Do
             If Len(s) = 0 Then
                'durch
                Exit Do
             End If
             'keine Directorys
             If (GetAttr(F & s) And vbDirectory) _
                <> vbDirectory Then
                'keine Systemfiles
                If (s <> ".") And (s <> "..") Then
                   'Dateiendung isolieren
                   s1() = Split(UCase(s), ".")
                   If UBound(s1) > 0 Then
                      'alle vorgegebenen Pattern durchlaufen
                      For j = LBound(p) To UBound(p)
                         If (p(j) = "*") Or (p(j) = _
                                         s1(UBound(s1))) Then
                            'gefunden
                            ReDim Preserve sArray(z)
                            sArray(z) = s
                            z = z + 1
                            Exit For
                         End If
                      Next
                   End If
                End If
             End If
             s = Dir()
          Loop
          FileNamesFromFolder = z
    End Function
    
    Private Function CopyFile(QuellFile As String, ZielFile As String) As Boolean
    
       Dim QF As Integer
       Dim ZF As Integer
       Dim Percent As Double
       Dim CopyLen As Double
       Dim Copied As Double
       Dim AnzByte As Long
       Dim Msg As String
       Dim s As String
       
          'Block to Copy
          AnzByte = 256& * 256&
          
          On Error GoTo Fehler
          QF = FreeFile
          Msg = "Fehler beim Öffnen QuellFile"
          Open QuellFile For Binary As #QF
          CopyLen = LOF(QF)
          
          
          ZF = FreeFile
          Msg = "Fehler beim Öffnen ZielFile"
          Open ZielFile For Output As #ZF
          
          On Error GoTo 0
          
          Do
             If CopyLen = Copied Then
                Exit Do
             ElseIf (CopyLen - Copied) < AnzByte Then
                AnzByte = CopyLen - Copied
             End If
             s = Space(AnzByte)
             Get #QF, Copied + 1, s
             Print #ZF, s;
             Copied = Copied + AnzByte
             
             Picture1.Visible = True
             Percent = (Copied * 100) \ CopyLen
             ShowPercent Picture1, Percent
             DoEvents
          Loop
          
          Close #QF, #ZF
          CopyFile = True
          Picture1.Visible = False
          Exit Function
          
    Fehler:
          If ZF > 0 Then
             Close #QF
          End If
          Msg = Msg & vbCrLf & vbCrLf & "Fehler: " & _
                Err.Number & vbCrLf & Err.Description
          MsgBox Msg, vbCritical, "Fehler Copy"
          Err.Clear
    End Function
    
    Public Function ShowPercent(picBox As Control, ByVal Percent As Variant)
    'Fortschrittsanzeige
     
       Dim Wert As String
       Dim p As Long
       
          p = Int(CLng(Percent))
          picBox.AutoRedraw = True
          picBox.Cls 'clear
          picBox.ScaleWidth = 100
          picBox.DrawMode = 14
          Wert = Format(p, "###") & " %"
          ' Wert zentrieren:
          picBox.CurrentX = 50 - picBox.TextWidth(Wert) / 2
          picBox.CurrentY = (picBox.ScaleHeight - picBox.TextHeight(Wert)) / 2
          picBox.Print Wert
          ' Füllen:
          picBox.Line (-2, -2)-(Int(p), picBox.ScaleHeight), vbBlue, BF
          ' Aktualisieren:
          picBox.Refresh
    End Function
    
    
    Private Sub Form_Load()
    Command1.Caption = "Fill Listview"
    Command2.Caption = " copy selected"
    Command3.Caption = "select all"
    End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  24. #24
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

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

    Quote Originally Posted by el84 View Post
    Thanks Chris

    I don't want to be awkward but I want to show successive filenames in my listbox. Not a progress bar.
    So what's your point?
    Add FOF_SILENT and FOF_NOCONFIRMMKDIR to NOT get a progress-bar and to silently create any sub-dir's to the wFunc-Flag of SHFileOperation.
    If you call the API in a loop for each file, you can easily do
    1) Add FileName to ListBox ("MyFile.txt (Copy start...)")
    2) Call the API
    3) When API returns, check for Result=0, if 0 change ListBox-Entry to ("MyFile.txt (Copy successful)") else ("MyFile.txt (Copy failed)")
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  25. #25

    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

    Sorry Zvoni

    I don't understand what you mean! FOF_SILENT etc means nothing to me! Is there some modification I could make to the last code I posted in post #18, which did everything I wanted except the recursive bit?
    Thanks all !

  26. #26
    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

    If you want to list all the files in every subfolder you are going to copy, that means recursive navigation of subfolders and likely individual copying of files. Wouldn't copying the subfolders via FSO suffice? That should copy all their contents too, though I admit I am not an FSO user.

    If recursive navigation is what you want, search the forums for examples. Plenty about. I'm sure there are examples using FSO, APIs, and of course VB's own Dir() function. Regardless of the method to detect folders/files, the logic is the same.

    Last but not least, depending on user permissions, the user must have read permissions on source and write permissions on destination.
    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}

  27. #27

    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

    Yes, but I as you would possibly guess I need a bit more of a helping hand on what I need to do to my code in #18!
    Thanks all !

  28. #28
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

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

    LaVolpe,

    Correct!

    Collecting Files incl. Directory-Tree has been done for the umpteenth time here (incl. myself some 2-3 months ago using the mentioned API)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  29. #29
    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

    Quote Originally Posted by el84 View Post
    Yes, but I as you would possibly guess I need a bit more of a helping hand on what I need to do to my code in #18!
    I doubt many will rewrite recursion code for you, especially since there are many examples in this forum. This is something I think you should do yourself so you fully understand it. The logic is fairly simple.

    1. In your recursive routine, you pass in the folder to be processed. Pass this "ByVal"
    - that routine iterates through the folder, filtering out stuff you don't want and targeting stuff you do want
    - if a folder you want to process exists, you call this same routine, passing that folder

    2. Start the recusive routine by passing it the first folder to process

    3. You would not create/release an FSO object in that routine. Do it once outside the routine & clear when the routine finishes. Recursive routines should use as little resources and memory as possible to prevent stack overruns (out of memory)
    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}

  30. #30
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

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

    After reading up a bit on some stuff i still say CopyFileEx would be the smart way to achieve this.
    1) Using already built-in functions - no need to invent the wheel anew (and probably way faster than any FSO or self-invented function. Did i see that correct to open source and target in binary-mode and then to tranfer the bytes?)
    2) Through the callback you can show which file is processed, what's the status of the file (how many Bytes/KB/Percent is processed), show a progressbar (or not)
    3) Using the provided flags in a smart way, you can even implement stuff like "File exists - Overwrite/Rename?". Nevermind the Result-codes returned

    If someone is dead set to not use the API, then there is no pity from me.
    "Programming for Windows" is called that way for a reason......
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  31. #31

    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

    Quote Originally Posted by LaVolpe View Post
    I doubt many will rewrite recursion code for you, especially since there are many examples in this forum. This is something I think you should do yourself so you fully understand it. The logic is fairly simple.
    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!
    Thanks all !

  32. #32

    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

    Quote Originally Posted by LaVolpe View Post
    I doubt many will rewrite recursion code for you, especially since there are many examples in this forum. This is something I think you should do yourself so you fully understand it. The logic is fairly simple.
    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!
    Thanks all !

  33. #33
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

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

    El, when we give advice we never expect any kind of specific knowledge (or at least in some small part), but what we expect is to take the advice and to read up on it.
    You can't expect us to give you advice on how to drive on a dirt-road, when (at least in my case) the last I've driven there is some 20 years ago.
    Been on the free- and highways since.
    Frankly, to advise you how to achieve your goal with the FSO is for me beating a dead horse since i'll never use it personally (and probably would forget how to do it tomorrow).
    So the motivation for us is pretty low.
    What you said with my comment to use FOF_SILENT for the SHFileOperation-API:
    Just type SHFileOperation into google and read the MS-Docs to it. Everything is explained there
    That's what we (at least I) expect

    My 2 €-cent

    And now i'm off to my weekend.
    It's high time to jump out of a perfectly fine airplane again to get my head clear again for monday
    Last edited by Zvoni; Aug 10th, 2018 at 02:36 PM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  34. #34
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    264

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

    Following the advice in Post #2 (google for "vb6 shell and wait") gives you a nice wrapper function from stackoverflow, almost exactly what you were asking for (originally):
    https://stackoverflow.com/questions/...er-code-in-vb6
    That does the OpenProcess and WaitForSingleObject stuff for you.
    For more async handling, you can poll the OpenProcess handle using GetExitCodeProcess in a timer or similar loop.
    Last edited by ahenry; Aug 10th, 2018 at 03:25 PM. Reason: oops, I guess the thread has moved way past the original question

  35. #35

    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

    Sorry everyone. My 78 year old (and ex CPM) brain is not getting any of this. I have searched extensively on SHFileOperation, and am further confused by all the examples I have seen with comments such as 'it's not working'! Nowhere have I found out how to implement just the recursive copy into a program (not delete or any other manipulation).

    Unless some kind person can show me exactly how to implement this, with using technical terms, you'll have to give up on me! I appreciate all efforts.
    Thanks all !

  36. #36

    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

    Sorry everyone. My 78 year old (and ex CPM) brain is not getting any of this, as it is not couched in beginner's language (well, not for *this* beginner anyway). I have searched extensively on SHFileOperation, and am further confused by all the examples I have seen with comments such as 'it's not working'! Nowhere have I found out how to implement just the recursive copy into a program (not delete or any other manipulation).

    Unless some kind person can show me exactly how to implement this, without using technical terms, you'll have to give up on me! I appreciate all efforts.

    Not sure why I am getting this double posting! Sorry again!
    Thanks all !

  37. #37

    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

    http://vbnet.mvps.org/index.html?cod...hfileopadv.htm

    That (above) is exactly the sort of advice I was looking for BUT it is for vb.Net!
    Thanks all !

  38. #38

    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

    http://vbnet.mvps.org/index.html?cod...hfileopadv.htm

    That (above) is exactly the sort of advice I was looking for BUT it is for vb.Net!
    Thanks all !

  39. #39
    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

    Quote Originally Posted by el84 View Post
    http://vbnet.mvps.org/index.html?cod...hfileopadv.htm

    That (above) is exactly the sort of advice I was looking for BUT it is for vb.Net!
    Umm, that is VB code not .Net code. Yes, I'm 1000% sure. Also the SHFileOperation API was previously suggested but you rejected it.
    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}

  40. #40

    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

    Apologies. I just preferred to modify what I already understand (at least partly!) rather than try to learn something completely new. I'm trying now to change the all-purpose stuff in that link I gave to my specific requirement, when I know for sure I just want to copy recursively and will have specific filename for both the origin and the destination.
    Thanks all !

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
  •  



Click Here to Expand Forum to Full Width