ReNamer:Pascal Script:SplitPath

From den4b Wiki
Revision as of 09:15, 17 August 2011 by Stefan (talk | contribs) (Tried some formating :-()
Jump to: navigation, search

Find the build-in functions there > http://www.den4b.com/wiki/ReNamer:Pascal_Script:Functions#File_Name_Utilities

That are:

For e.g. "C:\GreatGrand\GrandParent\ParentFolder\file.ext"

The always available variable 'FilePath' and 'FileName':

Variable provides
FilePath C:\GreatGrand\GrandParent\ParentFolder\file.ext
FileName file.ext



And this functions to extract parts:

Function provides
WideExtractFilePath C:\GreatGrand\GrandParent\ParentFolder\
WideExtractFileDir C:\GreatGrand\GrandParent\ParentFolder
WideExtractFileDrive C:\
WideExtractFileName file.ext
WideExtractBaseName file
WideExtractFileExt ext




But this functions didn't gave all possibilities to split an full path into all wanted parts.
You have to know how to handle this functions and/or use own code to achieve what you want.

Here are some code snippets for this issue:

First an 'trick' seen by Denis:

nested Functions provides
WideExtractFileName(WideExtractFileDir(FilePath)); ParentFolder
WideExtractFileName(WideExtractFileDir(WideExtractFileDir(FilePath))); GrandParent
WideExtractFileName(WideExtractFileDir(WideExtractFileDir(WideExtractFileDir(FilePath)))); GreatGrand


Use this like:

var 
  ParentFolder, GrandParent, GreatGrandParent: WideString;
begin 
  ParentFolder := WideExtractFileName(WideExtractFileDir(FilePath));
  GrandParent := WideExtractFileName(WideExtractFileDir(WideExtractFileDir(FilePath)));
  GreatGrandParent := WideExtractFileName(WideExtractFileDir(WideExtractFileDir(WideExtractFileDir(FilePath))));
  FileName := GreatGrandParent +'-'+GrandParent + '-'+ParentFolder + '-' + FileName;
end.




Here is an another way by splitting the path at the back slash into an array 'Folders':


var 
  Folders: TStringsArray; 
  oldPath, ParentFolder, GrandParentFolder, GrandGrandParentFolder, TopMostFolder, SecondTopMostFolder: WideString;

begin 

    // Get parts of the current file path:
  oldPath := WideExtractFileDir(FilePath);
  Folders := WideSplitString(oldPath, '\'); 
  TopMostFolder          := Folders[1];
  SecondTopMostFolder    := Folders[2];
  GrandGrandParentFolder := Folders[Length(Folders)-3];
  GrandParentFolder      := Folders[Length(Folders)-2];
  ParentFolder           := Folders[Length(Folders)-1];

  FileName := SecondTopMostFolder +'-'+GrandParent + '-'+ParentFolder + '-' + FileName;
end.




And there is Regular Expression to extract the parts:


  Parent := ReplaceRegEx(FilePath, '.+\\(.+)\\.+', '$1', False, True);
  GrandPa := ReplaceRegEx(FilePath, '.+\\(.+)\\.+\\.+', '$1', False, True);
  GrandGrandPa := ReplaceRegEx(FilePath, '.+\\(.+)\\.+\\.+\\.+', '$1', False, True);

But note that RegEx is slow by its nature.




And there are Meta Tags to extract e.g. the parent folder

ParentFolder := CalculateMetaTag(FilePath, ':File_FolderName:');

See 'Insert' Rule and click there at 'Insert Meta Tag'




To split file name into parts at an delimiter we can use f.ex.:

E.g. for
FROM:
"my fav artist - title album song.mp3"
TO:
"My Fav Artist - Title album song.mp3"
Use:

var  
  Delimiter, Extension, Part1, Part2, Part2Char1, Part2Rest: WideString;
  PosOfDelimiter: Integer;
begin
  Delimiter := '-';
  PosOfDelimiter := Pos(Delimiter, FileName);

  if (PosOfDelimiter > 0) then
    begin
       Extension  := WideExtractFileExt(FileName)
       Part1      := WideCopy(WideExtractBaseName(FileName), 1, PosOfDelimiter -2);
       Part2      := WideCopy(WideExtractBaseName(FileName), PosOfDelimiter +2, Length(FileName));
       Part2Char1 := WideCopy(Part2, 1, 1);
       Part2Rest  := WideCopy(Part2, 2, Length(Part2)-1);

       //ShowMessage('Debug: #' + Part1 + '#' + Part2 + '#'  + Part2Char1 + '#' + Part2Rest + '#');

       FileName := WideCaseCapitalize(Part1)
                  + ' ' + Delimiter + ' '
                  + WideUpperCase(Part2Char1) + WideLowerCase(Part2Rest)
                  + WideUpperCase(Extension);
    end;
end.