Difference between revisions of "ReNamer:Pascal Script:SplitPath"
(Clean up) |
(Arrrghh, another little glitch.) |
||
Line 64: | Line 64: | ||
|- | |- | ||
| WideExtractFileExt | | WideExtractFileExt | ||
− | | ext | + | | .ext (dot included) |
|} | |} | ||
<BR> | <BR> | ||
Line 82: | Line 82: | ||
vBase := WideExtractBaseName(FilePath); | vBase := WideExtractBaseName(FilePath); | ||
vExt := WideExtractFileExt(FilePath); | vExt := WideExtractFileExt(FilePath); | ||
− | |||
// Test output as MsgBox: | // Test output as MsgBox: | ||
Line 90: | Line 89: | ||
+ #13#10 | + #13#10 | ||
+ 'Extracted by using functions:' + #13#10 | + 'Extracted by using functions:' + #13#10 | ||
− | + 'WideExtractFileDrive >>> '+ vDrive | + | + 'WideExtractFileDrive >>> ' + vDrive + #13#10 |
− | + 'WideExtractFileDir >>> ' | + | + 'WideExtractFileDir >>> ' + vDir + #13#10 |
− | + 'WideExtractFilePath >>> ' + vPath | + | + 'WideExtractFilePath >>> ' + vPath + #13#10 |
− | + 'WideExtractFileName >>> ' + vName | + | + 'WideExtractFileName >>> ' + vName + #13#10 |
− | + 'WideExtractBaseName >>> ' + vBase | + | + 'WideExtractBaseName >>> ' + vBase + #13#10 |
− | + 'WideExtractFileExt >>> ' | + | + 'WideExtractFileExt >>> ' + vExt; |
− | |||
ShowMessage( vOUT ); | ShowMessage( vOUT ); | ||
end. | end. | ||
Line 104: | Line 102: | ||
<source> | <source> | ||
begin | begin | ||
− | FileName := FileName + ' | + | FileName := FileName + '_backup' + WideExtractFileExt(FilePath); |
end. | end. | ||
</source> | </source> | ||
Line 149: | Line 147: | ||
FileName := GreatGrandParent + '-' + GrandParent + '-' + ParentFolder + '-' + FileName; | FileName := GreatGrandParent + '-' + GrandParent + '-' + ParentFolder + '-' + FileName; | ||
end. | end. | ||
+ | </source> | ||
+ | <BR> | ||
+ | <BR> | ||
+ | To get the extension without the dot use something like:<BR> | ||
+ | <source> | ||
+ | vExt := WideReplaceStr(WideExtractFileExt(FilePath), '.', ''); | ||
+ | </source> | ||
+ | Or step by step:<BR> | ||
+ | <source> | ||
+ | vExt := WideExtractFileExt(FilePath); | ||
+ | vExt := WideReplaceStr( vExt, '.', ''); | ||
</source> | </source> | ||
<BR> | <BR> | ||
Line 173: | Line 182: | ||
ParentFolder := Folders[Length(Folders)-1]; | ParentFolder := Folders[Length(Folders)-1]; | ||
− | FileName := SecondTopMostFolder +'-' + GrandParent + '-' + ParentFolder + '-' + FileName; | + | FileName := SecondTopMostFolder + '-' + GrandParent + '-' + ParentFolder + '-' + FileName; |
end. | end. | ||
</source> | </source> |
Revision as of 20:56, 17 August 2011
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 (dot included) |
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);
// 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;
ShowMessage( vOUT );
end.
Or just use the function 'on the fly'
Use this e.g. like:
begin
FileName := FileName + '_backup' + WideExtractFileExt(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.
To get the extension without the dot use something like:
vExt := WideReplaceStr(WideExtractFileExt(FilePath), '.', '');
Or step by step:
vExt := WideExtractFileExt(FilePath);
vExt := WideReplaceStr( vExt, '.', '');
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.