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:
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.
Re: stderr / stdout: delete files if there's no error
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...)
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
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.
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
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
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.