Difference between revisions of "ReNamer:Pascal Script:SplitPath"

From den4b Wiki
Jump to navigation Jump to search
m (cleanup note added)
(Clean up)
Line 1: Line 1:
 
{{Cleanup}}
 
{{Cleanup}}
  
Find the build-in functions here: [[ReNamer:Pascal_Script:Functions#File_Name_Utilities|File Name Utilities]].
+
Find an overview of all build-in functions there >> [[ReNamer:Pascal_Script:Functions#File_Name_Utilities|File Name Utilities]].
  
That are:
 
  
For e.g. "C:\GreatGrand\GrandParent\ParentFolder\file.ext"
+
Here on this page we show you only the needed functions to extract parts<BR>
 +
of the file name and how to use them in an PascalScript for ReNamer.
 +
<BR>
  
The always available variable 'FilePath' and 'FileName':<BR>
+
 
 +
We like to show you this on an example file name<BR>
 +
"C:\GreatGrand\GrandParent\ParentFolder\file.ext"
 +
<BR>
 +
<BR>
 +
<BR>
 +
 
 +
First, there are the always available variables 'FilePath' and 'FileName'<BR>
 +
You don't have to declare ('var' / 'dim') or initialize ('var="";') this variables.<BR>
 +
Just use them, they are always there for you.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 20: Line 30:
 
| file.ext
 
| file.ext
 
|}
 
|}
 +
Use this e.g. like:<BR>
 +
<source>
 +
var
 +
  vExt: WideString;
 +
begin
 +
  vExt    := WideExtractFileExt(FilePath);
 +
  FileName := FileName + '.backup' + vExt;
 +
end.
 +
</source>
 +
<BR>
 +
<BR>
 +
And there are this functions to extract parts:<BR>
  
<BR><BR>
 
And this functions to extract parts:<BR>
 
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 47: Line 67:
 
|}
 
|}
 
<BR>
 
<BR>
<BR>
+
You can fill an variable with the extracted part first<BR>
 
Use this e.g. like:<BR>
 
Use this e.g. like:<BR>
<BR>
 
 
<source>
 
<source>
 
var  
 
var  
Line 69: Line 88:
 
         + 'FilePath >>> '            + FilePath + #13#10
 
         + 'FilePath >>> '            + FilePath + #13#10
 
         + 'FileName >>> '            + FileName + #13#10
 
         + 'FileName >>> '            + FileName + #13#10
         + #13#10
+
         + #13#10
 
         + 'Extracted by using functions:'      + #13#10
 
         + 'Extracted by using functions:'      + #13#10
 
         + 'WideExtractFileDrive >>> '+ vDrive  + #13#10
 
         + 'WideExtractFileDrive >>> '+ vDrive  + #13#10
Line 79: Line 98:
 
         + 'And without the dot >>> ' + vE;
 
         + 'And without the dot >>> ' + vE;
 
       ShowMessage( vOUT );
 
       ShowMessage( vOUT );
 +
end.
 +
</source>
 +
Or just use the function 'on the fly'<BR>
 +
Use this e.g. like:<BR>
 +
<source>
 +
begin
 +
  FileName := FileName + '_from(' + WideExtractFileDrive(FilePath) + ')';
 
end.
 
end.
 
</source>
 
</source>
Line 91: Line 117:
 
Here are some code snippets for this issue:<BR>
 
Here are some code snippets for this issue:<BR>
 
<BR>
 
<BR>
First an 'trick' seen by Denis:<BR>
+
First we show you an 'trick' seen by Denis:<BR>
 +
Here we extract the path first "WideExtractFileDir(FilePath)"<BR>
 +
and then extract the last part, which is normal the filename, but here the parent folder "WideExtractFileName(...)"<BR>
 +
<BR>
 +
You can even use this trick more then once:<BR>
 +
(for our example "C:\GreatGrand\GrandParent\ParentFolder\file.ext")<BR>
 +
<BR>
 
{| class="wikitable"  
 
{| class="wikitable"  
 
|-
 
|-
Line 106: Line 138:
 
| GreatGrand  
 
| GreatGrand  
 
|}
 
|}
 
 
<BR>
 
<BR>
 
Use this like:<BR>
 
Use this like:<BR>
<BR>
 
 
 
<source>
 
<source>
 
var  
 
var  
 
   ParentFolder, GrandParent, GreatGrandParent: WideString;
 
   ParentFolder, GrandParent, GreatGrandParent: WideString;
 
begin  
 
begin  
   ParentFolder := WideExtractFileName(WideExtractFileDir(FilePath));
+
   ParentFolder     := WideExtractFileName(WideExtractFileDir(FilePath));
   GrandParent := WideExtractFileName(WideExtractFileDir(WideExtractFileDir(FilePath)));
+
   GrandParent     := WideExtractFileName(WideExtractFileDir(WideExtractFileDir(FilePath)));
 
   GreatGrandParent := WideExtractFileName(WideExtractFileDir(WideExtractFileDir(WideExtractFileDir(FilePath))));
 
   GreatGrandParent := WideExtractFileName(WideExtractFileDir(WideExtractFileDir(WideExtractFileDir(FilePath))));
   FileName := GreatGrandParent +'-'+GrandParent + '-'+ParentFolder + '-' + FileName;
+
   FileName         := GreatGrandParent + '-' + GrandParent + '-' + ParentFolder + '-' + FileName;
 
end.
 
end.
 
</source>
 
</source>
Line 128: Line 157:
 
Here is an another way by splitting the path at the back slash into an array 'Folders':
 
Here is an another way by splitting the path at the back slash into an array 'Folders':
  
 
 
<source>
 
<source>
 
var  
 
var  
Line 137: Line 165:
  
 
     // Get parts of the current file path:
 
     // Get parts of the current file path:
   oldPath := WideExtractFileDir(FilePath);
+
   oldPath               := WideExtractFileDir(FilePath);
   Folders := WideSplitString(oldPath, '\');  
+
   Folders               := WideSplitString(oldPath, '\');  
 
   TopMostFolder          := Folders[1];
 
   TopMostFolder          := Folders[1];
 
   SecondTopMostFolder    := Folders[2];
 
   SecondTopMostFolder    := Folders[2];
Line 145: Line 173:
 
   ParentFolder          := Folders[Length(Folders)-1];
 
   ParentFolder          := Folders[Length(Folders)-1];
  
   FileName := SecondTopMostFolder +'-'+GrandParent + '-'+ParentFolder + '-' + FileName;
+
   FileName := SecondTopMostFolder +'-' + GrandParent + '-' + ParentFolder + '-' + FileName;
 
end.
 
end.
 
</source>
 
</source>
Line 157: Line 185:
 
   
 
   
 
<source>
 
<source>
   Parent := ReplaceRegEx(FilePath, '.+\\(.+)\\.+', '$1', False, True);
+
   Parent       := ReplaceRegEx(FilePath, '.+\\(.+)\\.+',         '$1', False, True);
   GrandPa := ReplaceRegEx(FilePath, '.+\\(.+)\\.+\\.+', '$1', False, True);
+
   GrandPa     := ReplaceRegEx(FilePath, '.+\\(.+)\\.+\\.+',     '$1', False, True);
 
   GrandGrandPa := ReplaceRegEx(FilePath, '.+\\(.+)\\.+\\.+\\.+', '$1', False, True);
 
   GrandGrandPa := ReplaceRegEx(FilePath, '.+\\(.+)\\.+\\.+\\.+', '$1', False, True);
 
</source>
 
</source>
  
But note that RegEx is slow by its nature.
+
But note that RegEx is slow by its nature. But then you will see this first for more then thousand files ;-)
  
 
<BR>
 
<BR>
Line 188: Line 216:
 
"My Fav Artist - Title album song.mp3"<BR>
 
"My Fav Artist - Title album song.mp3"<BR>
 
Use:<BR>
 
Use:<BR>
 +
We split the file name at the dash and then modify the case different for the part before, and the part after the dash.<BR>
 
<source>
 
<source>
 
var   
 
var   
Line 199: Line 228:
 
     begin
 
     begin
 
       Extension  := WideExtractFileExt(FileName)
 
       Extension  := WideExtractFileExt(FileName)
       Part1      := WideCopy(WideExtractBaseName(FileName), 1, PosOfDelimiter -2);
+
       Part1      := WideCopy(WideExtractBaseName(FileName),                 1, PosOfDelimiter -2 );
       Part2      := WideCopy(WideExtractBaseName(FileName), PosOfDelimiter +2, Length(FileName));
+
       Part2      := WideCopy(WideExtractBaseName(FileName), PosOfDelimiter +2, Length(FileName) );
       Part2Char1 := WideCopy(Part2, 1, 1);
+
       Part2Char1 := WideCopy(Part2, 1,     1           );
       Part2Rest  := WideCopy(Part2, 2, Length(Part2)-1);
+
       Part2Rest  := WideCopy(Part2, 2, Length(Part2) -1 );
  
 
       //ShowMessage('Debug: #' + Part1 + '#' + Part2 + '#'  + Part2Char1 + '#' + Part2Rest + '#');
 
       //ShowMessage('Debug: #' + Part1 + '#' + Part2 + '#'  + Part2Char1 + '#' + Part2Rest + '#');

Revision as of 20:40, 17 August 2011

{{{iparam}}} This article needs to be cleaned up!

Find an overview of all build-in functions there >> File Name Utilities.


Here on this page we show you only the needed functions to extract parts
of the file name and how to use them in an PascalScript for ReNamer.


We like to show you this on an example file name
"C:\GreatGrand\GrandParent\ParentFolder\file.ext"


First, there are the always available variables 'FilePath' and 'FileName'
You don't have to declare ('var' / 'dim') or initialize ('var="";') this variables.
Just use them, they are always there for you.

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

Use this e.g. like:

var 
  vExt: WideString;
begin 
  vExt     := WideExtractFileExt(FilePath);
  FileName := FileName + '.backup' + vExt;
end.



And there are 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


You can fill an variable with the extracted part first
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.

Or just use the function 'on the fly'
Use this e.g. like:

begin 
  FileName := FileName + '_from(' + WideExtractFileDrive(FilePath) + ')';
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 we show you an 'trick' seen by Denis:
Here we extract the path first "WideExtractFileDir(FilePath)"
and then extract the last part, which is normal the filename, but here the parent folder "WideExtractFileName(...)"

You can even use this trick more then once:
(for our example "C:\GreatGrand\GrandParent\ParentFolder\file.ext")

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. But then you will see this first for more then thousand files ;-)




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:
We split the file name at the dash and then modify the case different for the part before, and the part after the dash.

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.