File Mover Blog

August 1, 2020

Q&A 22: move a file and rename the file so it includes the directory name

Q: I’m trying to move a file that’s within 1 directory and rename the file so that it includes the directory name

Source: C:\Source1\File.txt

Destination: C:\Destination\Source1_File.txt

I want it to take the directory name that it is contained in, then string it to the front of the filename, but move it to it’s destination C:\Destination\.

We would only want it to pertain to the previous directory by 1 level.

  • C:\Source1\File1.txt => C:\Destination\Source1_File1.txt
  • C:\Source1\Sub\File2.txt => C:\Destination\Sub_File2.txt

A: Yes this is possible using some ‘Pascal Script’

Let’s start with the Source, we added a Windows folder:

LimagitoX file Mover Windows Folder as Source

In the ‘Directory Filter’ setup we enabled include scanning of subdirectories (optional, is not a must):

LimagitoX file Mover Include Subdirectories

Destinations setup, important the first destination must be a ‘Pascal Script’:

LimagitoX file Mover Destination Setup

First Destination should be a ‘Pascal Script’. We used ‘Source1’ as default PreFix for the Source Root Folder (you can adjust this):

Var
  tmpPos: Integer;
  tmpSubDir: String;
Const
  ctRootPrefix = 'Source1';
Begin
  psExitCode:= 1;
  // ... add your code here
  psVSA := '';
  tmpSubDir := '%SFS';
  tmpPos := pos('\', tmpSubDir);
  If tmpPos<>0 Then
  Begin
    psVSA := Copy(tmpSubDir, tmpPos + 1, Length(tmpSubDir) - tmpPos);
    psLogWrite(1, '', 'Stripped Prefix from SubDir: ' + tmpSubDir + ' : ' + psVSA);  
  End
  Else
    psVSA := ctRootPrefix;   
End.

LimagitoX file Mover Pascal script

Second Destination is a windows folder where the source files should be moved/copied to:

LimagitoX file Mover Windows Folder as Destination

Enable and setup File Renaming in the Windows Destination:

LimagitoX file Mover File Renaming

File Renaming setup:

  • RegEx:   (.*)
  • Replacement:   %VSA_\1

LimagitoX file Mover File Renaming Setup

RunTime Log result:

LimagitoX file Mover RunTime Log

Output Folder result:

If you need any help , please let us know.
Regards,
Limagito Team
By Limagito-Team Q&A , Share:
SEARCH