File Mover Blog

May 26, 2023

Get a field value to rename a csv file

Q: Get a field value to rename a file. I received a file with a specific filename every week which goes through various processes, after I have finished I need to send the original data with some extra data columns generated by our process back to the client using the original filename. Unfortunately, I cannot keep hold of the filename throughout the process but I can have a column in the return file which contains the original filename. Is there a way I can use Filmover to have a look at a specific column of a data file get the contents of it and use it to rename the file to that value then deleted the column that I got the information from? Bit complex but I thought I’d ask before exploring other avenues. The column that contains the original filename to be used and then deleted is called IMPNAME.

A: Should be possible (we received an example file of the customer).

  • Please set filename filter to *.csv

limagito file mover filename filter

  • Add ‘Pascal Script’ as Destination:

limagito file mover pascal script as destination

  • Don’t forget to adjust the ctOutputPath Const (must end with a \ )
Var
  iEntry, iIndex, iList: Integer;
  tmpFilename, tmpInputFile, tmpOutputFile: String;
  tmpEntryList, tmpFileList: TStringList;
Const
  ctOutputPath = 'C:\Test\Out_Csv\';  // Must end with a \
  ctIMPNAME = 'IMPNAME';
Begin
  psExitCode := 0;
  iIndex := -1;
  tmpInputFile := psFilePath + psFilename;
  tmpOutputFile := '';
  // ... add your code here
  tmpEntryList := TStringList.Create;
  tmpEntryList.Delimiter := ',';
  tmpEntryList.StrictDelimiter := True;
  tmpEntryList.QuoteChar := '"';
  tmpFileList := TStringList.Create;
  Try
    Try
      tmpFileList.LoadFromFile(tmpInputFile);
      tmpEntryList.DelimitedText := tmpFileList.Strings[0];
      For iEntry := 0 to (tmpEntryList.Count - 1) Do
      Begin
        If SameText(ctIMPNAME, tmpEntryList.Strings[iEntry]) Then
        Begin
          iIndex := iEntry;
          psLogWrite(1, '', 'Found ' + ctIMPNAME + ' at Column with Index ' + IntToStr(iIndex));
          Break;
        End;
      End;
      // Did we find 'IMPNAME'
      If iIndex <> -1 Then
      Begin
        tmpFileList.Strings[0] := psStringReplace(tmpFileList.Strings[0], ',"' + ctIMPNAME + '"', '');
        For iList := 1 to (tmpFileList.Count - 1) Do
        Begin
          tmpEntryList.DelimitedText := tmpFileList.Strings[iList];
          If tmpEntryList.Count > iIndex Then
          Begin
            If tmpOutputFile = '' Then
            Begin
              tmpFilename := tmpEntryList.Strings[iIndex];
              If tmpFilename <> '' Then
                tmpOutputFile := ctOutputPath + tmpFilename + '.csv';
            End;  
            If tmpFilename <> '' Then
            Begin
              tmpFileList.Strings[iList] := psStringReplace(tmpFileList.Strings[iList], ',"' + tmpFileName + '"', '');           
              psLogWrite(1, '', 'Entry: ' + tmpOutputFile + ' : ' + tmpFileList.Strings[iList]);
            End;  
          End;
        End;
      End;
      // Save To File
      If tmpOutputFile <> '' Then
      Begin
        Try
          tmpFileList.saveToFile(tmpOutputFile);
          psExitCode := 1;
        Except
          psLogWrite(1, '', 'Error saving file: ' + tmpOutputFile);
        End;
      End;
    Except
      psLogWrite(1, '', 'Error loading file: ' + tmpInputFile);
    End;
  Finally
    tmpEntryList.Free;
    tmpFileList.Free;
  End;
End.

Get a field value

 

#filetransfer #mft #filemanagement #csv

If you need any info  about this ‘get a field value to rename a file’, please let us know.

Best regards,

Limagito Team

By Limagito-Team CSV , Share:
SEARCH