ReNamer:Pascal Script:FilePath

From den4b Wiki
Revision as of 15:02, 8 February 2017 by Den4b (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Let's imagine a collection of mp3 files with no tags (such as Artist or Title) filled in. 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 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 : TWideStringArray;
  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 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).