Pascal

  • 24 Apr

    Pascal Script, User Request Example No 2

    Dear users,

    Second Pascal Script is based on the following user request:

    We need some help on setting up a rule.  The rule we are trying to setup is as follow. Source folder has files in it, example file names ABC-test.txt, 123_test.txt, and test_XYZ.txt.  We have a destination folder called “Test”, and we have 3 folders under it called ABC, 123, and XYZ.  We want the rule to separate the files and place them in their specified folders.  For example, file ABC-test.txt will be placed into folder ABC, 123_test.txt file will be placed into folder 123, and text_XYZ.txt file will be place into folder XYZ.  We want the files to be moved into the separated folders that matches their name.

    > Add your Destination:

    > Destination Setup, change the ‘Create Subdir, opt.’ to %VSB

    > Add the following ‘On Rule Begin’ Pascal Script. Very important: the ctOutputPath contant must be the same as your destination path (first screenshot) !

    Var
      iList: Integer;
      tmpSub: String;
      tmpList: TStringList;
    Const
      ctOutputPath = ‘C:\Test\Out\’;
    Begin
      psExitCode:= 1;
      // … add your code here
      psVSA := ”;
      tmpList := TStringList.Create;
      Try
        psListPaths(ctOutputPath, ‘*.*’, 0, tmpList);
        for iList := 0 to (tmpList.Count-1) do
        Begin
          tmpSub := tmpList.Strings[iList];
          tmpSub := psStringReplace(tmpSub, ctOutputPath, ”);
          tmpList.Strings[iList] := tmpSub;
        End;
        psVSA := tmpList.CommaText;
        // Debug
        psLogWrite(1, ”, ‘SubFolder found: ‘ + psVSA);
      Finally
        tmpList.Free;
      End;
    End.
    > Add the following ‘On Destinations‘ Pascal Script.
    Var
      iList: Integer;
      tmpSub: String;
      tmpList: TStringList;
    Const
      ctOutputSubPathNone = ‘None\’;
    Begin
      psExitCode:= 1;
      // … add your code here
      psVSB := ctOutputSubPathNone;
      // Iterate
      tmpList := TStringList.Create;
      Try
        tmpList.CommaText := psVSA;
        tmpList.Sort;
        for iList := (tmpList.Count-1) downto 0 Do
        Begin
          tmpSub := tmpList.Strings[iList];
          tmpSub := psStringReplace(tmpSub, ‘\’, ”);
          psLogWrite(1, ”, ‘Sub: ‘ + tmpSub);
          if Pos(LowerCase(tmpSub), LowerCase(psFileName)) <> 0 Then
          Begin
            psVSB := tmpSub;
            Break;
          End;
        End;
        // Debug
        psLogWrite(1, ‘Destination SubFolder will be: ‘, psVSB);
      Finally
        tmpList.Free;
      End;
    End.
    If you have any question about this example, don’t hesitate to ask.
    Regards,
    Limagito Team
  • 24 Nov

    Pascal Script, User Request Example No 1

    Dear Users,

    We often get user specific questions. Not every request goes into our file transfer tool as an option. In such cases (and if it is possible) we create a custom Pascal Script for the end user.

    Example No1:

    People are asking a lot to LimagitoX. Today, a user asked me if I can make a LimagitoX rule with a filter on the number of lines in a file.

    Example:  Transfer the file if it contains more than 300 records.  The file type is *.txt

    The rule will be:

    Transfer files with less than 300 lines in weekday.

    Transfer files with more than 300 lines on Sunday.

    No transfer on Saturday.

    Do you think you can add such a kind of filter?

     

    For this request we created the ‘On Destination’ Pascal Script below. IMPORTANT: Be sure you set the file filter to only allow *.txt files

    image.png

    Var
      tmpList: TStringList;
      tmpNow: TDateTime;
    Begin
      psExitCode:= 0;
      // … add your code here
      tmpList := TStringList.Create;
      Try
        Try
          tmpList.LoadFromFile(psFilePath + psFileName);
          tmpNow := Now;
          // Transfer files with less than 300 lines in weekday ( 1 .. 5 ).
          If (tmpList.Count < 300) And (DayOfTheWeek(tmpNow) < 6) Then
          Begin
            psLogWrite(1, ”, ‘Line Count of ‘ + psFilePath + psFileName + ‘ = ‘ + IntToStr(tmpList.Count));
            psLogWrite(1, ”, ‘Day Of The Week is ‘ + IntToStr(DayOfTheWeek(tmpNow)));
            psExitCode := 1;
          End;
          // Transfer files with more than 300 lines on Sunday ( 7 ).
          If (tmpList.Count > 300) And (DayOfTheWeek(tmpNow) = 7) Then
          Begin
            psLogWrite(1, ”, ‘Line Count of ‘ + psFilePath + psFileName + ‘ = ‘ + IntToStr(tmpList.Count));
            psLogWrite(1, ”, ‘Day Of The Week is ‘ + IntToStr(DayOfTheWeek(tmpNow)));
            psExitCode := 1;
          End;
        Except
          psLogWrite(1, ”, ‘Exception error on loading ‘ + psFilePath + psFileName);
        End;
      Finally
        tmpList.free;
      End;
    End.
  • 26 Aug

    On Pascal Script Events

    Starting from version 11.8.26.0 we’ve added two new events:

    • On Pascal Script Success
    • On Pascal Script Error

    Events.PascalScript

     

    When enabled these events can be triggered by the psExitCode of any enabled Pascal Script.

    PascalScript.Events

     

    • Setting the psExitcode to 0 (zero) will trigger the ‘On Pascal Script Error’ event
    • Setting the psExitcode to 1 (one) will trigger the ‘On Pascal Script Success’ event
    • Setting psExitCode to any other value will not trigger the ‘On Pascal Script’ events

    Regards,

    Limagito Team