TheCrimsonCrusader Posted October 26, 2018 Posted October 26, 2018 Greetings, I am looking to see if anyone has suggestions on more intelligent code than what I have below, particular in regards to the overuse of the loops. What I am ultimately trying to do here is when RoobCopy dumps out the percentage complete in a log file, it's using a format similar to below, which isn't easy to parse: 0.4% 0.4% 0.4% 0.4% 0.4% 0.4% There will be several repeated values it logs during the copy operation, but will in increase. So what I am doing is doing a string search for 1.0, 2.0, 3.0, etc. and taking that and writing to a separate file where it will replace the previous percentage on the exact same line. What I will ultimately be doing here is creating a progress bad based on the current percentage value based on whether it is 1.0, 2.0, 3.0, etc. I'm not worried about the RoboCopy aspect, nor the progress bar aspect. I am just trying to have code that is less "ghetto" than I have below where it is using separate loop statements for each percentage point because if I put all of the percentage checks in the same loop statement, it's just going to loop over to reading the lower percentage points again, which when I do implement the progress percentage, it's going to bounce back and forth. Any help would be greatly appreciated and my apologies if what I am asking about is unclear. expandcollapse popup#include <File.au3> FileDelete("d:\Progress.txt") While 1 $file = FileOpen("d:\test.txt", 0) $read = FileRead($file) If @error = -1 Then MsgBox(0, "Error", "File not read") Exit Else If StringRegExp($read, "1.0") Then $1 = "1" FileClose($file) ExitLoop EndIf Wend While 1 $file = FileOpen("d:\test.txt", 0) $read = FileRead($file) If @error = -1 Then MsgBox(0, "Error", "File not read") Exit Else If StringRegExp($read, "2.0") Then $2 = "2" FileClose($file) ExitLoop EndIf Wend While 1 $file = FileOpen("d:\test.txt", 0) $read = FileRead($file) If @error = -1 Then MsgBox(0, "Error", "File not read") Exit Else If StringRegExp($read, "3.0") Then $3 = "3" FileClose($file) ExitLoop EndIf Wend If $1 = "1" Then FileWriteLine("d:\Progress.txt","1") If $2 = "2" Then _FileWriteToLine("d:\Progress.txt", 1, "2", True) If $3 = "3" Then _FileWriteToLine("d:\Progress.txt", 1, "3", True)
FrancescoDiMuro Posted October 26, 2018 Posted October 26, 2018 2 hours ago, TheCrimsonCrusader said: 0.4% 0.4% 0.4% 0.4% 0.4% 0.4% So this is the RoboCopy log file, in which are stored are the various percents about the copy of different files. What are you trying to achieve, is read these percents and write them to another file? In your code, where do you read those percents? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Moderators JLogan3o13 Posted October 26, 2018 Moderators Posted October 26, 2018 I guess I am confused as well, can you please post an example of the progress.txt? I would think you should be able to do something like this to consolidate (unless I am misunderstanding your intentions): #include <File.au3> FileDelete("d:\Progress.txt") While 1 $file = FileOpen("d:\test.txt", 0) $read = FileRead($file) If @error = -1 Then MsgBox(0, "Error", "File not read") Exit Else If StringRegExp($read, "1.0") Then FileWriteLine("d:\Progress.txt","1") ElseIf StringRegExp($read, "2.0") Then _FileWriteToLine("d:\Progress.txt", 1, "2", True) ElseIf StringRegExp($read, "3.0") Then _FileWriteToLine("d:\Progress.txt", 1, "3", True) EndIf FileClose($file) ExitLoop EndIf Wend "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
careca Posted October 26, 2018 Posted October 26, 2018 There's the possibility to remove white spaces and then stringsplit by % 0.4%0.4%0.4%0.4%0.4%0.4% Then sort the array and the highest number (the top one) is the percentage you're looking for. What does robocopy do that autoit cant? You could do it all in autoit, and then the percentage is easier to get. Spoiler Renamer - Rename files and folders, remove portions of text from the filename etc. GPO Tool - Export/Import Group policy settings. MirrorDir - Synchronize/Backup/Mirror Folders BeatsPlayer - Music player. Params Tool - Right click an exe to see it's parameters or execute them. String Trigger - Triggers pasting text or applications or internet links on specific strings. Inconspicuous - Hide files in plain sight, not fully encrypted. Regedit Control - Registry browsing history, quickly jump into any saved key. Time4Shutdown - Write the time for shutdown in minutes. Power Profiles Tool - Set a profile as active, delete, duplicate, export and import. Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes. NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s. IUIAutomation - Topic with framework and examples Au3Record.exe
TheCrimsonCrusader Posted October 26, 2018 Author Posted October 26, 2018 LOL Yeah, I figured I failed to explain it well. Due to the horrible formatting layout of the percentages from the RoboCopy log file, I am basically running a loop where it's checking for 1.0. 2.0, 3.0, etc. in that log file. So when it sees a 1.0, it writes a "1" to a new file. Then when it sees a 2.0, it then overwrites that first line number that had a 1 with a 2 and rinse and repeat. So what I will effectively be doing is, reading from that new file that has the first line overwritten and then having that dictate the progress bar (when I add that code) so when it reads that file in the loop, if it sees a 1, it then changes the progress bar to 1%. I'm not sure if that clarifies anything. :-)
TheCrimsonCrusader Posted October 26, 2018 Author Posted October 26, 2018 1 minute ago, careca said: There's the possibility to remove white spaces and then stringsplit by % 0.4%0.4%0.4%0.4%0.4%0.4% Then sort the array and the highest number (the top one) is the percentage you're looking for. What does robocopy do that autoit cant? You could do it all in autoit, and then the percentage is easier to get. I will be performing retry attempts, bandwidth throttling in certain instances and other things of that nature. It's not going to be mere file copy unfortunately.
FrancescoDiMuro Posted October 26, 2018 Posted October 26, 2018 (edited) @TheCrimsonCrusader Could you please upload a part or the file you're talking about? So, how many percents do you have to track? Edited October 27, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
mikell Posted October 27, 2018 Posted October 27, 2018 (edited) Using the robocopy option "\tee" you can output to console instead of a log, example below This way allows formatting but I don't know if this will be of help ... ... or just something to play with #include <AutoItConstants.au3> $source = "C:\Program Files\AutoIt3\Include" $dest = @desktopdir & "\test" Local $iPID = Run('robocopy.exe "' & $source & '" "' & $dest & '" /tee' , @SystemDir, @SW_HIDE, $STDOUT_CHILD) Local $sOutput = "", $log = "" While 1 $s = StdoutRead($iPID) If @error Then ExitLoop If StringRegExp($s, "\S") Then $log &= $s If StringRegExp($s, "[\h\d.]+%") Then $sOutput &= Number($s) & @crlf WEnd Filewrite("robo.txt", $sOutput) Filewrite("log.txt", $log) Edited October 27, 2018 by mikell
mikell Posted October 27, 2018 Posted October 27, 2018 Well, finally did it. It's rainy today #include <AutoItConstants.au3> $source = "C:\Program Files\AutoIt3" $dest = @desktopdir & "\test" $file = "AutoIt.chm" ProgressOn("Progress", "File", "0%") Local $iPID = Run('robocopy.exe "' & $source & '" "' & $dest & '" "' & $file & '" /njh /njs /ipg:100 /tee' , @SystemDir, @SW_HIDE, $STDOUT_CHILD) Local $sOutput = "", $log = "" While 1 $s = StdoutRead($iPID) If @error Then ExitLoop If StringRegExp($s, "\S") Then $log &= $s If StringRegExp($s, "(?m)^[\h\d.]+%") Then $sOutput &= Number($s) & @crlf ProgressSet(Number($s), $file, Number($s) & " %") EndIf WEnd ProgressSet(100, $file, "Complete") Filewrite("robo.txt", $sOutput) Filewrite("log.txt", $log) Sleep(2000) ProgressOff()
TheCrimsonCrusader Posted October 29, 2018 Author Posted October 29, 2018 On 10/27/2018 at 10:53 AM, mikell said: Well, finally did it. It's rainy today #include <AutoItConstants.au3> $source = "C:\Program Files\AutoIt3" $dest = @desktopdir & "\test" $file = "AutoIt.chm" ProgressOn("Progress", "File", "0%") Local $iPID = Run('robocopy.exe "' & $source & '" "' & $dest & '" "' & $file & '" /njh /njs /ipg:100 /tee' , @SystemDir, @SW_HIDE, $STDOUT_CHILD) Local $sOutput = "", $log = "" While 1 $s = StdoutRead($iPID) If @error Then ExitLoop If StringRegExp($s, "\S") Then $log &= $s If StringRegExp($s, "(?m)^[\h\d.]+%") Then $sOutput &= Number($s) & @crlf ProgressSet(Number($s), $file, Number($s) & " %") EndIf WEnd ProgressSet(100, $file, "Complete") Filewrite("robo.txt", $sOutput) Filewrite("log.txt", $log) Sleep(2000) ProgressOff() Thanks for all of the responses. mikell's code works perfectly so hats off to you sir! I had a way to do it , but it was going to be a lot of code that I knew wouldn't be necessary for those more adept than I.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now