Issue getting count of lines in a file - VisualCron - Forum

Community forum

Gary_W
2017-03-01T20:01:19Z
In a job triggered by a file appearing in a folder, the first task attempts to set a job variable equal to the number of rows in the file minus 2 for the header and footer rows. I am using a formula for this I found by searching in the forum here: {STRING(RowCount|{FILE(Content|g:\temp\test.txt)})}-2

The actual task looks like this:
UserPostedImage

{STRING(RowCount|{FILE(Content|{JOB(Active,Variable,JobWorkingDir)}\{TRIGGER(Active,LastTrigger,File.Result.Name)})})}-2

The result ends up being "1-2" (I now realize I'll need to use a math function to do the subtraction once I get this figured out). It seems the folder\filename gets resolved to a valid path which is 1 line, then the 1 line is counted instead of the file's content's being read by the rowcount function.

What am I doing wrong? Can't one use job variables for the PATH/FILENAME in the FILE/Content function? Or do I need to resolve those first somehow before rowcount sees them?

Thanks,
Gary
ErikC
2017-03-02T07:29:01Z
Hi,

The FILE{CONTENT|filename} is not resolving, so the ROWCOUNT sees only one line of your variable. As it is a STRING, the -2 is added to the value and you get 1-2 as result.

What you can do is using a Powershell task and using a variable as your filename.
See this post how to use a variable in powershell: Use variables in Powershell 

And with this script you get your result:

Param(
	[string] $filename
)
@(Get-Content $filename).Length - 2

Regards,
Erik
Uses Visualcron since 2006.
thomas
2017-03-02T11:53:44Z
Works for me with a Math function


{MATH(Add|Integer|{STRING(RowCount|{FILE(Content|g:\temp\test.txt)})}|-2|)}
Gary_W
2017-03-02T14:30:12Z
Gentlemen, thank you for your replies. After some continued playing around with it my original try worked after removing the "-2" from the end (which I came to know was wrong anyway). Interesting that this caused the path I built not to be interpreted correctly. I suspect it causes an internal issue and stops expansion of the variables maybe.

This could be a bug as I would still expect the variables to be resolved and the rowcount to work and the "-2" to get tacked to the end though.
Scroll to Top