Difference between revisions of "ReNamer:Pascal Script:FilePath"
Line 14: | Line 14: | ||
We will be renaming only the Albums’ folders which are the deepest folders in the structure (so the 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 is no subfolders in the FilePath folder. | We will be renaming only the Albums’ folders which are the deepest folders in the structure (so the 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 is no subfolders in the FilePath folder. | ||
− | <source | + | <source> |
const | const | ||
DEST_FOLDER = 'C:\Destination\mp3\'; | DEST_FOLDER = 'C:\Destination\mp3\'; |
Revision as of 21:56, 12 August 2009
Let’s imagine the collection of mp3 files with no tags (such as Artist or Title) filled. It is a very neat collection organised with folders structure, eg.: 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 are hidden in the folders structure we will need to introduce the FilePath constant to solve the problem. It contains full path to the file together with the filename (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 lost the valuable information 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 the Albums’ folders which are the deepest folders in the structure (so the 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 is no subfolders in the FilePath folder.
const
DEST_FOLDER = 'C:\Destination\mp3\';
var
Folders, SubFolders : TStringsArray;
PathDepth : Integer;
begin
if WideDirectoryExists(FilePath) then
begin
WideScanDirForFolders(FilePath, SubFolders ,True, False, False); //checking for subfolders
if (Length(SubFolders)=0) then //if there are no subfolders
begin
Folders:=WideSplitString(FilePath,'\'); //creating an array with names of all the folders in the FilePath
PathDepth:=Length(Folders);
if (PathDepth>1) then //if the folder has a parent
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 a folder. This will prevent us from touching files. For file checking you may use WideFileExist(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 (Folders[PathDepth-2]) and the name of the deepest folder (Folders[PathDepth-1] or simply FileName).