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

From den4b Wiki
Jump to navigation Jump to search
(Add some examples to split path and file into parts)
 
m (Tried some formating :-()
Line 3: Line 3:
 
That are:
 
That are:
  
For "C:\GreatGrand\GrandParent\ParentFolder\file.ext"
+
For e.g. "C:\GreatGrand\GrandParent\ParentFolder\file.ext"
  
The always available variable 'FilePath' and 'FileName':
+
The always available variable 'FilePath' and 'FileName':<BR>
*  FilePath > C:\GreatGrand\GrandParent\ParentFolder\file.ext
 
*  FileName > file.ext
 
  
And this functions to extract parts:
+
{| class="wikitable"
WideExtractFilePath ==> C:\GreatGrand\GrandParent\ParentFolder\
+
|-
WideExtractFileDir ==> C:\GreatGrand\GrandParent\ParentFolder
+
! Variable
WideExtractFileDrive ==> C:\
+
! provides
WideExtractFileName ==> file.ext
+
|-
WideExtractBaseName ==> file
+
| FilePath
WideExtractFileExt ===> ext
+
| C:\GreatGrand\GrandParent\ParentFolder\file.ext
 +
|-
 +
| FileName
 +
| file.ext
 +
|}
 +
 
 +
<BR><BR>
 +
And this functions to extract parts:<BR>
 +
{| class="wikitable"
 +
|-
 +
! Function
 +
! provides
 +
|-
 +
| WideExtractFilePath
 +
| C:\GreatGrand\GrandParent\ParentFolder\
 +
|-
 +
| WideExtractFileDir
 +
| C:\GreatGrand\GrandParent\ParentFolder
 +
|-
 +
| WideExtractFileDrive
 +
| C:\
 +
|-
 +
| WideExtractFileName
 +
| file.ext
 +
|-
 +
| WideExtractBaseName
 +
| file
 +
|-
 +
| WideExtractFileExt
 +
| ext
 +
|}
 
<BR>
 
<BR>
 
---------------------
 
---------------------
 
<BR>
 
<BR>
 +
 
But this functions didn't gave all possibilities to split an full path into all wanted parts.<BR>
 
But this functions didn't gave all possibilities to split an full path into all wanted parts.<BR>
 
You have to know how to handle this functions and/or use own code to achieve what you want.<BR>
 
You have to know how to handle this functions and/or use own code to achieve what you want.<BR>
 +
<BR>
 
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 an 'trick' seen by Denis:<BR>
WideExtractFileName(WideExtractFileDir(FilePath)); ==> ParentFolder
+
{| class="wikitable"
WideExtractFileName(WideExtractFileDir(WideExtractFileDir(FilePath))); ==> GrandParent
+
|-
WideExtractFileName(WideExtractFileDir(WideExtractFileDir(WideExtractFileDir(FilePath)))); ==> GreatGrand
+
! nested Functions
 +
! provides
 +
|-
 +
| WideExtractFileName(WideExtractFileDir(FilePath));  
 +
| ParentFolder  
 +
|-
 +
| WideExtractFileName(WideExtractFileDir(WideExtractFileDir(FilePath)));  
 +
| GrandParent  
 +
|-
 +
| WideExtractFileName(WideExtractFileDir(WideExtractFileDir(WideExtractFileDir(FilePath))));  
 +
| GreatGrand  
 +
|}
  
 
<BR>
 
<BR>
Line 45: Line 86:
  
 
---------------------
 
---------------------
 
+
<BR>
  
 
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':
Line 70: Line 111:
 
</source>
 
</source>
  
 +
<BR>
 
---------------------
 
---------------------
 
+
<BR>
  
 
And there is Regular Expression to extract the parts:
 
And there is Regular Expression to extract the parts:
Line 84: Line 126:
 
But note that RegEx is slow by its nature.
 
But note that RegEx is slow by its nature.
  
 
+
<BR>
 
---------------------
 
---------------------
 +
<BR>
  
 
And there are Meta Tags to extract e.g. the parent folder
 
And there are Meta Tags to extract e.g. the parent folder
Line 95: Line 138:
 
See 'Insert' Rule and click there at 'Insert Meta Tag'
 
See 'Insert' Rule and click there at 'Insert Meta Tag'
  
 
+
<BR>
 
---------------------
 
---------------------
 
+
<BR>
  
 
To split file name into parts at an delimiter we can use f.ex.:
 
To split file name into parts at an delimiter we can use f.ex.:
Line 133: Line 176:
 
</source>
 
</source>
  
 +
<BR>
 
---------------------
 
---------------------
 +
<BR>

Revision as of 08:15, 17 August 2011

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.