#11 2008-08-12 18:37

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Folder Rename strictly using Pascal Rule

It makes the problem much easier to solve. In the simplest solution we need a script that renames a folder only when it contains files and no folders...
So we DO NOT have to rename parent folders, so the "parent folders first" problem does not exist... Am I right?

Last edited by krtek (2008-08-12 18:37)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#12 2008-08-12 22:37

clapp
Member
Registered: 2008-02-21
Posts: 9

Re: Folder Rename strictly using Pascal Rule

Yes... that seems right... all of the files will be under the deepest folders... so if the script could somehow differentiate and separate out the folders that only contain files (and no folders)... and rename these... then I guess it would be renaming only the deepest folders...

... and could these renamed folders (renamed to Fld1,Fld2,etc.) and their files end up directly under the MainFld:

C:\MainFld\Fld1\renamed_file1
C:\MainFld\Fld1\renamed_file2
C:\MainFld\Fld2\renamed_file3

etc....

and I guess, in the end, the old subfolders will still be there too, just empty of files.

Offline

#13 2008-08-12 23:45

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Folder Rename strictly using Pascal Rule

Yup, that's it. Ok, I'll try to write that script tomorrow...
Once we will have a script that solves your particular problem, we will be able to start  thinking abstractly about how we could improve it.

Last edited by krtek (2008-08-13 16:33)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#14 2008-08-13 17:35

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Folder Rename strictly using Pascal Rule

Hmm.... It's not so easy... Now we have "files before folders" problem...

Anyway, I tried to modify Andrews script and that's what I came up with. I'm not proud of it. It's vulnerable and solves the problem only partially.
You may get
Fld1\File4.txt
Fld2\File1.txt
Fld2\File2.txt
Fld2\File3.txt

Whats more it MUST be run with files table sorted by "path" in revert order...
And you need to define path for the ROOT folder which should end with "\" (in the head of script) manually...
And it doesn't solve the problem of updating COUNT_FILE with every Preview...

So, there is still huge work to be done. Perhaps we will need few scripts to do the job, which will be communicating among themselves with a txt file. First would look for the root folder, second would try to build a CSV file with oldnames-newnames, and then third rule would import that file and use it for actual renaming...
But I don't know when I will have time to try to write it.




const
FLD = 'Fld';
FILE = 'File';
COUNT_FILE = 'Count.txt';
INCLUDE_HIDDEN = false;
INCLUDE_SYSTEM = false;
ROOT = '';

var
  Initialized: Boolean;
  DirCnt, FileCnt : Integer;
  TempFiles, TempFolders : TStringsArray;
  Empty : TStringsArray;

begin
  if not Initialized then
  begin
    DirCnt := 0;
    FileCnt := 0;
    if(WideFileExists(COUNT_FILE)) then
    begin
      DirCnt := StrToIntDef(FileReadLine(COUNT_FILE, 2), 0);
      FileCnt := StrToIntDef(FileReadLine(COUNT_FILE, 4), 0);
    end;
          //    FileWriteContent('List.txt', '');
    Initialized := True;
  end;

  TempFiles := Empty;
  TempFolders := Empty;

  if(WideDirectoryExists(FilePath)) then
  begin
      WideScanDirForFolders(FilePath, TempFolders, false, INCLUDE_HIDDEN, INCLUDE_SYSTEM);
      if Length(TempFolders) = 0 then
      begin
       WideScanDirForFiles(FilePath, TempFiles, false, INCLUDE_HIDDEN, INCLUDE_SYSTEM,'*');
       if Length(TempFiles) > 0 then
       begin
         DirCnt := DirCnt + 1;
             //FileAppendContent('List.txt', FileName);
         FileName := ROOT + FLD + IntToStr(DirCnt);
             //    FileAppendContent('List.txt', ','+FileName+#13#10);
       end
      end
  end
  
  else
  begin
    FileCnt := FileCnt + 1;
           // FileAppendContent('List.txt', FileName);
    FileName := FILE + IntToStr(FileCnt) + WideExtractFileExt(FileName);
         // FileAppendContent('List.txt', ','+FileName+#13#10);
  end;
  
  FileWriteContent(COUNT_FILE, 'Dir Count:'+#13#10+IntToStr(DirCnt)+#13#10+'File Count:'+#13#10+IntToStr(FileCnt));
end.

Last edited by krtek (2008-08-13 17:58)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#15 2008-08-13 18:08

Andrew
Senior Member
Registered: 2008-05-22
Posts: 542

Re: Folder Rename strictly using Pascal Rule

Hmm, even though I haven't run it, that doesn't look half-bad, krtek! smile I am on a different track altogether and am trying to make my code as generic as possible so no paths have to be hard-coded etc. Let me see how far I can get with it, 'cos I may be a bit busy for the next few days. However, it is a nice challenge so I hope to finish it soon (hopefully!)

Offline

#16 2008-08-14 12:46

krtek
Senior Member
From: Łódź (Poland)
Registered: 2008-02-21
Posts: 262

Re: Folder Rename strictly using Pascal Rule

Ok, now I have written something useful tongue

That script creates three files:
DIR_LIST  - list of folders in files table
FILE_LIST - list of files in files table
DATA_FILE - root folder (in first line, without the ending '\') and depth of the deepest folder (in second line)
and does nothing about renaming.

Now second script may take these data (and using only Initialization part of script) create a list of files/folders with their new names and pass it to files table or rename files using WideRenameFile(const OldName, NewName: WideString) function...
At least I guess so, cause I have no time to write it tongue
edit: Using the following script will need few separate projects in ReNamer, and not few scripts/rules in one project as I said before...
It's because all rules are executed at once on every file/folder (all rules on first file, all rules on second file...). My previous speculation would need rules to be executed separately on all files/folders (first rule on all files, then second rule on all files...).
Hopefully clapp wants to use it from command line, so he will be able to write a batch file to rerun ReNamer with different scripts on the same root catalouge.


const
DIR_LIST = 'DirList.txt';
FILE_LIST = 'FileList.txt';
DATA_FILE = 'Data.txt';

var
  Initialized: Boolean;
  TempFolders : TStringsArray;
  Empty : TStringsArray;
  bottom, top : Integer;
  root : String;

begin
  if not Initialized then
  begin
    bottom := -1;
    top := -1;
    root := '';
    FileWriteContent(DIR_LIST, '');
    FileWriteContent(FILE_LIST, '');
    Initialized := True;
  end;

  if(WideDirectoryExists(FilePath)) then
  begin
    FileAppendContent(DIR_LIST, FilePath + #13#10); 
       
    TempFolders:= WideSplitString(FilePath, '\');
    if (top > Length(TempFolders)) or (top < 0) then
      begin
        top := Length(TempFolders);
        root := FilePath;
    end;
      
    if (bottom < Length(TempFolders)) or (bottom < 0) then
    begin
      bottom := Length(TempFolders);
    end;
    
    TempFolders := Empty;
    //SetLength(TempFolders, 0);

  end
  
  else
  begin
    FileAppendContent(FILE_LIST, FilePath +#13#10);
  end;
  
  FileWriteContent(DATA_FILE, root +#13#10+'Deepest folder: '+IntToStr(bottom)+#13#10);
end.

It has no security checks, so if you put only files and not folders (watch your filters) or if you put more than one root folder, you can't expect correct results...


BTW, do anyone know how to empty array in Pascal? I'm using assigning now, but there should be some convenient way of reinitializing array...

Last edited by krtek (2008-08-15 19:47)


Regular Expressions are not as hard to understand as you may think. Check ReNamer's manual or nice Regular Expressions tutorial for more info and start to use full power of applications that use them (like ReNamer, Mp3Tag and so on).

Offline

#17 2008-08-17 19:12

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: Folder Rename strictly using Pascal Rule

I think there is enought input on this topic, so I'll just answer this one question...

how to empty array in Pascal? I'm using assigning now, but there should be some convenient way of reinitializing array...

1) Array := nil;
2) SetLength(Array, 0);

Both do the same thing, but I usually go for the first one.

Offline

Board footer

Powered by FluxBB