subtracting

  • 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 ,
SEARCH