cvocvo Posted June 13, 2010 Posted June 13, 2010 I am working on writing a program to interface with diskpart. I've gotten diskpart to output to a text file but instead of just outputting the number like I want, it outputs this: Microsoft DiskPart version 6.1.7600 Copyright (C) 1999-2008 Microsoft Corporation. On computer: C762NR Volume 3 is the selected volume. The maximum number of reclaimable bytes is: 42 GB (43044 MB) Leaving DiskPart... How would I go about writing something that would parse that output.txt file for (in this case) 43044 MB? I only want it to return the value 43044, but this number could be anything depending on the drive that it is run on. Thanks for any advice/code snippets. Chris
Thatsgreat2345 Posted June 13, 2010 Posted June 13, 2010 Check the function StringRegExp in the help file.
cvocvo Posted June 13, 2010 Author Posted June 13, 2010 That's what I've looked at before, but I'm confused as to how I can use it with the output.txt file. All the examples are using arrays.
Thatsgreat2345 Posted June 13, 2010 Posted June 13, 2010 look online at examples for how to construct regular expressions, it takes a little while to get used to but trust me it is something you really need to learn and will come in handy in the future.
cvocvo Posted June 13, 2010 Author Posted June 13, 2010 Ok, can do. I searched a bit and think I found my answer within this post: http://www.autoitscript.com/forum/index.php?showtopic=115506&view=findpost&p=807180 Here's the breakdown for anyone else that may wonder this. Use FileOpen and set it = to a variable Use FileReadLine to read each line of the file, set = variable Use StringRegExp to parse the line variable Loop it all until whatever parameter you have set. Thanks
Thatsgreat2345 Posted June 13, 2010 Posted June 13, 2010 I'm actually really bad with regular expressions haha but no need to have to loop or anything#include <Array.au3> $output = FileRead('output.txt'); $reg = StringRegExp($output,'\((\d+)\sMB\)',3) _ArrayDisplay($reg)
GEOSoft Posted June 13, 2010 Posted June 13, 2010 $sNum = StringRegExpReplace(FileRead("output.txt"), "(?i)(?s).+reclaim.+?\((\d+).+", "$1") It can be made even simpler if you post that part of the code where you call and get the return of diskpart. No need to write to a file at all if that's the only info you want from diskpart. 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!"
cvocvo Posted June 13, 2010 Author Posted June 13, 2010 $sNum = StringRegExpReplace(FileRead("output.txt"), "(?i)(?s).+reclaim.+?\((\d+).+", "$1") It can be made even simpler if you post that part of the code where you call and get the return of diskpart. No need to write to a file at all if that's the only info you want from diskpart. The only reason I was writing the diskpart output to a file was because that's the only way I know to have diskpart run a command and have autoit get its answer. Do you know how to interface directly with diskpart from autoit so that I don't have to write that file? Right now I'm just using comspec to call diskpart /s script.s > output.txt Here's what the script.s looks like: select volume=c shrink querymax exit If you know a better way to interface with diskpart through an API or something I would be very interested to know how as well. Thanks
GEOSoft Posted June 13, 2010 Posted June 13, 2010 Sure but post the actual section of the code that contains the Run(@Comspec part so I can see what flags you have set. 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!"
cvocvo Posted June 13, 2010 Author Posted June 13, 2010 RunWait(@ComSpec & " /c " & 'diskpart -s "@scriptdir & "\fixc.s"" > "@scriptdir & "\output.txt""', "", @SW_HIDE)
GEOSoft Posted June 13, 2010 Posted June 13, 2010 (edited) Tested on my system which has a different return to yours but I made allowances for that and it worked either way.$sScript = "c:\script.txt"; I changed the extension of your file and moved it to the root for easier testing. $hRun = Run(@ComSpec & " /c diskpart /S " & $sScript, "", @SW_HIDE, 0x02) $sNum = "" $sOut = "" While 1 $sOut &= StdoutRead($hRun) If @Error Then ExitLoop $sNum = StringRegExpReplace($sOut, "(?i)(?s).+reclaim.+?(\d+)\s[mkb][by].+", "$1") If @Extended Then ExitLoop WEnd StdIoClose($hRun) MsgBox(0, "Test result", $sNum)If you want to play with the RegExp then try the PCRE Toolkit in my signature. It allows you to test the expression against a command StdIO stream. Edited June 13, 2010 by GEOSoft 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!"
cvocvo Posted June 13, 2010 Author Posted June 13, 2010 (edited) I've tried to implement that code, but it's jumping from to the MsgBox before the diskpart script has actually finished running. It takes a while for the 'Shrink QueryMax' function to run. Any idea how to get it to wait? Edit: Nevermind, I tested your code by itself and it works great. I just need to get it worked into mine. Thanks Edit 2: Ok, I've been trying a bunch of ways, but I can't get diskpart to use a relative location for the script. How would I solve this? This is the code: ;$sScript = @scriptdir & \shrinkquerymax.s ;I commented this out because I was trying to directly put it in the @ComSpec call, but neither way worked. I would prefer to have it here in this variable. $hRun = Run(@ComSpec & " /c diskpart /S " & '@scriptdir & "\shrinkquerymax.s"', "", @SW_HIDE, 0x02) $sNum = "" $sOut = "" While 1 $sOut &= StdoutRead($hRun) If @Error Then ExitLoop $sNum = StringRegExpReplace($sOut, "(?i)(?s).+reclaim.+?(\d+)\s[mkb][by].+", "$1") If @Extended Then ExitLoop WEnd StdIoClose($hRun) MsgBox(0, "Test result", $sNum) Edit 3: Ok so I couldn't get it to work with @scriptdir & "\shrinkquerymax.s", but what I did do was put each of those in variable and then add those variables. After they're added I just point the ComSpec to use that variable. $folder = @ScriptDir $file = "\shrinkquerymax.s" $fullpath = $folder & $file $hRun = Run(@ComSpec & " /c diskpart /S " & $fullpath, "", @SW_HIDE, 0x02) $sNum = "" $sOut = "" While 1 $sOut &= StdoutRead($hRun) If @Error Then ExitLoop $sNum = StringRegExpReplace($sOut, "(?i)(?s).+reclaim.+?(\d+)\s[mkb][by].+", "$1") If @Extended Then ExitLoop WEnd StdIoClose($hRun) MsgBox(0, "Test result", $sNum) Might be an easier/cleaner way to do this, but it works. Edited June 14, 2010 by cvocvo
GEOSoft Posted June 14, 2010 Posted June 14, 2010 (edited) I've tried to implement that code, but it's jumping from to the MsgBox before the diskpart script has actually finished running. It takes a while for the 'Shrink QueryMax' function to run. Any idea how to get it to wait? Edit: Nevermind, I tested your code by itself and it works great. I just need to get it worked into mine. Thanks Edit 2: Ok, I've been trying a bunch of ways, but I can't get diskpart to use a relative location for the script. How would I solve this? This is the code: ;$sScript = @scriptdir & \shrinkquerymax.s ;I commented this out because I was trying to directly put it in the @ComSpec call, but neither way worked. I would prefer to have it here in this variable. $hRun = Run(@ComSpec & " /c diskpart /S " & '@scriptdir & "\shrinkquerymax.s"', "", @SW_HIDE, 0x02) $sNum = "" $sOut = "" While 1 $sOut &= StdoutRead($hRun) If @Error Then ExitLoop $sNum = StringRegExpReplace($sOut, "(?i)(?s).+reclaim.+?(\d+)\s[mkb][by].+", "$1") If @Extended Then ExitLoop WEnd StdIoClose($hRun) MsgBox(0, "Test result", $sNum) Try this $sScript = Chr(34) & @scriptdir & \shrinkquerymax.s & Chr(34) $hRun = Run(@ComSpec & " /c diskpart /S " & $sScript, "", @SW_HIDE, 0x02) EDIT. I suggested another method in my reply to the PM so if this fails try that one. Edited June 14, 2010 by GEOSoft 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!"
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