Difference between revisions of "ReNamer:Pascal Script:FilePath"
(cleanup) |
|||
Line 1: | Line 1: | ||
{{Template:Cleanup}} | {{Template:Cleanup}} | ||
− | + | Let's imagine a collection of mp3 files with no tags filled in, such as Artist or Title. It is a very neat collection organized with folders structure, e.g.: ''D:\MP3\Arist\Album\Title.mp3''. But some day we decide to change the structure to: ''D:\MP3\Artist – Album\Title.mp3''. | |
− | As all the data | + | As all the data is stored in the folders structure we will need to introduce the '''FilePath''' constant to solve the problem. It contains full path to the file, e.g. ''"D:\test\file.txt"''. It's declaration looks like that: |
− | <source | + | <source>const FilePath: WideString;</source> |
+ | Note: '''FilePath''' constant is provided only for reference. It is possible to change its value, but this will have no influence on renaming operation and you will lose valuable information that it contains. | ||
− | + | Now when we know the full path to the file all we need is to extract the names of Artist and Album folders and use them to build the new filename. To make it easy we will move all the renamed folders to some temporary destination folder (DEST_FOLDER) so we could check if everything is ok and then delete the old empty structure. Moving folder moves also the contents of the folder so we don't need to trouble ourselves about that. | |
− | + | We will be renaming only Album folders which are the deepest folders in the structure (ones that have no subfolders). This will be checked with '''WideScanDirForFolders '''function. If the function retrieves an empty SubFolders array we can be sure that there are no subfolders in the FilePath folder. | |
− | |||
− | |||
− | We will be renaming only | ||
<source> | <source> | ||
const | const | ||
− | DEST_FOLDER = 'C:\Destination\mp3\'; | + | DEST_FOLDER = 'C:\Destination\mp3\'; |
var | var | ||
− | Folders, SubFolders : TStringsArray; | + | Folders, SubFolders : TStringsArray; |
− | PathDepth : Integer; | + | PathDepth : Integer; |
begin | begin | ||
if WideDirectoryExists(FilePath) then | if WideDirectoryExists(FilePath) then | ||
+ | begin | ||
+ | // checking for subfolders | ||
+ | WideScanDirForFolders(FilePath, SubFolders ,True, False, False); | ||
+ | // if there are no subfolders | ||
+ | if Length(SubFolders) = 0 then | ||
begin | begin | ||
− | + | // creating an array with names of all the folders in the FilePath | |
− | + | Folders := WideSplitString(FilePath,'\'); | |
− | + | PathDepth := Length(Folders); | |
− | + | // if the folder has a parent | |
− | + | if PathDepth > 1 then | |
− | + | FileName := DEST_FOLDER+Folders[PathDepth-2]+' - ' + Folders[PathDepth-1]; | |
− | + | end; | |
− | + | end; | |
− | |||
end. | end. | ||
</source> | </source> | ||
− | |||
− | The main part happens after using '''WideSplitString''' function to create an array filled with names of all folders in the FilePath. | + | In our script we've used another useful function '''WideDirectoryExists'''(FilePath). It simply checks if the given path describes folder (an existing folder). This will prevent us from touching files. For file checking you may use '''WideFileExists'''(FilePath) function. |
− | We build the new filename by adding together DEST_FOLDER, the name of the second folder from the end of the FilePath (Artist) and the name of the deepest folder (Album). | + | |
+ | The main part happens after using '''WideSplitString''' function to create an array filled with names of all folders in the FilePath. We build the new filename by adding together DEST_FOLDER, the name of the second folder from the end of the FilePath (Artist) and the name of the deepest folder (Album). |
Revision as of 00:32, 13 August 2009
Let's imagine a collection of mp3 files with no tags filled in, such as Artist or Title. It is a very neat collection organized with folders structure, e.g.: D:\MP3\Arist\Album\Title.mp3. But some day we decide to change the structure to: D:\MP3\Artist – Album\Title.mp3.
As all the data is stored in the folders structure we will need to introduce the FilePath constant to solve the problem. It contains full path to the file, e.g. "D:\test\file.txt". It's declaration looks like that:
const FilePath: WideString;
Note: FilePath constant is provided only for reference. It is possible to change its value, but this will have no influence on renaming operation and you will lose valuable information that it contains.
Now when we know the full path to the file all we need is to extract the names of Artist and Album folders and use them to build the new filename. To make it easy we will move all the renamed folders to some temporary destination folder (DEST_FOLDER) so we could check if everything is ok and then delete the old empty structure. Moving folder moves also the contents of the folder so we don't need to trouble ourselves about that.
We will be renaming only Album folders which are the deepest folders in the structure (ones that have no subfolders). This will be checked with WideScanDirForFolders function. If the function retrieves an empty SubFolders array we can be sure that there are no subfolders in the FilePath folder.
const
DEST_FOLDER = 'C:\Destination\mp3\';
var
Folders, SubFolders : TStringsArray;
PathDepth : Integer;
begin
if WideDirectoryExists(FilePath) then
begin
// checking for subfolders
WideScanDirForFolders(FilePath, SubFolders ,True, False, False);
// if there are no subfolders
if Length(SubFolders) = 0 then
begin
// creating an array with names of all the folders in the FilePath
Folders := WideSplitString(FilePath,'\');
PathDepth := Length(Folders);
// if the folder has a parent
if PathDepth > 1 then
FileName := DEST_FOLDER+Folders[PathDepth-2]+' - ' + Folders[PathDepth-1];
end;
end;
end.
In our script we've used another useful function WideDirectoryExists(FilePath). It simply checks if the given path describes folder (an existing folder). This will prevent us from touching files. For file checking you may use WideFileExists(FilePath) function.
The main part happens after using WideSplitString function to create an array filled with names of all folders in the FilePath. We build the new filename by adding together DEST_FOLDER, the name of the second folder from the end of the FilePath (Artist) and the name of the deepest folder (Album).