Pascal Script

  • 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

    By Limagito-Team Pascal Script ,
  • 20 Jan

    How-To Convert XLS Files using Pascal Script, Example 1

    https://limagito.com/converting-xls-files/

    First example: convert a xls(x) file to a tabbed txt file

    1) Add the following ‘Filename Include Filter’ so we are 100% sure we’ll only handle xls(x) files

    2) Add PS (Pascal Script) as Destination

    3) Add the following Pascal Script

    Const
      CsvPath = 'C:\Test\OUT_XLS\';
    Begin
      psExitCode := 0;
      // ... add your code here
      // 2 = Text based file, #09 is tab delimited, 0 is UTF8 text encoded
      If psConvertXlsFile(psFilePath + psFileName, CsvPath + ChangeFileExt(psFileName, '.txt'), 2, #09, 0, 'Run results') Then
      Begin
        psExitCode := 1;
        psLogWrite(1, '', 'Converted ' + psFilePath + psFileName);
      End
      Else
        psLogWrite(1, '', 'Convert error ' + psFilePath + psFileName);
     
      // psConvertXlsFile(psFilePath + psFileName, CsvPath + ChangeFileExt(psFileName, '.csv'), 2, ',', 0, 'Run results');
    End.
  • 19 Jan

    How-To Convert XLS Files using Pascal Script

    Dear Users,

    We got a request to add an option to convert xls(x) to csv and txt. It should work without Excel being installed. All should be native code.

    This options was added as a Pascal Script function in version v2019.1.19.0.

    – Use psConvertXlsFile to convert xls(s) files to xls(x), csv, txt, …

    Function psConvertXlsFile(SrcFile, DstFile: String; Format: Integer; Delimiter: Char; Encoding: Integer; Sheet: String): Boolean;

    SrcFile = Source File (FilePath + filename)
    DstFile = Destination File (FilePath + Filename)
    Format = Destination File format

    • 0 = Automatic Automatically detect the type of the file when opening files.
    • 1 = Xls Excel 97-2000-XP-2003
    • 2 = Text Delimiter separated values. Depending on the delimiter, this can be csv, tab delimited text, etc.
    • 3 = Pxl Pocket Excel 1.0 or 2.0
    • 4 = Xlsx Excel 2007 standard file format. Note that this is *not* a macro enabled file format. If you want to save a file with macros, you need to use Xlsm instead.
    • 5 = Xlsm Excel 2007 macro enabled file format.

    Delimiter = Delimiter Char to use if Format Text ( #09 = Tab )
    Encoding = Encoding for the generated file, when writing a Text-delimited file (csv or txt). This parameter has no effect on xls/x files.

    • 0 = UTF8
    • 1 = UTF7
    • 2 = Unicode
    • 3 = Default
    • 4 = BigEndianUnicode
    • 5 = ASCII
    • 6 = ANSI

    Sheet = Name of the sheet that needs to be converted

    Pascal Script Examples:

    i.e. Convert the sheet Results of a xls(x) file to a tab delimited txt file. Destination will be a UTF8 encoded text file.

    Const
      CsvPath = 'C:\Test\OUT_XLS\';
    Begin
      psExitCode := 0;
      // ... add your code here
      If psConvertXlsFile(psFilePath + psFileName, CsvPath + ChangeFileExt(psFileName, '.txt'), 2, #09, 0, 'Results') Then psExitCode := 1;
    End.

    i.e. Convert the sheet Results of a xls(x) file to a csv delimited txt file. Destination will be a UTF8 encoded text file.
    Const

    Const
      CsvPath = 'C:\Test\OUT_XLS\';
    Begin
      psExitCode := 0;
      // ... add your code here
      If psConvertXlsFile(psFilePath + psFileName, CsvPath + ChangeFileExt(psFileName, '.csv'), 2, ',', 0, 'Results') Then psExitCode := 1;
    End.

    Don’t hesitate to contact us if you need any information about this new function.

    Regards,
    Limagito Team

    By Limagito-Team Pascal Script XLS/CSV , , , ,
1 24 25 26 27 28 29
SEARCH