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 ?
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...
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
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...
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..
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.
Re: stderr / stdout: delete files if there's no error
Oops errr try it with out the brackets.....
Gremmy...
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
Ok -- i'm testing and Finilising the Function for ya..
Give me about 1/2 hour..
1 Attachment(s)
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 :blush: :blush:
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.. :thumb:
Enjoy
Gremmy
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 " ?
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....
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 :)
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....
Re: stderr / stdout: delete files if there's no error
Personally, I would find it better to just modify the Find_Files function to suit your needs. That way you can get just the files after "Extracting from" and none other. I would assume the lines with the files you need to delete all begin with "Extracting from", so you could just seek out those lines and get the filenames from there.
Also, Gremmy's method, while good, would not work correctly if they have a RAR file with a space in the file name. Worse if there is a space in the file path (i.e. any of the folders have a space in the name).
If the output maintains a standard (i.e. always says "Extracting from <archive name>" when it gets a RAR file to extract and says "Extracting <file name>" when it extracts a file inside), you could use a much simpler method:
Code:
Public Sub Find_Files(ByVal Input_string As String, ByRef File_array() As String)
Dim Tmp_Array() As String
Dim Tmp_Loop 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 = 0 To UBound(Tmp_Array)
If Left$(Tmp_Array(Tmp_Loop), 15) = "Extracting from" Then
ReDim Preserve File_array(UBound(File_array) + 1)
File_array(UBound(File_array)) = Right$(Tmp_Array(Tmp_Loop), Len(Tmp_Array(Tmp_Loop)) - 16) '16 is the length of "Extracting from "
End If
Next
End Sub
That should work (you'll still get the first item in the passed array being blank).
Re: stderr / stdout: delete files if there's no error
Haha Snap Chaos...
Good Comments..
I am still to find a method to use that first item in the array...
Dont know why they made it that ubound wont return -1 for undimmed arrays... and why Redim Array (-1) errors
Grem....
Re: stderr / stdout: delete files if there's no error