CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Dec 2005
    Posts
    151

    stderr / stdout: delete files if there's no error

    Hi, I'm using the code below to wite the output of a commandline tool to STDOut and STDErr to find out if an error occured. What I'd like to do is remove the files shown in the STDOut if no error occured in STDErr.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    
    Dim wsh As IWshRuntimeLibrary.WshShell
    Dim wshExec As IWshRuntimeLibrary.wshExec
    Dim STDErrResults As String
    Dim STDOutResults As String
    Set wsh = New WshShell
    Set wshExec = wsh.Exec("C:\unrar.exe x " & "D:\Archives\My.Video.part1.rar" & " " & "D:\")
    STDOutResults = wshExec.StdOut.ReadAll
    STDErrResults = wshExec.StdErr.ReadAll
    
    If STDErrResults = "" Then
    'Delete all the files shown in STDOutResults
    End If
    
    End Sub
    STDOut: shows the entire process
    STDErr: shows the error that occured (shows nothing if there was no error)

    This is what gets written to STDOut:

    Code:
    UNRAR 3.50 freeware      Copyright (c) 1993-2005 Alexander Roshal
    
    
    Extracting from D:\Archives\My.Video.part1.rar
    
    Extracting  D:\My.Video.avi                                              
    
    Extracting from D:\Archives\My.Video.part2.rar
    
    ...         My.Video.avi                                                 
    
    Extracting from D:\Archives\My.Video.part3.rar
    
    ...         My.Video.avi                                                 
    
    Extracting from D:\Archives\My.Video.part4.rar
    
    ...         My.Video.avi                                                 
    
    Extracting from D:\Archives\My.Video.part5.rar
    
    ...         My.Video.avi                                                 
    
    Extracting from D:\Archives\My.Video.part6.rar
    
    ...         My.Video.avi                                                 
    
    Extracting from D:\Archives\My.Video.part7.rar
    
    ...         My.Video.avi                                                 
    
    Extracting from D:\Archives\My.Video.part8.rar
    
    ...         My.Video.avi                                                   OK 
    All OK
    If no error occured, then I want to delete all these files:

    D:\Archives\My.Video.part1.rar
    D:\Archives\My.Video.part2.rar
    D:\Archives\My.Video.part3.rar
    D:\Archives\My.Video.part4.rar
    D:\Archives\My.Video.part5.rar
    D:\Archives\My.Video.part6.rar
    D:\Archives\My.Video.part7.rar
    D:\Archives\My.Video.part8.rar

    Is there a way to read all these files from STDOut and delete them ?

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    You can write a function to return all the filenames in the STDout string, and then use Kill to delete the files...

    If you need any pointers on how to write the function Let me know..
    I'll look through my box of trick to see if i can find something...

    Tip: use the ':\' to identify the file path and name....

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Dec 2005
    Posts
    151

    Re: stderr / stdout: delete files if there's no error

    Thanks GremlinSA, if you could give me some more information about how to write the function, then that would be great.

    I tried to put the contents of STDOut to a multiline textbox, filter out all the filepaths, but I couldn't get it to work. I'm also quite sure it can be done without using an invisible textbox.

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    Quote Originally Posted by chaden
    Thanks GremlinSA, if you could give me some more information about how to write the function, then that would be great.

    I tried to put the contents of STDOut to a multiline textbox, filter out all the filepaths, but I couldn't get it to work. I'm also quite sure it can be done without using an invisible textbox.
    The following Code is written mostly as a guide and my not work 100% but here goes...
    Code:
    Public Sub Find_Files(ByVal Input_string As String, ByRef File_array() As String)
    Dim Tmp_Array() As String
    Dim Tmp_words() As String
    Dim Tmp_Loop_1 As Long
    Dim Tmp_Loop_2 As Long
    ReDim File_array(0)
    Tmp_Array = Split(Input_string, vbCrLf, -1, vbTextCompare) 'Split each line into it's own string line..
    For Tmp_Loop_1 = 0 To UBound(Tmp_Array)
        Tmp_words = Split(Tmp_Array(Tmp_Loop_1), " ", -1, vbTextCompare) ' Split each line into its words
        For Tmp_Loop_2 = 0 To UBound(Tmp_words)
            If InStr(1, Tmp_words(Tmp_Loop_2), ":/", vbTextCompare) Then
                ReDim Preserve File_array(UBound(File_array) + 1)
                File_array(UBound(File_array)) = Tmp_words(Tmp_Loop_2)
            End If
        Next
    Next
    End Sub
    This funtion takes in the string and returns a String Array containing the File names...

    The Split function could have been used to split it straight into words but this poses a small problem with VbCrLf, when the Lines do not start with and end with a space..

    this should work (Written on the fly and untested...)

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Dec 2005
    Posts
    151

    Re: stderr / stdout: delete files if there's no error

    Thank you. This what I have now (with your function), but if gives me an "Argument not optional" message with Find_Files (STDOutResults). What am I doing wrong ?


    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    
    Dim wsh As IWshRuntimeLibrary.WshShell
    Dim wshExec As IWshRuntimeLibrary.wshExec
    Dim STDErrResults As String
    Dim STDOutResults As String
    Set wsh = New WshShell
    Set wshExec = wsh.Exec("C:\unrar.exe x " & "D:\Archives\My.Video.part1.rar" & " " & "D:\")
    STDOutResults = wshExec.StdOut.ReadAll
    STDErrResults = wshExec.StdErr.ReadAll
    
    If STDErrResults = "" Then
      Find_Files (STDOutResults)
      Kill Find_Files
    End If
    
    End Sub

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    you need to use the function as follows...
    Code:
    Dim File_results() As string
    Dim Loop1 As Long
    
    If STDErrResults = "" Then
        Find_Files STDOutResults, File_results
        for Loop1 = 1 to ubound(file_results)
            Kill file_results(loop1)
        Next Loop1
    End If
    Hope this helps...

    Gremmy..
    Last edited by GremlinSA; January 31st, 2006 at 02:39 PM.
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    Dec 2005
    Posts
    151

    Re: stderr / stdout: delete files if there's no error

    Thank you, but unfortunately I'm getting a compile error with: Find_Files (STDOutResults, File_results)

    Compile error: Expected: =

    And when I add "=", it needs an expression.

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    Oops errr try it with out the brackets.....

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  9. #9
    Join Date
    Dec 2005
    Posts
    151

    Re: stderr / stdout: delete files if there's no error

    Without the brackets there are no errors anymore, but unfortunately nothing happens.

    I also tested it with a multiline textbox (instead of STDOutResults) and pasted the text in there, but nothing happens either.

    Code:
    Private Sub Command1_Click()
    Dim File_results() As String
    Dim Loop1 As Long
    
        Find_Files Text1.Text, File_results
        For Loop1 = 1 To UBound(File_results)
            Kill File_results(Loop1)
        Next Loop1
    
    End Sub

  10. #10
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    Ok -- i'm testing and Finilising the Function for ya..

    Give me about 1/2 hour..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  11. #11
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    Quote Originally Posted by GremlinSA
    Ok -- i'm testing and Finilising the Function for ya..

    Give me about 1/2 hour..
    Goodnes.. I'm loosing it ... the error was this simple
    Code:
    If InStr(1, Tmp_words(Tmp_Loop_2), ":/", vbTextCompare) Then
    is suposed to look like this
    Code:
    If InStr(1, Tmp_words(Tmp_Loop_2), ":\",  vbTextCompare) Then
    and the function jumped to life... but anyways heres a demo of the function..

    Enjoy

    Gremmy
    Attached Files Attached Files
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  12. #12
    Join Date
    Dec 2005
    Posts
    151

    Re: stderr / stdout: delete files if there's no error

    That works perfectly. Thank you very much

    Only one small thing (sorry to be such a pain in the a$$)

    It also lists D:\My.Video.avi, which will be deleted then.
    I need to keep the file(s) that get extracted from the rar archives. Only the rar archives need to be deleted, because they aren't needed anymore after extraction.

    People will kill me if the contents of the rar archives get deleted as well

    D:\Archives\My.Video.part1.rar
    D:\My.Video.avi
    D:\Archives\My.Video.part2.rar
    D:\Archives\My.Video.part3.rar
    D:\Archives\My.Video.part4.rar
    D:\Archives\My.Video.part5.rar
    D:\Archives\My.Video.part6.rar
    D:\Archives\My.Video.part7.rar
    D:\Archives\My.Video.part8.rar

    Is it possible to get only the paths after "Extracting from " ?
    Last edited by chaden; January 31st, 2006 at 04:53 PM.

  13. #13
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    You could add a little checking before killing the file.. somthing like
    Code:
        For Loop1 = 1 To UBound(File_results)
            If UCase(Right(Trim(File_results(Loop1)) ,3)) = "RAR" Then
                Kill File_results(Loop1)
            End If
        Next Loop1
    should work for you...

    Gremmy....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  14. #14
    Join Date
    Dec 2005
    Posts
    151

    Re: stderr / stdout: delete files if there's no error

    That works fine.

    It will only be a problem if there are rar archives in the rar archives, but I'll find something out for that.

    Thank you very much for your help

  15. #15
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: stderr / stdout: delete files if there's no error

    for this we could also add
    Code:
    If InStr(1, Tmp_words(Tmp_Loop_2), ":\", vbTextCompare)  AND InStr(1, Tmp_Array(Tmp_Loop_1), "from", vbTextCompare) Then
    and it will only pickup filenames that have the word "from" in the same sentance..

    Gremmy....
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

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

Featured