Pascal Script

  • 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
  • 04 Aug

    Combine csv files to one and add a column to the end of each line

    Q: Combine csv files to one and add a column to the end of each line.

    1. I have some .CSV files that we receive and want to combine them all into 1 file.
    2. We should also add a column to the end of each line, a new piece of data for each record. The extra data to add will depend on the filename of the cvs file.
      • If the csv filename contains FILEID1 then we should add FILEID1-DATA.
      • If the csv filename contains FILEID2 then we should add FILEID2-DATA.

    Is this possible?

    A: Yes, this is possible using some custom Pascal Script we created for you.

    • Please open our Pascal Script option:

    Limagito File Mover Pascal Script

    • Add and enable the following ‘On Rule Begin’ Pascal Script. This script will clear string var ( psVSA and psVSE ) when the rule begins.
    Begin
      psExitCode:= 1;
      // ... add your code here
      psVSA := '';
      psVSE := '';
    End.

    Limagito File Mover On Rule Begin Pascal Script

    • Add and enable the following ‘On Rule End’ Pascal Script. Do not forget to adjust the combined output filename.
    Var
      tmpList: TStringList;
    Const
      ctOutputFile = 'C:\Test\Out_Csv\MergeCsvFile.csv';
    Begin
      // Init
      psExitCode:= 1;
      // ... add your code here
      If psVSE = '' Then
      Begin
        // No Error Occured
        If Trim(psVSA) <> '' Then
        Begin
          tmpList := TStringList.Create;
          Try
            tmpList.CommaText := psVSA;
            Try
              tmpList.SaveToFile(ctOutputFile);
            Except
              Begin
                psExitCode := 0;
                psLogWrite(1, '', 'Save To File Exception: ' + ctOutputFile);
              End;
            End;
          Finally
            tmpList.Free;
          End;
        End
        Else
          psLogWrite(1, '', 'No csv data to save');
      End
      Else
      Begin
        // Error Occured
        psLogWrite(1, '', 'Error handling csv file(s): ' + psVSE);
      End;
    End.

    Limagito File Mover On Rule End Pascal Script

    • Add ‘Pascal Script’ as Destination

    Limagito File Mover Destinations

    Var
      iList: Integer;
      tmpCount: Integer;
      tmpList, tmpListMem: TStringList;
      tmpAddData, tmpEntry: String;
    Const
      ctAddHeader = ',"D"';
    Begin
      If psVSE <> '' Then
      Begin
        psExitCode:= 0;
        psLogWrite(1, '', 'Skip File due to previous error: ' + psVSE);
      End
      Else
        psExitCode:= 1;
      // Set AddData
      If pos('FILEID1', UpperCase(psFileName)) > 0 then
        tmpAddData := 'FILEID1-DATA'
      Else if pos('FILEID2', UpperCase(psFileName)) > 0 then
        tmpAddData := 'FILEID2-DATA'
      Else
        tmpAddData := '';
      // Load From File
      tmpList := TStringList.Create;
      tmpListMem := TStringList.Create;
      Try
        If Trim(psVSA) <> '' Then
          tmpListMem.CommaText := psVSA;
        Try
          psLogWrite(1, '', 'Load From File: ' + psFilePath + psFileName);
          tmpList.LoadFromFile(psFilePath + psFileName);
          tmpCount := tmpList.Count;
          If tmpCount > 0 Then
          Begin
            For iList := 0 to (tmpCount - 1) Do
            Begin
              tmpEntry := tmpList.Strings[iList];
              If iList = 0 Then
              Begin
                // Header
                If Trim(tmpEntry) <> '' Then
                  tmpListMem.Add(tmpEntry + ctAddHeader);
              End
              Else
              Begin
                // Data
                If Trim(tmpEntry) <> '' Then
                  tmpListMem.Add(tmpEntry + tmpAddData);
              End;
            End;
            // Update psVSA
            If Trim(tmpListMem.CommaText) <> '' Then
              psVSA := tmpListMem.CommaText;
          End;
        Except
          Begin
            psExitCode := 0;
            psVSE := 'LoadFromFile ' + psFilePath + psFileName + ' Exception';
          End;
        End;
      Finally
        tmpList.Free;
        tmpListMem.Free;
      End;
    End.

    Limagito File Mover combine csv files Pascal Script

    If you need any help with this ‘Combine csv files’ request, please let us know.

    Best Regards,

    Limagito Team

    #filetransfer #filemanagement

    By Limagito-Team CSV Pascal Script
1 2 3 4 17