Network Automation Software We are currently migrating this forum
over to our HelpSystems domain. Please
post all new threads in our new
HelpSystems Community Portal.
Post to the HelpSystems Forum
You are not currently logged on. You must be logged on in order to post. Log on
Or Create a new account
AutoMate Discussion
Decrease font size
Increase font size
Topic Title: Read File Into Variable And Retrieve Number of Lines
Topic Summary: Number Of Lines Ending With CR
Created On: 05/05/2006 02:34 PM
Status: Read Only
Linear : Threading : Single : Branch
Search Topic Search Topic
Topic Tools Topic Tools
View similar topics View similar topics
View topic in raw text format. Print this topic.
 05/05/2006 02:34 PM
User is offline View Users Profile Print this message

Author Icon
Eric Enderle
Expert (100-199)

Posts: 155
Joined: 04/10/2005

Hello,
                      
                Need some help, I need to determine the number of lines contained in a text file I receive from different locations around the U.S. I have setup a variable called file and a variable called file_bytes, I read the file contents into the variable file and the file bytes into the variable file_bytes, now if each line were the same length I could use division to determine the number of lines but each line is a different length and will change from file to file, is there a way to determine the number of lines ending in a crlf in a text file ?

Sample of data below:

Los Angeles
Carson
Las Vegas
San Francisco University


Any help appreciated...

Thanks
Eric.

 Category Survey


-------------------------
Eric Enderle
Network Automation & Programming Manager
Enforcement Technology Inc
Irvine, Ca. 92618



Edited: 05/05/2006 at 02:34 PM by Eric Enderle
 05/05/2006 02:55 PM
User is offline View Users Profile Print this message

Author Icon
ScottParker
Artisan (200-499)

Posts: 433
Joined: 08/29/2005

You can use an 'Embedded VB' script, or the Automate 'Loop List' action.  Here is an actual embed script I use:




 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)


-------------------------
Scott W. Parker - Automate Beta Tester
Win XP Pro SP3 - Automate 7 Premium
 05/05/2006 09:16 PM
User is offline View Users Profile Print this message

Author Icon
JeanDelfosse
Supreme Guru (1000+)

Posts: 8883
Joined: 03/30/2005

Eric,
once the file is read
a very simple expression could do it.
It just counts the number of vbCrLf's

 Category Survey
AutoMate 6 version:
Windows version:



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)


-------------------------
Jean Delfosse - Automate Beta Tester
Win7 Pro 64 bits -Win XP Pro SP3 32 bits - AM8, AM7, BPA10, BPA9, BPA8, BPA7, AM6 Enterprise, AM4
 05/06/2006 09:38 AM
User is offline View Users Profile Print this message

Author Icon
Pete
Master (500-999)

Posts: 740
Joined: 03/30/2005

Hi Eric,

Jean and I are not in complete agreement about point (1) below, but anyway, in addition to Jean's and Scott's versions, you can also look at mine in the Task Exchange forum: Tasks to count lines in a text file

Some notes about my version:

1) It will correctly count the lines if the last line in the file does not have a CR/LF.

2) It will report zero lines if the file is empty.

3) If the file is big, you don't have to read the entire thing into a variable, so it would be faster.

It doesn't matter to me which version you use, but if you use mine I recommend that you use the second one in that topic.

 Category Survey


-------------------------
AutoMate Pro 7.1.1.0 (Medium isolation level)
WinXP Home sp3, "Classic Theme", 120 DPI

Edited: 05/06/2006 at 10:35 AM by Pete
 05/06/2006 12:08 PM
User is offline View Users Profile Print this message

Author Icon
JeanDelfosse
Supreme Guru (1000+)

Posts: 8883
Joined: 03/30/2005

Yes,
it's always better when the customer has the choice among various products.

 Category Survey
AutoMate 6 version:
Windows version:


-------------------------
Jean Delfosse - Automate Beta Tester
Win7 Pro 64 bits -Win XP Pro SP3 32 bits - AM8, AM7, BPA10, BPA9, BPA8, BPA7, AM6 Enterprise, AM4
 05/17/2006 08:38 AM
User is offline View Users Profile Print this message

Author Icon
Eric Enderle
Expert (100-199)

Posts: 155
Joined: 04/10/2005

Thank you both for your help, I will try both and use whatever works best....


Thanks Again..

Eric.

 Category Survey


-------------------------
Eric Enderle
Network Automation & Programming Manager
Enforcement Technology Inc
Irvine, Ca. 92618

 05/17/2006 09:15 AM
User is offline View Users Profile Print this message

Author Icon
JeanDelfosse
Supreme Guru (1000+)

Posts: 8883
Joined: 03/30/2005

You're welcome, Eric

 Category Survey
AutoMate 6 version:
Windows version:


-------------------------
Jean Delfosse - Automate Beta Tester
Win7 Pro 64 bits -Win XP Pro SP3 32 bits - AM8, AM7, BPA10, BPA9, BPA8, BPA7, AM6 Enterprise, AM4
 05/17/2006 11:23 AM
User is offline View Users Profile Print this message

Author Icon
Eric Enderle
Expert (100-199)

Posts: 155
Joined: 04/10/2005

Guys,
      Guess I am going to need a little help, I am working with petes script (and I have never used scripts before), I have changed his script to use my variables and seem to be getting an error message, can someone check my syntax and see if I am doing this correctly...???  See below...
Variables are:

Account_Logon - Directory where files are located
Pick_File_Name - File Name

Function COUNTLINES(U:\Autopark\%Account_Logon%\%Pick_File_Name%)
Open U:\Autopark\%Account_Logon%\%Pick_File_Name% For Input As #1
Lines = 0
While Not EOF(1)
        Line Input #1, FileLine
        Lines = Lines + 1
Wend
Close #1 COUNTLINES = Lines
End Function


Thanks.

Eric.


 Category Survey


-------------------------
Eric Enderle
Network Automation & Programming Manager
Enforcement Technology Inc
Irvine, Ca. 92618

 05/17/2006 01:16 PM
User is offline View Users Profile Print this message

Author Icon
JeanDelfosse
Supreme Guru (1000+)

Posts: 8883
Joined: 03/30/2005

Eric
It should be

Function CountLines(fn)
Open fn for input as #1
the rest unchanged.

It is when calling the function from Automate that you write
%CountLines("U:\Autopark\" & Account_Logon & "\" & Pick_File_Name)%

This way CountFiles is called with the full file name as an argument

 Category Survey
AutoMate 6 version:
Windows version:


-------------------------
Jean Delfosse - Automate Beta Tester
Win7 Pro 64 bits -Win XP Pro SP3 32 bits - AM8, AM7, BPA10, BPA9, BPA8, BPA7, AM6 Enterprise, AM4
 05/17/2006 02:03 PM
User is offline View Users Profile Print this message

Author Icon
Eric Enderle
Expert (100-199)

Posts: 155
Joined: 04/10/2005

Thanks for the quick response, I,ll give it a try....




Thanks Jean.....


Eric


 Category Survey


-------------------------
Eric Enderle
Network Automation & Programming Manager
Enforcement Technology Inc
Irvine, Ca. 92618

 05/17/2006 02:28 PM
User is offline View Users Profile Print this message

Author Icon
Eric Enderle
Expert (100-199)

Posts: 155
Joined: 04/10/2005

Jean,
            I forgot to mention this machine is running 5.5.5.1 of automate and perhaps that is why I am having the problem, I have changed the script back to what you recommended and it appears in the task builder as "run embedded script" but I don,t see how to pass the file variable to the script unless the variables are contained within the script ?

Function COUNTLINES (fn)
    Open fn For Input As #1
    Lines = 0
    While Not EOF(1)
        Line Input #1, FileLine
        Lines = Lines + 1
    Wend
    Close #1
    COUNTLINES = Lines
End Function

what I need is to pass a file variable to the script have it count the lines and
place the results in another variable, how do you call a function and pass a file variable in 5.5.5.1 ?


 Category Survey


-------------------------
Eric Enderle
Network Automation & Programming Manager
Enforcement Technology Inc
Irvine, Ca. 92618

 05/17/2006 02:33 PM
User is offline View Users Profile Print this message

Author Icon
Adam Kosecki
AUTOMATE EMPLOYEE

Posts: 1387
Joined: 03/30/2005

Originally posted by: EricEnderle Jean,             I forgot to mention this machine is running 5.5.5.1 of automate


Please keep AutoMate 5 questions in the AutoMate 5 forum category.

 Category Survey


-------------------------
Adam Kosecki
Network Automation, Inc.

Edited: 05/17/2006 at 02:33 PM by Adam Kosecki
 05/17/2006 02:37 PM
User is offline View Users Profile Print this message

Author Icon
Adam Kosecki
AUTOMATE EMPLOYEE

Posts: 1387
Joined: 03/30/2005

Please see the attached task for an example of variable inheritance.

 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)


-------------------------
Adam Kosecki
Network Automation, Inc.
 05/18/2006 10:05 AM
User is offline View Users Profile Print this message

Author Icon
Pete
Master (500-999)

Posts: 740
Joined: 03/30/2005

Originally posted by: EricEnderle
what I need is to pass a file variable to the script have it count the lines and place the results in another variable, [...]

Hi Eric, I've attached a step that does this. The filename (either the actual filename or a variable) will be passed to the function called CountLines.

Anywhere that you use the expression %CountLines(filename)%, Automate will change it to the number of lines in the file.

So among other things, you can use the expression %CountLines(filename)% in a message-box action, in a create-variable action, in a set-variable action, etc. The attached step uses it in a create-variable action because it seems that this is what you want.

 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)


-------------------------
AutoMate Pro 7.1.1.0 (Medium isolation level)
WinXP Home sp3, "Classic Theme", 120 DPI
 05/18/2006 10:17 AM
User is offline View Users Profile Print this message

Author Icon
Eric Enderle
Expert (100-199)

Posts: 155
Joined: 04/10/2005

Thanks Pete !!!..

 Category Survey


-------------------------
Eric Enderle
Network Automation & Programming Manager
Enforcement Technology Inc
Irvine, Ca. 92618

 05/19/2006 08:44 AM
User is offline View Users Profile Print this message

Author Icon
AkivaMiller
Expert (100-199)

Posts: 172
Joined: 03/06/2006

Another "Line Counter" method:

Use SQL to query the number of lines. Yes, SQL can query a .txt file!

This is done by creating a 'database' connection to the directory containing the file.

1) Create a variable named, DIRECTORY
2) Create a variable named, FILE
3) Click Database\SQL Query
4) Enter the following into the Connection String field
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%DIRECTORY%;Extended Properties="text;HDR=NO;FMT=CVSDelimited";Persist Security Info=False
5) Enter a dataset named: line_count
6) Enter the following query in the SQL Statement field:
   select count(*) from %FILE%
7) Use Message Box to output the result: Number of lines: %line_count.Expr1000%


Here is the code:

<AMVARIABLE NAME="DIRECTORY">C:\wget\staging\dds</AMVARIABLE><AMVARIABLE NAME="FILE">student.txt</AMVARIABLE><AMSQLQUERY CONNECTIONSTRING="19y9I4LjuL6XmCLAvRM48OiQXzPcJt2zNDf6g2L8y8OlibGYYt4mg6taNYQQK0X36Gu+Zr1pEesi9n/LZCNdDDYTsfkjYiIvxGAlAD2vFWkumyw+UnDJfF/J0HKU4br73wUgv0Re/AacVKz3J9ZXPxWRQgLVdFv35NK8KWO1r/speMasdAeI1vQ==" RESULTDATASET="line_count">select count(*)from %FILE%</AMSQLQUERY><AMMESSAGEBOX WINDOWTITLE="Line Counter">Number of lines: %line_count.Expr1000%</AMMESSAGEBOX>


 Category Survey
 06/02/2006 09:59 AM
User is offline View Users Profile Print this message

Author Icon
Eric Enderle
Expert (100-199)

Posts: 155
Joined: 04/10/2005

Akiva,

     Very cool, thanks for yet another option......


Eric.

 Category Survey


-------------------------
Eric Enderle
Network Automation & Programming Manager
Enforcement Technology Inc
Irvine, Ca. 92618

 06/02/2006 11:00 AM
User is offline View Users Profile Print this message

Author Icon
RollandCoburn
AUTOMATE EMPLOYEE

Posts: 715
Joined: 05/15/2006

I second that.  Great work Akiva.

 Category Survey


-------------------------
Rolland Coburn

Network Automation, Inc
 07/19/2007 12:39 PM
User is offline View Users Profile Print this message

Author Icon
Kelly Morris
Apprentice (20-49)

Posts: 31
Joined: 05/02/2007

Pete - is there an easy way to adjust my SQL statement to correctly account for no lines? The issue I have is if there are no lines to count - I get errors and can't get a correct count. Here is my code:

 Category Survey



Task Steps: (Right click, Select All, Copy to copy to the clipboard. Steps can then be pasted into the AutoMate Task Builder)
 07/19/2007 12:47 PM
User is offline View Users Profile Print this message

Author Icon
Pete
Master (500-999)

Posts: 740
Joined: 03/30/2005

Unfortunately I've never used Automate's SQL actions so I don't know the answer but maybe someone else will help.

 Category Survey


-------------------------
AutoMate Pro 7.1.1.0 (Medium isolation level)
WinXP Home sp3, "Classic Theme", 120 DPI
 06/17/2014 01:18 PM
User is offline View Users Profile Print this message

Author Icon
mctimoth
Journeyman (50-99)

Posts: 66
Joined: 05/04/2007

@AkivaMiller: What is done for zero line (empty) files?

 Category Survey
AutoMate 6 version:
Windows version:
 06/17/2014 04:07 PM
User is offline View Users Profile Print this message

Author Icon
Alex Escalante
HelpSystems

Posts: 871
Joined: 02/26/2014

Hello Kelly,

What is the error that you receive when there are no lines to count?

 Category Survey
AutoMate 6 version:
Windows version:


-------------------------

Alex Escalante | Support Analyst
AutoMate | Division of HelpSystems
HelpSystems, LLC | T: +1.213.738.1700 | F: +1.213.738.7665
Statistics
18258 users are registered to the AutoMate Discussion forum.
There are currently 0 users logged in.
The most users ever online was 5551 on 01/08/2018 at 11:11 AM.
There are currently 1556 guests browsing this forum, which makes a total of 1556 users using this forum.

FuseTalk Enterprise Edition v4.0 - © 1999-2020 FuseTalk Inc. All rights reserved.

Sitemap Network Automation Software Blog