ReNamer:Pascal Script:SplitPath

From den4b Wiki
Revision as of 17:20, 17 August 2011 by Den4b (talk | contribs) (cleanup note added)
Jump to: navigation, search
{{{iparam}}} This article needs to be cleaned up!

Find the build-in functions here: 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



Use this e.g. like:

var 
  vPath, vDir, vDrive, vName, vBase, vExt, vE: WideString;
  vOUT: WideString; 
begin
 
  //extract the parts and store them into an var each:
  vPath  := WideExtractFilePath(FilePath);
  vDir   := WideExtractFileDir(FilePath);
  vDrive := WideExtractFileDrive(FilePath);
  vName  := WideExtractFileName(FilePath);
  vBase  := WideExtractBaseName(FilePath);
  vExt   := WideExtractFileExt(FilePath);
  vE     := WideReplaceStr( vExt, '.', '');

  // Test output as MsgBox:
  vOUT  := 'Default build-in vars:'             + #13#10
        + 'FilePath >>> '            + FilePath + #13#10
        + 'FileName >>> '            + FileName + #13#10
        + #13#10
        + 'Extracted by using functions:'       + #13#10
        + 'WideExtractFileDrive >>> '+ vDrive   + #13#10
        + 'WideExtractFileDir >>> '  + vDir     + #13#10
        + 'WideExtractFilePath >>> ' + vPath    + #13#10
        + 'WideExtractFileName >>> ' + vName    + #13#10
        + 'WideExtractBaseName >>> ' + vBase    + #13#10
        + 'WideExtractFileExt >>> '  + vExt     + #13#10
        + 'And without the dot >>> ' + vE;
       ShowMessage( vOUT );
end.





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.