#1 2018-07-22 03:26

ericzang
Member
Registered: 2018-07-22
Posts: 2

how to number as xx of xx (eg: 01 of 12)

( Sorry, I don't know the term for this kind of numbering method in order to search for it in the forum. )

Is it possible (via script or other wise) to number automatically all the files in a folder as  xx of xx (eg: 01 of 12)? Of course the first number is simple to do, but the "of xx" part? Can a script count the number of files in a folder, and then use this count result as a variable to insert in the name?

Thanks for such a comprehensive yet easy to use program! (I just started using it).

Last edited by ericzang (2018-07-22 03:27)

Offline

#2 2018-07-22 09:59

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

Re: how to number as xx of xx (eg: 01 of 12)

ericzang wrote:

Can a script count the number of files in a folder, and then use this count result as a variable to insert in the name?

Yes, you can achieve this with a Pascal Script rule, as follows:

function CountFilesInDir(const Dir, Mask: WideString): Integer;
var
  Files: TWideStringArray;
begin
  WideScanDirForFiles(Dir, Files, False, False, False, Mask);
  Result := Length(Files);
end;

begin
  FileName := WideExtractBaseName(FileName) +
    IntToStr(CountFilesInDir(WideExtractFileDir(FilePath), '*.*')) +
    WideExtractFileExt(FileName);
end.

This will append to the end of each filename the total number of matching files (see "*.*" mask) within the same directory.

You can adjust the script to do your whole operation, but the rest of actions can be achieved with the basic renaming rules, for example:

1) Insert: Insert " (" as Suffix (skip extension)
2) Serialize: Incremental from 1 repeat 1 step 1 (reset index if folder changes) as Suffix (skip extension)
3) Insert: Insert " of " as Suffix (skip extension)
4) Pascal Script
5) Insert: Insert ")" as Suffix (skip extension)

These rules will produce the following filename changes:

C:\Temp\a.txt  ->  a (1 of 5).txt
C:\Temp\b.txt  ->  b (2 of 5).txt
C:\Temp\c.txt  ->  c (3 of 5).txt
C:\Temp\d.txt  ->  d (4 of 5).txt
C:\Temp\e.txt  ->  e (5 of 5).txt

Offline

#3 2018-07-22 10:03

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

Re: how to number as xx of xx (eg: 01 of 12)

ericzang wrote:

Sorry, I don't know the term for this kind of numbering method in order to search for it in the forum.

Indeed, it is difficult to find topics similar to this one. However, Google's site search usually does a better job than the built-in forum search.

Here is one similar discussion:
track n of N tracks for audio books, way to get N?

Offline

#4 2018-07-22 10:34

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: how to number as xx of xx (eg: 01 of 12)

Additional to den4bs' script I have accepted that challenge too.
This script works without additional rules, adjust the zero-padding and also resets the file count if directory changes.
That way you can select several folders (Add files of folders // Do not add Folders as files; see Filter Settings) and rename all in one go.


FROM:
AB\a.txt
AB\b.txt
AB\c.txt
AB\e.txt

CD\a.txt
CD\b.txt
CD\c.txt

TO:
AB\a (01 of 88).txt
AB\b (02 of 88).txt
AB\c (03 of 88).txt
AB\e (04 of 88).txt

CD\a (0001 of 4444).txt
CD\b (0002 of 4444).txt
CD\c (0003 of 4444).txt


Use this PascalScript (http://www.den4b.com/wiki/ReNamer:Rules:PascalScript)

//http://www.den4b.com/forum/viewtopic.php?pid=10480#p10480
//http://www.den4b.com/wiki/ReNamer:Rules:PascalScript
//Add Serialize number to file name with amount of all files of a Folder: "Filename xx.ext" >>> "Filename xx (03 of 20).ext"
//Add Folders >> Filter Settings: Add Files of Folders, Add no Folders as files
VAR 
    //Filesarray:TStringsArray;
    Filesarray:TWideStringArray;
    CurrPath,LastPath,IdxStr:WideString;
    Initialized:Boolean;
    FileIndex, Padding: Integer;


Procedure Initialize;
  BEGIN
    CurrPath :=WideExtractFilePath(FilePath);
    SetLength(Filesarray, 0);
                       //Folder , array var ,  Recurse?, Hidden?, System?, mask
    WideScanDirForFiles(CurrPath, Filesarray,  False,   False , False ,  '*' );
    FileIndex := 0;
    Initialized := True;
  END;
  
BEGIN

    CurrPath :=WideExtractFilePath(FilePath);
    IF Not (CurrPath = LastPath) Then Initialize;
    LastPath := CurrPath;

    Padding := Length(IntToStr(Length(Filesarray)));
    FileIndex := FileIndex + 1;
    IdxStr := IntToStr(FileIndex);
    While Length(IdxStr) < Padding Do
       IdxStr := '0' + IdxStr;

  FileName :=   WideExtractBaseName(FileName)
                + ' (' + IdxStr + ' of ' + IntToStr(Length(Filesarray)) + ')'
                + WideExtractFileExt(FileName);
END.



Hope that makes sense and is not that much nonsense.


Thank you den4b for making ReNamer.

Last edited by Stefan (2018-07-22 10:41)


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#5 2018-07-22 12:44

ericzang
Member
Registered: 2018-07-22
Posts: 2

Re: how to number as xx of xx (eg: 01 of 12)

Wow, you guys are amazing! Thanks so much it worked great! I'll be getting to know the program more...

Offline

Board footer

Powered by FluxBB