engjcowi Posted November 5, 2010 Share Posted November 5, 2010 Hi Guys Im trying to use the command line defrag and i cant seem to get it to read the percentage its upto. I use this command just to check if it needs defragging. It just wont seem to show the information. On windows 7 it currently doesnt show anything. it has once or twice started then froze and crashed. I just want it to tell me what percentage the analysis is upto. Can you guys help? thanks $DefragPath = " /c defrag.exe " & "c: /A /U" $drun = Run(@ComSpec & $DefragPath, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) While 1 $line = StderrRead($drun) If @error Then ExitLoop $message = "" $message = $line TrayTip("Percentage",$line, 25, 1) Wend StdioClose($line) Drunken Frat-Boy Monkey Garbage Link to comment Share on other sites More sharing options...
GEOSoft Posted November 5, 2010 Share Posted November 5, 2010 For openers /U is not a valid switch for defrag in all operating systems. You might have meant /v which is useless for what you want. This works $sStdOut = "" $sCmd = "defrag c: -a" $iFrag = 0 $hPid = Run(@ComSpec & " /c " & $sCmd, "", @SW_HIDE, 0x8) While 1 $sStdOut = StdoutRead($hPid) If @error Then ExitLoop $iFrag = StringRegExpReplace(StringStripWS($sStdOut, 8), "(?m:^|\n)(?i).+file\D+(\d+)\h*%.*", "$1") If @Extended Then ExitLoop Wend MsgBox(0, "TEST", $sStdOut & @CRLF & "Fragmentation: " & $iFrag & "%"); Use your TrayTip here instead of the MsgBox() StdioClose($hPid) George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
engjcowi Posted November 5, 2010 Author Share Posted November 5, 2010 Hi Geosoft i want to thank you for your reply. Youve helped me alot on this forum and i want to thank you very much for that. Thank you again for you fast response here too however your example doesnt work for me. The -a switch is for xp users and windows 7 has a /a switch which i can alter no probelem. I did this on your code and at the end it saif "fragmentation: %" and that was all. Windows 7 uses /a and /u switch. thsi responds by telling the user whether to defragment the volume or not but before that process it gives some simple info "analysis xx%" and this is what im trying to capture and update as it updates in the command window. Ive included 2 screenshots below. 1 of the command line switches and one for the percentage im trying to capture and update to let the user know where its upto. Thanks Jamie Drunken Frat-Boy Monkey Garbage Link to comment Share on other sites More sharing options...
GEOSoft Posted November 5, 2010 Share Posted November 5, 2010 just change the -a to /a. That will make it compatable even with XP. That was part of the MS plan to drop "/" in favor of "-" which seems to have been forgotten. The /u switch is only valid in windows 7. I'll be booting into that OS later today and I can come up with a new function for you but remember that it will only work in Win 7 George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
engjcowi Posted November 5, 2010 Author Share Posted November 5, 2010 thanks Drunken Frat-Boy Monkey Garbage Link to comment Share on other sites More sharing options...
GEOSoft Posted November 6, 2010 Share Posted November 6, 2010 I finally got around to booting into Win 7. The problem you are having (besides the wrongstdio call)is related to the speed at which Windows updates the Std out stream. This works in Windows 7 but as you can see it picks up way too late and skips much in between. Now bear in mind that I'm running it on a 3Gb quad-core processor. Also the processor usage is sky high and if I introduce a sleep to pull down the CPU usage then it makes the refresh problem even worse. $sFile = @DesktopDir & "\result.txt" FileDelete($sFile) $DefragPath = " /c defrag " & "d: /a /u" $drun = Run(@ComSpec & $DefragPath, @SystemDir, @SW_HIDE, 0x8) $message = "" $sOut = "" $line = "" While 1 $line = StdoutRead($drun) If @error Then ExitLoop $message = StringRegExpReplace($line, "(?i)(?s).+Analysis:\h*(\d+%).*complete.*", "$1") If @Extended Then If $sOut <> $message Then $sOut = $message FileWrite($sFile, ">> " & $sOut & @CRLF) ;TrayTip("Percentage",$sOut, 25, 1) EndIf EndIf Wend StdioClose($drun) The refresh problem is also affected by the size of the drive. I ran it against 3 different drives, all with unsatisfactory results where the original pickup point was anything from 60% to 98%. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
engjcowi Posted November 7, 2010 Author Share Posted November 7, 2010 Thanks for trying. For now ill just have to leave a message up saying that its working and may take a couple of minutes. I was looking last night about using the windows api to do it direct however it seems a little out of my range of skills. There also only seems to be 1 example of anyone doing it and its with c#. Ive posted both links i was looking at http://msdn.microsoft.com/en-us/library/aa363911(VS.85).aspx#defragmenting_a_file http://blogs.msdn.com/b/jeffrey_wall/archive/2004/09/13/229137.aspx If i get anywhere ill post here and let you know Drunken Frat-Boy Monkey Garbage Link to comment Share on other sites More sharing options...
DOTCOMmunications Posted November 8, 2010 Share Posted November 8, 2010 Hi all You could try the following for windows 7 (obviously needs admin permissions to run): #RequireAdmin $DefragPath = " /c defrag.exe C: /a /h /u" $pid = Run(@ComSpec & $DefragPath, @SystemDir, @SW_HIDE, 2); Stdout Global $data Do $data &= StdOutRead($pid) Until @error $file = FileOpen("result.txt", 2) FileWrite($file, $data) FileClose($file) sleep(2000) Exit This will produce a text file called result.txt which has the data inside. From my machine this produced the percentages shown in the attached text documentresult.txt Link to comment Share on other sites More sharing options...
GEOSoft Posted November 8, 2010 Share Posted November 8, 2010 That is precisly the output of the StdOutRead() and therein lies his problem. he wants the completion percentages to update a traytip in real time. If you watch that actually run using CMD, the percentage completion is not strung out that way, instead it just updates the one line. Because of the speed at which it runs, it is going to be very difficult, if not impossible, to use TrayTip() with it. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
engjcowi Posted November 8, 2010 Author Share Posted November 8, 2010 Traytip was just a suggetsion. To be honest i just wanted the user to see what was going on as it runs for a while and looks as if nothing is going on to the average user. I have a edit box in my prog i use as a command console to show whats going on. Ill try the above examples with it and see what happens in it. I was just thinking it would be good for the user and myself to see what happening nd give a rough indication of where upto. thanks jamie Drunken Frat-Boy Monkey Garbage Link to comment Share on other sites More sharing options...
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