Sign in to follow this  
Followers 0
cvocvo

Search output file and return a value?

13 posts in this topic

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

Share this post


Link to post
Share on other sites

Check the function StringRegExp in the help file.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

$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!"

Share this post


Link to post
Share on other sites

$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

Share this post


Link to post
Share on other sites

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!"

Share this post


Link to post
Share on other sites

RunWait(@ComSpec & " /c " & 'diskpart -s "@scriptdir & "\fixc.s"" > "@scriptdir & "\output.txt""', "", @SW_HIDE)

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 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!"

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by cvocvo

Share this post


Link to post
Share on other sites

#13 ·  Posted (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 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!"

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0