Why is this REGEX Replace not working? - VisualCron - Forum

Community forum

Maybe I've been trying to hard and am at the frustration point where I am missing something obvious. I'm even using an example from another post that I myself wrote and it's not working!
VC 8.2.9

With a loop I am getting the files in a folder. For each one I am extracting the date from the filename and storing it in a job variable. I need to add a pipe and the job variable contents to the end of every line in the file. Should be simple! So I use a read task to read the contents of the file then a write task to a new file.

Here's the "value" section from the write file task. in regex logic, I want to replace the end of the line with the contents of the job variable. "Error in argument".
{TASK(PrevTask,StdOut)} by itself works ok.


Save me from going crazy!!! is there a visualcron-specific regex character for the EOL that I should be using? It's usually a dollar sign but that does not work here for matching the end of the line.

By the way, I must say "Error in argument" is not enough info for an error message. At least report what argument and what the error is please!

EDIT 2/12/2019 for future searchers. I learned recently when you need to use the pipe symbol in your regex, and you thus need to use a different character as the separator in the REGEX command (as the pipe is the normal separator in visualcron functions), the character you choose needs to be the first character in the REGEX call. Here I used a comma, so if I were to have constructed the call like the following, all would have been good! Note the comma as the first character in front of the REPLACE.

Hi Gary,

I think you have the to use a different character. The ',' and the '|' are special characters in a formula.

I have the solution for you: Use a powershell task to do this.

  1. Have three parameters in there and name them: source, regex, newtext
  2. add the values to the paramaters: source = {TASK(PrevTask,StdOut)} and: regex = \r\n and: newtext = |{JOB(Active,Variable,JobFileDate)}
  3. create the folowing script:

	[string] $source,
	[string] $regex,
	[string] $newtext

"$source" -replace "$regex","$newtext"

Using te parameters (in the script) you use a '$' in front of the name.

You can use the Powershell output to write data to the file.

Uses Visualcron since 2006.
Thanks for the suggestion! I have only dabbled with powershell so it was good to try something new. This ended up adding the string only to the end of the file though. I suspected my regex string was not right so after some searching and tweaking, I ended up with an even better solution using Powershell that edits the file in place.

As you said, I created the Powershell task but with 2 parameters, "source" and "newtext". Source I set equal to "{LOOP(CurrentValueXLine)}" so it will be set to the current file and newtext will be the file date from the file name. Then, this code edits the file in place, added the date to the end of each line:

[string] $source,
[string] $newtext

(get-content "$source") | foreach { $_ + "$newtext"} | set-content "$source"

This saves me a read and write file task. Another one for the bag of tricks! Thank you Erik!
Scroll to Top