Pascal Script

  • 17 Jul

    Convert character set of text file

    Q: Convert character set of text file. I think I need some help here. We connect to a SFTP-server and download 3 files. One of these are a UTF-8 file named Vyymmdd.Thhmmss (i.e. V220627.TT110759). We need to download all 3 files, and convert the UTF-8 file to ISO-8859-1. I have found this article: https://limagito.com/text-character-encoding-conversion/

    And edited like this : psChangeTxtEncodingExt(psFilePath + psFileName, ctOutputpath + psFileName, 65001, 28591, True)

    Runtime Log says the file was successfully converted. But if I open the file in Notepad++ and click the “Encoding” option on the menu, it highlights UTF-8.

    A: In version v2022.7.17.0 we added some extra functions to help you convert the character set of  text files:

    Function psChangeTxtEncodingCk(Source, Destination: String; SrcCharset, DstCharset: String; WriteBOM: Boolean; ErrorAction: Integer): Boolean;

    Do not use this function when Source and Destination file are the same, use psChangeTxtEncodingCkExt instead.

    • ErrorAction: Controls how errors are handled. When a character in the input data cannot be converted to the target charset, the action taken is controlled by this property.
      The possible settings are:
      (0) drop the error characters,
      (1) substitute the data set by a ?
      (2) convert to a hex-escaped string (&#xXXXX)
      (3) RESERVED
      (4) RESERVED
      (5) RESERVED
      (6) RESERVED
      (7) Pass non-convertible characters to the output unchanged.
    • The Source and Destination Charset can be any of the supported encodings listed at Chilkat Supported Character Encodings.

    Function psChangeTxtEncodingCkExt(Source, Destination: String; SrcCharset, DstCharset: String; WriteBOM, WriteFileIfModified: Boolean): String;

      • WriteFileIfModified: writes the contents to a file, but only if it is a new file or if the contents are different than the existing file.
      • WriteBOM: the BOM (also known as a preamble), is emitted for charsets that define a BOM (such as utf-8, utf-16, utf-32, etc.)
      • The Source and Destination Charset can be any of the supported encodings listed at Chilkat Supported Character Encodings.

    #Filetransfer #Filemanagement #Encoding

    If you need any info about this ‘Convert character set’ question, please let us know.

    Best regards,

    Limagito Team

  • 04 Jun

    Execute Command, continuing on error

    Q: Execute Command, continuing on error. I have a rule which moves a file from one win folder to another win folder and then runs a CMD against it. If the CMD errors out it keeps retrying the file. How do I move the failed file to another folder on the error and NOT retry it with the command line?
    My current setup is:

    • WIN as Source
    • Destinations:
      1. WIN
      2. CMD (Execute Command)

    A: In our latest version v2022.6.2.0 we added a new ‘Redirect ExitCode to PS Integer Var A (%VIA)’ option. Please check the ‘Advanced Tab’ of the CMD (Command) as Destination. Enable this option to transfer the ExitCode to our Pascal Script Integer Var psVIA. We’ll use the psVIA var to move the source file in case the Command returns a value <> 0 (which normally is an errorcode).

    Limagito File Mover continuing on error

    We will need a third ‘Pascal Script’ as Destination.

    1) WIN as Destination
    2) CMD as Destination
    Now please add a third ‘Pascal Script’ Destination. This must be in the last position. Do not forget to adjust the Const ctErrorPath value. This Pascal Script will move the source file to an error path if the ExitCode of the CMD (command) <> 0.

    Limagito File Mover Destination Setup

    Const
      ctErrorPath = 'C:\3ProcessError\'; // must end with a /
    Begin
      psExitCode:= 1;
      // ... add your code here
      If psVIA <> 0 Then
      Begin
        psMoveFile(psFilePath + psFileName, ctErrorPath + psFileName);
        psLogwrite(1, '', 'CMD ExitCode Error, psVIA: ' + IntToStr(psVIA));
      End;
    End.
    Limagito File Mover Pascal Script as Destination

    #Filetransfer #Filemanagement

    If you need any info about this ‘continuing on error’ request, please let us know.

    Best regards,

    Limagito Team

  • 26 May

    Move 1 file to 1 location and another file to a different location

    Q: Is it possible to move 1 file to 1 location and another file to a different location but only when a file with a specific extension is present? I’m processing a job where I create a data file with print information and a PDFfile,  in the Main location there will be .csv file

    Main Location

    .csv

    At some point a pdf file will appear in the main location i.e.

    .csv

    .pdf

    Can I create a rule where

    1. It only starts when a .pdf is present
    2. Moves the .pdf file to 1 location and the .csv file to a different location?

    A: Yes this is possible using a bit of Pascal Script. We added some screenshots from an example to get you started.

    • We added a local Windows folder as Source:

    Limagito File Mover Source Setup

    • Important is to add the following FileName Include Filters. We’ll scan for ‘*.csv’ and ‘*.pdf’ files.

    Limagito File Mover Filename Include Filter

    • Open the Pascal Script option:

    Limagito File Mover Pascal Script option

    • Enable and Add the following ‘On Rule Begin’ Pascal Script:
    Begin
      psExitCode:= 1;
      // ... add your code here
      psVSA := '';
    End.

    Limagito File Mover Pascal Script

    • Enable and Add the following ‘On Destination’ Pascal Script:
    Var
      tmpExt: String;
      tmpFile: String;
    Begin
      psExitCode := 0;
      // ... add your code here
      tmpExt := ExtractFileExt(psFileName);
      // Check for pdf if csv exists
      If tmpExt = '.csv' Then
      Begin
        tmpFile := psStringReplace(psFileName, tmpExt, '.pdf');
        If FileExists(psFilePath + tmpFile) or (Pos(psFilePath + tmpFile, psVSA) <> 0) Then
        Begin
          psLogWrite(1, '', 'File ' + tmpFile + ' Exists');
          psVSA := psVSA + ';' + psFilePath + psFileName;
          psExitCode := 1;
        End;
      End;
      // Check for csv if pdf exists
      If tmpExt = '.pdf' Then
      Begin
        tmpFile := psStringReplace(psFileName, tmpExt, '.csv');
        If FileExists(psFilePath + tmpFile) or (Pos(psFilePath + tmpFile, psVSA) <> 0)  Then
        Begin
          psLogWrite(1, '', 'File ' + tmpFile + ' Exists');
          psVSA := psVSA + ';' + psFilePath + psFileName;
          psExitCode := 1;
        End;
      End;
    End.

    Limagito File Mover Pascal Script

    • Enable and Add the following ‘On Destinations‘ Pascal Script:
    Begin
      psExitCode := -1;
      // .csv files to Destination ID1
      If (psDestinationID = 'ID1') And (ExtractFileExt(psFileName) = '.csv') Then
        psExitCode := 1;
      // .pdf files to Destination ID2    
      If (psDestinationID = 'ID2') And (ExtractFileExt(psFileName) = '.pdf') Then
        psExitCode := 1;
    End.
    

    Limagito File Mover another file to a different location

    • Add two Destinations. Important, the first Destination (ID1) should receive hte csv files and the second Destination (ID2) should receive the pdf files.

    Limagito File Mover Destination Setup

    • RunTime Log Result. The Csv file goes to the first Destination and only when a pdf file with the same name exists. The pdf file itself goes to the second Destination.

    Limagito File Mover RunTime Log

    #Filetransfer #Filemanagement

    If you need any info about this request, please let us know.

    Best regards,

    Limagito Team

    By Limagito-Team Pascal Script WIN
1 9 10 11 12 13 14 15 26
SEARCH