Pascal Script

  • 07 May

    Copy only the modified contents of a file

    Q: We are using Limagito for different projects and we have a query to its functionality. We want to see that it can copy only the modified contents of a file  in a new file with same name. Below is an example on what is being appended and how.

    Let us use the following example: Equipment_AE_Alarm_Event0_2023-05-04_00.00.01.csv.

    • The original content of this csv file:

    “Time;””AlarTxt””;””AlmPrio””;””Almusr””;””acksts”””
    “2023-05-03 00:00:02;””sample text1″”;200;KLMN;1″
    “2023-05-03 00:00:03;””sample text2″”;300;KLMN;1″
    “2023-05-03 00:00:04;””sample text3″”;100;KLMN;0″
    “2023-05-03 00:00:05;””sample text4″”;200;KLMN;1″
    “2023-05-03 00:00:06;””sample text5″”;200;KLMN;1″

    • Expected content of the output file would be the same when we scan this file for the first time:

    “Time;””AlarTxt””;””AlmPrio””;””Almusr””;””acksts”””
    “2023-05-03 00:00:02;””sample text1″”;200;KLMN;1″
    “2023-05-03 00:00:03;””sample text2″”;300;KLMN;1″
    “2023-05-03 00:00:04;””sample text3″”;100;KLMN;0″
    “2023-05-03 00:00:05;””sample text4″”;200;KLMN;1″
    “2023-05-03 00:00:06;””sample text5″”;200;KLMN;1″

    • Data is being added to this csv so the content of file during the next scan is showing more entries:

    “Time;””AlarTxt””;””AlmPrio””;””Almusr””;””acksts”””
    “2023-05-03 00:00:02;””sample text1″”;200;KLMN;1″
    “2023-05-03 00:00:03;””sample text2″”;300;KLMN;1″
    “2023-05-03 00:00:04;””sample text3″”;100;KLMN;0″
    “2023-05-03 00:00:05;””sample text4″”;200;KLMN;1″
    “2023-05-03 00:00:06;””sample text5″”;200;KLMN;1″
    “2023-05-03 00:00:06;””sample text6″”;200;KLMN;0″
    “2023-05-03 00:00:06;””sample text7″”;200;KLMN;1″
    “2023-05-03 00:00:08;””sample text8″”;200;KLMN;0″
    “2023-05-03 00:00:10;””sample text9″”;200;KLMN;1″
    “2023-05-03 00:00:12;””sample text10″”;200;KLMN;0″

    • Expected content of the output file after the second scan should be:

    “Time;””AlarTxt””;””AlmPrio””;””Almusr””;””acksts”””
    “2023-05-03 00:00:06;””sample text6″”;200;KLMN;0″
    “2023-05-03 00:00:06;””sample text7″”;200;KLMN;1″
    “2023-05-03 00:00:08;””sample text8″”;200;KLMN;0″
    “2023-05-03 00:00:10;””sample text9″”;200;KLMN;1″
    “2023-05-03 00:00:12;””sample text10″”;200;KLMN;0″

     

    A: This should be possible using our scripting option. We created a script that will keep the data of the csv files it already handled in a separate folder (= memory folder). These memory files will be used during next scans to only create output files where the content has new data.

    For the test, we added 3 folders:

    • Input
    • Memory
    • Output

    Be sure the ‘Memory’ folder is a local folder on the system where our File Mover is running.

    Modified contents of a file example

    Our Source will be the Input folder:

    Limagito file mover using WIN as source

    We added ‘*.csv’ as Filename Include filter because we only want to scan for csv files:

    limagito file mover filename filter

    Optional: we changed the ‘Source File Sort Order’ option to ‘File last write date ascending (Old to New)’. This will handle the oldest csv files first.

    limagito file mover scan older files first

    The function is set to ‘Copy Files’:

    limagito file mover function setup

    As Destination we’ll use our ‘Pascal Script’ option:

    You’ll need to adjust the following 2 constant values ( always end the constant with a \ ):

    • ctMemPath ( = path were we’ll keep our memory data )
    • ctOutPath ( = output path)

    We added a timestamp to the output filename. The format we used is ‘YYYYMMDDHHNNSS’. The reason we did this is to prevent data loss (in case the next system would not pickup the output files).

     

    Var
      tmpEntry, tmpText, tmpFileIn, tmpFileMem, tmpFileOut, tmpFileExtIn, tmpFileExtOut: String;
      tmpList, tmpListMem: TStringList;
    Const
      ctMemPath = 'C:\Test\Astra\Memory\';
      ctOutPath = 'C:\Test\Astra\Output\';
    Begin
      psExitCode:= 0;
      tmpFileIn := psFilePath + psFileName;
      tmpFileMem := ctMemPath + psFileName;
      tmpFileExtIn := ExtractFileExt(psFileName);
      tmpFileExtOut := '.' + FormatDateTime('YYYYMMDDHHNNSS', Now) + tmpFileExtIn;
      tmpFileOut := psStringReplace(psFileName, tmpFileExtIn, tmpFileExtOut);
      tmpFileOut := ctOutPath + tmpFileOut; 
      // Create Var
      tmpList := TStringList.Create; tmpListMem := TStringList.Create;
      Try
        Try 
          tmpList.LoadFromFile(tmpFileIn);
          // Check if file is already available in Memory folder
          If tmpList.Count > 1 Then
          Begin
            tmpEntry := tmpList.Strings[0]; tmpText := tmpList.Text; 
            If FileExists(tmpFileMem) Then
            Begin
              psLogWrite(1, '', psFileName + ' available in memory folder: ' + ctMemPath);                 
              Try
                tmpListMem.LoadFromFile(tmpFileMem);                          
                tmpList.Text := psStringReplace(tmpText, tmpListMem.Text, '');
                // tmpList.Insert(0, tmpEntry);
                psExitCode := 1;         
              Except
                psLogWrite(1, '', 'Error loading data from: ' + tmpFileMem);
              End;          
            End Else
            Begin
              psExitCode := 1;
              psLogWrite(1, '', psFileName + ' not available in memory folder: ' + ctMemPath);               
            End;  
            If tmpList.Count > 1 Then
            Begin  
              // Save to output folder
              If psExitCode = 1 Then 
              Begin
                psExitCode := 0;
                Try
                  psLogWrite(1, '', 'Saving data to: ' + tmpFileOut);       
                  tmpList.SaveToFile(tmpFileOut);
                  psExitCode := 1;
                Except
                  psLogWrite(1, '', 'Error saving data to: ' + tmpFileOut);
                End;  
              End;
              // Save to Memory folder      
              If psExitCode = 1 Then
              Begin
                psExitCode := 0;
                Try
                  psLogWrite(1, '', 'Saving data to: ' + tmpFileMem);
                  tmpList.Text := tmpText; tmpList.Delete(0);       
                  tmpList.SaveToFile(tmpFileMem);
                  psExitCode := 1;
                Except
                  psLogWrite(1, '', 'Error saving data to: ' + tmpFileOut);
                End;          
              End;
            End;        
          End Else
            psLogWrite(1, '', 'No data available in: ' + tmpFileIn);   
        Except
          psLogWrite(1, '', 'Error loading data from: ' + tmpFileIn);
        End;
      Finally
        tmpList.free; tmpListMem.Free;
      End;
    End.

    limagito file mover pascal script as destination

    This blog article is just a basic example of the customers request and can be tweaked so that it meets your requirements. Just contact us if you need help.

    Don’t forget to add an extra Rule which will cleanup the Memory folder on regular basis.

    #FileTransfer #csv #scada #MFT

    If you need any info  about this ‘Copy only the modified contents of a file’ request, please let us know.

    Best regards,Limagito Team

  • 24 Apr

    File renaming logic using day of week function

    Q: We need the dates tv listing dates moved to publications dates. This newspaper publishes 3 days a week, Wednesday, Friday and Saturday. In order to automatically flow the graphic onto the page, the file name needs to match the publication date.

    We need the following logic,

    • Wednesday graphic gets Wednesday date,
    • Thursday graphic gets Wednesday date (the day before)
    • Friday graphic gets Friday date
    • Saturday graphic gets Friday date (the day before)
    • Sunday / Monday / Tuesday graphics get the previous Saturday date.

    For example:

    Sunday / Monday / Tuesday dates:

    • PH_20230423.pdf
    • PH_20230424.pdf
    • PH_20230425.pdf

    We’d want renamed to

    • PH_Sun_20230422.pdf
    • PH__Mon_20230422.pdf
    • PH_Tues_20230422.pdf

    Technically the “Sun”, “Mon”, “Tues” could be anything as long as they’re unique and predictable.

    Moving onto the next set:

    • PH_20230426.pdf
    • PH_20230427.pdf

    Would be renamed to

    • PH_Wed_20230426.pdf
    • PH_Thurs_20230426.pdf

    And finally:

    • PH_20230428.pdf
    • PH_20230429.pdf

    Would be renamed to

    • PH_Fri_20230428.pdf
    • PH_Sat_20230428.pdf

     

    A: Yes this is possible using our Mover Pascal Script and Destination RegEx renaming options.

    • Open ‘Pascal Script’ setup

    Limagito File Mover Pascal Script

    • Enable and add the following ‘On Destination’ Pascal Script:
    Var
      tmpLen: Integer;
      tmpDateStr, tmpFilePart: String;
      tmpDate1, tmpDate2: TDateTime;
    Begin
      psExitCode:= 1;
      // ... add your code here
      // PH_20230423.pdf > PH_Sun_20230422.pdf
      tmpDateStr := psFileName;
      tmpDateStr := psStringReplace(tmpDateStr, '.pdf', '');
      tmpLen := Length(tmpDateStr);
      If tmpLen >= 8 Then
      Begin
        tmpDateStr := Copy(tmpDateStr, tmpLen - 8 + 1, 8);
        tmpDate1 := psStrToDate(tmpDateStr, 'YYYYMMDD', #0);
        Case DayOfWeek(tmpDate1) of
        1: // Sunday
          tmpDate2 := psIncDay(tmpDate1, -1);
        2: // Monday
          tmpDate2 := psIncDay(tmpDate1, -2);
        3: // Tuesday
          tmpDate2 := psIncDay(tmpDate1, -3);
        4: // Wednesday
          tmpDate2 := tmpDate1;
        5: // Thursday
          tmpDate2 := psIncDay(tmpDate1, -1);
        6: // Friday
          tmpDate2 := tmpDate1;
        7: // Saturday
          tmpDate2 := psIncDay(tmpDate1, -1);
        Else
          psExitCode := 0; // Error
        End;
        psVSA := FormatDateTime('DDD', tmpDate1);
        psVSB := FormatDateTime('YYYYMMDD', tmpDate2);
        psLogWrite(1, '', 'Date: ' + psVSB + ', Day: ' + psVSA);
      End
      Else
        psLogWrite(1, '', 'Length error of ' + tmpDateStr);
    End.

    Limagito File Mover Pascal Script Day of Week

    • Afterwards, adjust the File Rename option in your Destination setup:
    RegEx:  (.*)_(.*)\.(.*)
    Replacement:  \1_%VSA_%VSB.\3
    Limagito File Mover File Rename using RegEx
    – RunTime Log Result:
    Limagito File Mover RunTime Log

    #FileTransfer

    If you need any info about this  ‘day of week function’, please let us know.

    Best regards,

    Limagito Team

  • 10 Feb

    Move and change content of a txt-file

    Q: Hello Limagito, Now I need your help again…

    I got a TXT-file delivered from my customer with approximately 250 rows.

    In the txt file I like “search and replace” a text string :

    ;19;18;;

    Should be replaced with

    ;36;35;;

    And the string

    ;21;21;;

    Should be replaced with

    ;37;37;;

    I also tried to set the powershell exe file in the field for Application name (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)

    Is there any other way?

    A: Yes there is, we can do this easily with our Pascal Script so no need for external scripts if you want.

    In our example we’ll use a Windows folder as Source:

    Limagito File Mover WIN as Source

    We only want to handle .txt file so we added the following Filename Include Filter:

    Limagito File Mover Include Filename Filter

    As Destination we added a Pascal Script. This script will does the work you requested.

    Limagito File Mover Pascal Script as Destination

    • Adjust ctOutputPath > must end with a \
    • Encoding of destination text file can be adjusted

     

    Var
      tmpFileIn, tmpFileOut: String;
      tmpList: TStringList;
    Const
      ctOutputPath = 'C:\Test\Out_Txt\';
    Begin
      // Init var
      psExitCode:= 0;
      tmpFileIn := psFilePath + psFileName;
      tmpFileOut := ctOutputPath + psFileName; 
      // ... add your code here
      tmpList := TStringList.Create;
      Try
        Try
          tmpList.LoadFromFile(tmpFileIn);
          tmpList.Text := psStringReplace(tmpList.Text, ';19;18;;', ';36;35;;');
          tmpList.Text := psStringReplace(tmpList.Text, ';21;21;;', ';37;37;;');
          // https://docs.microsoft.com/nl-be/windows/win32/intl/code-page-identifiers
          If psSaveTxt2File(tmpList.Text, ctOutputPath + psFileName, 'ISO-8859-1', False) Then
          Begin
            psExitCode := 1;
            psLogWrite(1, '', 'SaveToFile ' + tmpFileOut + ' Successful');    
          End
          Else
            psLogWrite(1, '', 'SaveToFile ' + tmpFileOut + ' Error');
        Except
          psLogWrite(1, '', 'LoadFromFile ' + tmpFileIn + ' Exception');
        End;
      Finally
        tmpList.Free;
      End;  
    End.

    Limagito File Mover Pascal Script as Destination

    Feedback Customer:

    As always you save the day! The Pascal script did my work easily. Some day I have to learn more about pascal!

    I cheat some in powershell and some visual basic, but this stuff is powerful!

    By the way, it only worked in the newer version of Limagito (I still have some workflows in the old version)

    Thank you for help, quick and accurate, as always!

    #FileTransfer #Encoding

    If you need any info about this ‘change content of a txt-file’ request, please let us know.

    Best regards,

    Limagito Team

1 6 7 8 9 10 11 12 26
SEARCH