Source language: Translate to:

Reading an external file

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Reading an external file

Postby stevec » Fri Feb 09, 2007 11:02 am

Hi all.
I need some input with this.
I have an external file being generated.
Here's what it looks like:

Volume fragmentation
Total fragmentation = 31 %
File fragmentation = 62 %
Free space fragmentation = 0 %

I need to be able to have the pub, just pick up the 31% and 61%
figures and save to a variabvle.

The action that I'm currently using is:
FileRead "!C:\WINDOWS\Temp\defrag.txt" "13" "[TFrag1]"

Can anyone help me with this?

many thanks;
Steve Christensen
Steve Christensen
stevec
 
Posts: 223
Joined: Fri Apr 15, 2005 7:33 am
Location: Boise, Idaho

Postby Gaev » Fri Feb 09, 2007 11:20 am

Steve:
FileRead "!C:\WINDOWS\Temp\defrag.txt" "13" "[TFrag1]"

a) What is the significance of line "13" ?

b) Try ...
Code: Select all
FileRead "!C:\WINDOWS\Temp\defrag.txt" "All" "[TFrag1]"
... Total fragmentation Value
StrParse "[TFrag1]" "Total fragmentation = " "[TFragPart]" "[junk]"
StrParse "[TFragPart2]" "! %" "[TFragValue]" "[junk]"
... your required number will be in [TFragValue1]

- first StrParse will give you everything after the text "Total fragmentation = " in variable [TFragPart2]
- second StrParse will place stuff in [TFragPart2] that is before the " %" (note leading space) into [TFragValue1]
- if you actually needed the space and percent signs as well, add this line at the end ...
Code: Select all
SetVar "![TFragPart2]" "![TFragPart2] %"


c) repeat for each of the other values ... using different text strings in the first StrParse ... and perhaps different [variableNames].
User avatar
Gaev
 
Posts: 3737
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby Sam Cox » Fri Feb 09, 2007 11:29 am

In your FileRead statement, what is the "13" doing there?

Assuming that the file consists entirely of this sample:
Volume fragmentation
Total fragmentation = 31 %
File fragmentation = 62 %
Free space fragmentation = 0 %

then the following should work, but I have not actually tried it.
Code: Select all
FileRead "!C:\WINDOWS\Temp\defrag.txt" "all" "[TFrag1]"
StrParse "[TFrag1]" "[#13]" "[Line]" "[LineCount]"
Loop "1" "[LineCount]" "[N]"
  StrParse "[Line[N]]" " = " "[Field]" "[FieldCount]"
  SetVar "[Line[N]]" "[Field2]"
EndLoop

When this loop completes, the variables [Line1] through [Line4] will contain:

[Line1] = ""
[Line2] = "31 %"
[Line3] = "62 %"
[Line4] = "0 %"

Darn! Gaev beat me ... again!
User avatar
Sam Cox
 
Posts: 768
Joined: Fri Apr 01, 2005 7:30 am
Location: Loveland CO USA

Code

Postby stevec » Fri Feb 09, 2007 11:31 am

Here is the file text:

Windows Disk Defragmenter
Copyright (c) 2001 Microsoft Corp. and Executive Software International, Inc.

Analysis Report

Volume size = 74.50 GB
Cluster size = 4 KB
Used space = 42.26 GB
Free space = 32.24 GB
Percent free space = 43 %

Volume fragmentation
Total fragmentation = 31 %
File fragmentation = 62 %
Free space fragmentation = 0 %

File fragmentation
Total files = 57,201
Average file size = 918 KB
Total fragmented files = 1,355
Total excess fragments = 5,209
Average fragments per file = 1.09

Pagefile fragmentation
Pagefile size = 1.49 GB
Total fragments = 1

Folder fragmentation
Total folders = 3,587
Fragmented folders = 46
Excess folder fragments = 413

Master File Table (MFT) fragmentation
Total MFT size = 65 MB
MFT record count = 60,898
Percent MFT in use = 91
Total MFT fragments = 3

You should defragment this volume.

I'm trying to get the "volume fragmentation" information into a pub.
The code that you send, no errors but no data in the box.
I renamed the variable to TFragPart2] in the textbox.

Steve Christensen
Steve Christensen
stevec
 
Posts: 223
Joined: Fri Apr 15, 2005 7:33 am
Location: Boise, Idaho

Postby Sam Cox » Fri Feb 09, 2007 11:49 am

Here's an idea that will work given the particular format of this file
Code: Select all
.step 1
SetVar "[File]" "C:\WINDOWS\Temp\defrag.txt"
FileRead "[File]" "all" "[temp]"
StrReplace "[temp]" " = " "=" "[temp]"
FileWrite "[File]" "all" "[temp]"

.step 2
LoadVariables "[File]"

In step 1, we reformat the file to replace every " = " (space-equal-space) with "=" (equal).

In step 2, we use Neobook's LoadVariables command to read the file and, in doing so, assign values to variables. The variable names will be the text on the left side of each "=" and the values will be the text on the right side. Lines without "=" are ignored.

When done with step 2, variable [Volume size] will contain "74.50 GB", variable [Percent free space] will contain "43 %", and so forth. Notice that the variable names include spaces, and Neobook is okay with that.

I use this exact technique in several of my production programs.

-- Sam
User avatar
Sam Cox
 
Posts: 768
Joined: Fri Apr 01, 2005 7:30 am
Location: Loveland CO USA

Postby Gaev » Fri Feb 09, 2007 11:53 am

Steve:
The code that you send, no errors but no data in the box.
I renamed the variable to TFragPart2] in the textbox.

a) WHose code ? ... Sam Cox or mine ?

b) Try and add an AlertBox command at the end of the routine I supplied ...
Code: Select all
AlertBox "TFragValue1" "[TFragValue1]"

... and post your result here.

If it displays nothing in the message part of the box, add another ...
Code: Select all
AlertBox "TFragPart2" "[TFragPart2]"
... and post it too ... note: this might popup a large box as there is quite a lot of text in this variable.
User avatar
Gaev
 
Posts: 3737
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

file read

Postby stevec » Fri Feb 09, 2007 12:12 pm

Sam's file, runs and I can see the variables in the "Debugger box" however nothing on the pub.

Yours I get a pop-up box with a error, an OK button and thats all.

Sam:
I tried using the [File Fragmentation] variable, is there a way to take out the % sign as I would like to use the number in math, if I can get the
variable to show up.

Gaev
I like the code. I can't seem to find the correct variable name to put in the text box.
Steve Christensen
stevec
 
Posts: 223
Joined: Fri Apr 15, 2005 7:33 am
Location: Boise, Idaho

Postby Sam Cox » Fri Feb 09, 2007 12:27 pm

Hi Steve,

Now that we have a value in [File Fragmentation], we can clean it up using the StrParse command. Because of the way we rewrote the contents of the file, we know there are no leading spaces in the value, but there might be trailing spaces after the "%". Executing the following:
Code: Select all
StrParse "[File Fragmentation]" "[#32]" "[X]" "[unused]"

will extract the value into variable [X1]. Then:
Code: Select all
SetVar "[FileFrag]" "[X1]"

should complete the job.

You said, "I can see the variables in the 'Debugger box' however nothing on the pub." To see values in the pub, you will have to explicitly display them. To make sure you are getting everything you want from the file, create a SimpleText object and enter the variables (such as [File Fragmentation] and [FileFrag]). When you run the program, Neobook will display the values of the variables.

Hope this helps,
-- Sam
User avatar
Sam Cox
 
Posts: 768
Joined: Fri Apr 01, 2005 7:30 am
Location: Loveland CO USA

part issue solved

Postby stevec » Fri Feb 09, 2007 12:28 pm

The issue with the variable not showing up is with the spaces in the text file.
The origional defrag.txt file has spaces in the body and not all aligned to the left side of the page.
After I corrected this the variables showed up.
Now if I can get rid of the % signs, I can use it.

I just need to redo the origional file with one from notepad and get rid of the spaces.
Can the "FileRead "[File]" "all" "[temp]" get rid of spaces?

Thanks;
Steve Christensen
Steve Christensen
stevec
 
Posts: 223
Joined: Fri Apr 15, 2005 7:33 am
Location: Boise, Idaho

Postby Sam Cox » Fri Feb 09, 2007 12:33 pm

Try this:
Code: Select all
FileRead "[File]" "all" "[temp]"
StrReplace "[temp]" " " "" "[temp]"
StrReplace "[temp]" "%" "" [temp]"
FileWrite "[File]" "all" "[temp]"

which will kill every space and percent sign in the file. When you use LoadVariables on THIS reformatted file, your variable names should be clean (no spaces at all), and the percent signs will be gone.
User avatar
Sam Cox
 
Posts: 768
Joined: Fri Apr 01, 2005 7:30 am
Location: Loveland CO USA

Postby Gaev » Fri Feb 09, 2007 12:45 pm

Steve:
Yours I get a pop-up box with a error, an OK button and thats all.

Which (exact) command causes this ?
I can't seem to find the correct variable name to put in the text box.

As commented in the last line of the code box ...
Code: Select all
your required number will be in [TFragValue1]


Perhaps you can upload your file to Robert's NeoBook File Archive ... see third image box at top left corner of this page ... may be the file you have contains Tab characters ... that look like spaces on this forum post ... so one of us can examine its exact contents.
User avatar
Gaev
 
Posts: 3737
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby stevec » Fri Feb 09, 2007 1:07 pm

Sam;
Where should I place the code?
Steve Christensen
stevec
 
Posts: 223
Joined: Fri Apr 15, 2005 7:33 am
Location: Boise, Idaho

Postby Sam Cox » Fri Feb 09, 2007 1:11 pm

Use the new code in place of the FileRead-StrReplace-FileWrite code in "step 1" of my previous post -- the post beginning "Here's an idea ...". Basically, we are rewriting (or reformatting) the file differently than we did in the earlier code. It goes between the SetVar "[File]" and LoadVariables "[File]" statements.
User avatar
Sam Cox
 
Posts: 768
Joined: Fri Apr 01, 2005 7:30 am
Location: Loveland CO USA

Postby stevec » Fri Feb 09, 2007 1:19 pm

Sam;
I had it working.
Even added a progress bar and it was working.
The variable now has a [] like a tab space and it fails.
:( :( :(
Steve Christensen
stevec
 
Posts: 223
Joined: Fri Apr 15, 2005 7:33 am
Location: Boise, Idaho

Postby stevec » Fri Feb 09, 2007 1:22 pm

The format of the txt file is the following.

do the following.

Start- Run- CMD.
In the dos box type the following

defrag C: -a -v >C:\windows\temp\defrag.txt

Go to the C:\windows\temp folder and you'll see the
defrag.txt file. open it and you can see the format.

Steve Christensen
Steve Christensen
stevec
 
Posts: 223
Joined: Fri Apr 15, 2005 7:33 am
Location: Boise, Idaho

Next

Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 1 guest

cron