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

From den4b Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:Cleanup}}  
+
{{Up|ReNamer:Pascal Script}}
  
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''.
+
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 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:
+
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 lang="pascal">const FilePath : WideString;</source>
+
<syntaxhighlight lang="pascal">const FilePath: WideString;</syntaxhighlight>
  
 +
<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.
  
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 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.
+
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.  
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.
+
<syntaxhighlight lang="pascal">
 
 
<source lang="pascal">
 
 
const
 
const
DEST_FOLDER = 'C:\Destination\mp3\';
+
  DEST_FOLDER = 'C:\Destination\mp3\';
  
 
var
 
var
Folders, SubFolders : TStringsArray;
+
  Folders, SubFolders : TWideStringArray;
PathDepth : Integer;
+
  PathDepth : Integer;
  
 
begin
 
begin
 
   if WideDirectoryExists(FilePath) then
 
   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
 
     begin
       WideScanDirForFolders(FilePath, SubFolders ,True, False, False);  //checking for subfolders
+
       Folders := WideSplitString(FilePath,'\');         // creating an array with names of all the folders in the FilePath
      if (Length(SubFolders)=0) then                                    //if there are no subfolders
+
      PathDepth := Length(Folders);
        begin
+
      if PathDepth > 1 then                                                   // if the folder has a parent
          Folders:=WideSplitString(FilePath,'\');                       //creating an array with names of all the folders in the FilePath
+
        FileName := DEST_FOLDER+Folders[PathDepth-2]+' - ' + Folders[PathDepth-1];
          PathDepth:=Length(Folders);
+
    end;
          if (PathDepth>1) then                                         //if the folder has a parent
+
  end;
          FileName:=DEST_FOLDER+Folders[PathDepth-2]+' - ' + Folders[PathDepth-1];
 
        end;
 
      end;
 
 
end.
 
end.
</source>
+
</syntaxhighlight>
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.
+
 
 +
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.
+
[[Category:ReNamer]]
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).
+
[[Category:Pascal Script]]

Latest revision as of 15:02, 8 February 2017

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