Jump to content

Trying to read specific text from file


Recommended Posts

  • Moderators

I have been working on a script to use in conjunction with a command line defragmenting tool. The tool analyzes the drive(s), then spits out a report. I'd like to have the script read the number of fragmented items, and then take some action thereafter. The problem I am having is getting the script to read only what I want out of the log file. I've been playing around with FileRead and FileReadLine, but am not coming up with what I need. See below for an example of the log file by line:

1-12 omiited....

13. <blank>

14. Unfragmented items: 26,756,861,952 bytes 112,645 items

15. Fragmented items: 6,966,956,032 bytes 2,677 items

I would like to read line 14, but only the number of unfragmented bytes (26,756,861,952), and then use ClipPut to use this info late in the script. I would then read the fragmented bytes in line 15, and do the same. Can anyone offer any suggestions, or let me know that this is not feasible? Thanks.

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

Link to comment
Share on other sites

I have been working on a script to use in conjunction with a command line defragmenting tool. The tool analyzes the drive(s), then spits out a report. I'd like to have the script read the number of fragmented items, and then take some action thereafter. The problem I am having is getting the script to read only what I want out of the log file. I've been playing around with FileRead and FileReadLine, but am not coming up with what I need. See below for an example of the log file by line:

1-12 omiited....

13. <blank>

14. Unfragmented items: 26,756,861,952 bytes 112,645 items

15. Fragmented items: 6,966,956,032 bytes 2,677 items

I would like to read line 14, but only the number of unfragmented bytes (26,756,861,952), and then use ClipPut to use this info late in the script. I would then read the fragmented bytes in line 15, and do the same. Can anyone offer any suggestions, or let me know that this is not feasible? Thanks.

If "unfragmented" and "fragmented" only appear once in the log, you can real the whole file to a variable, split it by lines, then loop through the array of lines. In the loop, if the line contains "unfragmented", use text manipulation to pull out the bytes and set a variable equal to it. Use a different variable for "fragmented".

Look at fileread, stringsplit, for...in...next, stringinstr

Link to comment
Share on other sites

  • Moderators

JLogan3o13,

Something along these lines should work: :(

$sFile = @ScriptDir & "\Defrag.log"

$aArray = StringSplit(StringTrimLeft(FileReadLine($sFile, 14), 20), " ")
$sUnfrag = $aArray[1]
$aArray = StringSplit(StringTrimLeft(FileReadLine($sFile, 15), 18), " ")
$sFrag = $aArray[1]

MsgBox(0, "Bytes", "Unfragmented: " & $sUnfrag & @CRLF & "Fragmented: " & $sFrag)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

If "unfragmented" and "fragmented" only appear once in the log, you can real the whole file to a variable, split it by lines, then loop through the array of lines. In the loop, if the line contains "unfragmented", use text manipulation to pull out the bytes and set a variable equal to it. Use a different variable for "fragmented".

Look at fileread, stringsplit, for...in...next, stringinstr

Hi, Thanks for the reply. Unfortunately both words occur numerous times in the log file. I may just chalk this one up to more trouble than its worth.

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

Link to comment
Share on other sites

Well, you can still keep the code short and sweet by doing the suggested above.

Read the file line by line until you reach Fragmented or Unfragmented, call a function to do what you want when it finds that, it will then return to the loop when done and continue till end of file.

Just some thoughts to keep your project a possibility :(

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

  • Moderators

Thanks for all of the suggestions, I believe Melba's snippet gave me enough to get what I want. Thanks again.

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

Link to comment
Share on other sites

  • Moderators

With the assistance of M23's code, I was able to play around with this until I got it to do what I wanted. Below is what I ended up with, after reading that you could nest StringTrim functions.

$sFile = "C:\Fragmentation.log"

$line1 = FileReadLine($sFile, 14)

$line2 = FileReadLine($sFile, 15)

$value1 = StringTrimLeft (StringTrimRight($line1, 30), 20)

ClipPut( $value1 )

$value2 = StringTrimLeft (StringTrimRight($line2, 30), 24)

ClipPut($value2)

$frag = $value2 / $value1 * 100

$frag1 = StringTrimRight($frag, 12)

$defrag = MsgBox(4, "Fragmentation Level", $frag1 & "%. Would you like to defragment?")

Thanks again for the suggestions, works great now.

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

Link to comment
Share on other sites

With the assistance of M23's code, I was able to play around with this until I got it to do what I wanted. Below is what I ended up with, after reading that you could nest StringTrim functions.

$sFile = "C:\Fragmentation.log"

$line1 = FileReadLine($sFile, 14)

$line2 = FileReadLine($sFile, 15)

$value1 = StringTrimLeft (StringTrimRight($line1, 30), 20)

ClipPut( $value1 )

$value2 = StringTrimLeft (StringTrimRight($line2, 30), 24)

ClipPut($value2)

$frag = $value2 / $value1 * 100

$frag1 = StringTrimRight($frag, 12)

$defrag = MsgBox(4, "Fragmentation Level", $frag1 & "%. Would you like to defragment?")

Thanks again for the suggestions, works great now.

...just to make sure you know it's an option.

defrag c: -a -v
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...