Pascal Script

  • 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

  • 23 Oct

    Need help with subtracting date from a filename

    Q: Need help with subtracting date from a filename. My filename is like: 20221023_Pg_01.pdf (YYYYMMDD ) and I need to subtract 1 day and rename it so that it’s 2022-10-22_section_P01.pdf where it’s YYYYMMDD.  My other dilemma is what will I do for a case like 20221001_Pg_01.pdf?

    A: Yes this is possible using some Pascal Script. We’ve attached some screenshots to get you started.

    • We are using a Windows Folder a Source

    Windows folder as Source

    • Open the ‘Pascal Script’ option

    Limagito File Mover Pascal Script

    • Enable and set the following ‘On Destinations’ Pascal Script:
    Var
      tmpDate: TDateTime;
      tmpDateStr: String;
      tmpPos: Integer;
      tmpLen: Integer;
    Const
      ctDateLen = 8;
     
    Function GetDateFromString(const aInput: String): TDateTime;
    var
      wYear, wMonth, wDay: Word;
    Begin
      wYear := StrToInt(Copy(aInput, 1, 4));
      wMonth := StrToInt(Copy(aInput, 5, 2));
      wDay := StrToInt(Copy(aInput, 7, 2));
      Try
        Result := EncodeDate(wYear, wMonth, wDay);
      Except
        Result := 0;
      End;
    End;
     
    Begin
      psExitCode := 0;
      (*
      My filename is like: 20221023_Pg_01.pdf (YYYYMMDD ) and I need to subtract 1 day
      and rename it so that it’s 2022-10-22_section_P01.pdf where it’s YYYYMMDD.
      My other dilemma is what will I do for a case like 20221001_Pg_01.pdf?
      *)
      // ... add your code here
      tmpPos := pos('_', psFileName);
      tmpLen := Length(psFileName);
      // Check
      If (tmpLen > ctDateLen) And (tmpPos = 9) Then
      Begin
        Try
          tmpDateStr := Copy(psFileName, 1, ctDateLen);
          psLogWrite(1, '', 'Date stripped as String: ' + tmpDateStr);
          tmpDate := GetDateFromString(tmpDateStr);
          tmpDate := psIncDay(tmpDate, -1);
          psVSA := FormatDateTime('yyyy-mm-dd', tmpDate);
          psLogWrite(1, '', 'Date Update as String: ' + psVSA);
          // Set ExitCode
          psExitCode := 1;
        Except
          psLogWrite(1, '', 'Exception Error when searching for Date in filename: ' + psFilePath + psFileName);
        End;
      End
      Else
        psLogWrite(1, '', 'Not a valid filename: ' + psFilePath + psFileName);
    End.

    Pascal Script

    The above script will strip the datepart from the filename. We’ll substract one day from this datepart and put the result (in the requested format) in the psVSA var (%VSA).

    • Please enable and adjust the File Renaming option in you Destination:

    Destination File Renaming

    • File Rename Setup:

    RegEx:  (.*)_(.)(.*)_(.*)\.(.*)

    Replacement:  %VSA_section_\2\4.\5

    Destination File Renaming option

    #Filetransfer #Script

    If you need any help with this ‘subtracting date from a filename’ option, please let us know.

    Best Regards,

    Limagito Team

    By Limagito-Team Pascal Script ,
  • 01 Oct

    I have a task that needs to grab sets of files

    Q: I have a task that needs to grab sets of files. Pdf files that have “A01” and “A06” OR files that have “A01” and A08” in their filename. Could this be accomplished?

    A: So you would like us to check if pdf file A08 is available and if so then grab pdf files with A01 and A08 in their filename. If pdf file A08 is not available check the same but then using A06 in the filename.  Meaning a set of files and grabbing the first and last page pdf file. Yes, this is possible.

    • As Source we will use a windows folder. This folder will contain the pdf files.

    Limagito File Mover Source Setup

    • In the ‘File Filter’ we are going to use the ‘File Name Include’ filter option. Add the parameter %VSA to this filter. This %VSA (Var String A) will get its value from  a script that we will add in the next step.

    Limagito File Mover File Filter Setup

    • In the ‘Advanced’ tab of the ‘File Filter Setup’  we need to enable ‘Allow parameters in File Name Filter’.

    Limagito File Mover File Filter Advanced

    • Next, please open the ‘Pascal Script’ option:

    Limagito File Mover Pascal Script

    • Enable and add the following ‘On Rule Begin’ script:
    Begin
      psExitCode:= 0;
      // ... add your code here
      psLogWrite(1, '', 'Source Path: ' + psSourcePath);
      If psCountFiles(psSourcePath, '*A08*.pdf', False) >= 1 Then
      Begin
        psLogWrite(1, '', 'Found A08');
        psVSA := '*A01*.pdf;*A08*.pdf';
        psExitCode:= 1;
      End
      Else
      Begin
        If psCountFiles(psSourcePath, '*A06*.pdf', False) >= 1 Then
        Begin
          psLogWrite(1, '', 'Found A06');
          psVSA := '*A01*.pdf;*A06*.pdf';
          psExitCode:= 1;
        End;      
      End;
    End.

    Limagito File Mover Pascal Script Setup

    • As Destination we used a Windows folder:

    Limagito File Mover Destination Setup

    • First test with 6 pdf files in the Source, A01 to A06

    • RunTime log result, only pdf file A01 and A06 are copied:

    Limagito File Mover RunTime log

    • Second test with 6 pdf files in the Source, A01 to A08

    • RunTime log result, only pdf file A01 and A08 are copied:

    Limagito File Mover RunTime Log

    If you need any help with this ‘grab sets of files’ request, please let us know.

    Best Regards,

    Limagito Team

    #filetransfer #filemanagement

    By Limagito-Team Pascal Script
1 7 8 9 10 11 12 13 26
SEARCH