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

From den4b Wiki
Jump to: navigation, search
(cleanup)
Line 1: Line 1:
{{Template:Cleanup}}
+
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''.
 
 
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:
 
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>const FilePath: WideString;</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.  
+
<span style="color: red">Note:</span> '''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.
+
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.  
 
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.  
Line 23: Line 21:
 
   if WideDirectoryExists(FilePath) then
 
   if WideDirectoryExists(FilePath) then
 
   begin
 
   begin
    // checking for subfolders
+
     WideScanDirForFolders(FilePath, SubFolders ,True, False, False);         // checking for subfolders
     WideScanDirForFolders(FilePath, SubFolders ,True, False, False);
+
     if Length(SubFolders) = 0 then                                           // if there are no subfolders
    // 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,'\');          // creating an array with names of all the folders in the FilePath
      Folders := WideSplitString(FilePath,'\'); 
 
 
       PathDepth := Length(Folders);
 
       PathDepth := Length(Folders);
       // if the folder has a parent
+
       if PathDepth > 1 then                                                  // if the folder has a parent
      if PathDepth > 1 then
 
 
         FileName := DEST_FOLDER+Folders[PathDepth-2]+' - ' + Folders[PathDepth-1];
 
         FileName := DEST_FOLDER+Folders[PathDepth-2]+' - ' + Folders[PathDepth-1];
 
     end;
 
     end;
Line 39: Line 33:
 
</source>
 
</source>
  
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.
+
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).
+
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 17:10, 13 August 2009

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 : 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 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).