Topic: Rename dir with number of files?

Goal is to rename a list of directories by appending the number of files contained to the name:
   REN DirName DirName (xN) ;where N=# of files in the directory

Siren doesn't have a file count variable so I thought of using a textfile to save the file count & came up with this to generate the text file from the cmd prompt:
for /D %i in (*) do @(dir /b /a-d "%i" | find  /c /v "" > "%i\numfiles.txt")

So now I have numfiles.txt in each of the directories; I thought I could use %T to rename:

%b (x%T{"%fa\\numfiles.txt",1})

Alas %T appears not to accept variables in the "f" parameter.

Also thought of continuing at the command prompt but am stumped trying to get REN to accept variable input.

Ideas? Suggestions?

2 (edited by Stefan 2015-11-09 16:44:17)

Re: Rename dir with number of files?

I tried the same trick with %T the other day too without luck ;-)

Maybe it would be possible for Rémi to enhance %T{}
to allow calculating sub-expressions like:      >>> %T{  (%pa

was cut...

>>> %T{  (%pa"Numfiles.txt")  } <<<

3 (edited by Stefan 2015-11-09 20:16:13)

Re: Rename dir with number of files?

As an workaround, if you really need that feature NOW,
see here for basic idea >> … d=446#p446

And here is such a AHK-script for Siren v3 for your purpose:

#SingleInstance, force
;1) Open Siren v3 and select the wanted folders.
;2) Switch to WinExplorer and execute this script with installed AutoHotkey application
;3) Switch back to Siren and watch what happens....
;;;; THE STEPS....
;IF Siren gets the focus:
WinWaitActive, ahk_class wxWindowNR 
  ;Enter Siren expression: %fa to get full path of selected folder
  ControlSetText, Edit1,`%fa, Siren
  ;Execute Siren command Ctrl+P
  Send ^p
  ;For each copied line (folder name) do:
  Loop parse, clipboard, `n , `r
        ;Skip header line:
        if A_Index = 1

       myLine :=A_LoopField 
       Loop, parse, myLine, %A_Tab%
            ;myLine is tab-delimited.
            ;Get New Name field (2th field):
            if(A_Index = 2){
                vFolder := A_LoopField
                ;Read in the file content:
                vFile = %vFolder%\SirenNumfiles.txt
                if (FileExist(vFile)){
                    FileReadLine, vOutputVar,%vFile%, 1
                    myOut = %myOut%%vOutputVar%`r`n
;MsgBox OUT:`r`n%myOut%
clipboard := myOut   
WinActivate,  ahk_class wxWindowNR
;Enter Siren expression: "%b_(%C files)" for new name of selected folder:
ControlSetText, Edit1,`%b_(`%C files), Siren

Re: Rename dir with number of files?

Stefan, Thanks for suggestion.
When I get some time I may download AHK and try out your script.
I think there is also a way using DOS rename in a batch file but haven't quite cracked how to pass variables to the rename command.
Sure would be nice if the %T function was enhanced as you suggested!

Re: Rename dir with number of files?


There are different ways to get what you want.
The simplest seems to be the regular expression modifier.
For example : %b_%T{%fa(s/$/\\numfiles.txt/),1}

Here, the metacharacter '$' (regular expression standard) represents the end the string.
In our case the end of "%fa".
'$' is replaced by "\numfiles.txt" which is equivalent to concatenate the two strings.

I have already thought about expanding the expression syntax to allow a more natural way
to achieve this but I haven't found the right solution yet.
Do not hesitate to give your ideas.

One remark : you posted in the "Siren 1 or 2" part of this forum but this expression
won't work with these old versions.
Maybe should I move this post to the "Siren 3" section. Ok ?


Re: Rename dir with number of files?


Great solution, Rémi!

- - -

%b_(%T{%fa(s/$/\\_SirenNumFiles.txt/),1} files)

works for me on Siren v3.

Even with spaces in  path.

- - -

Examples (some folders without '_SirenNumFiles.txt'):

Current name    New name    Sel    Type                   
Accents    Accents_(6 files)    004 Directory
Add-fitting-Zeros    Add-fitting-Zeros_( files)    005 Directory
AddSerialize inbetween    AddSerialize inbetween_(2 files)    006    Directory
AddSpacebetween Upper case Letter    AddSpacebetween Upper case Letter_(1 files)    007    Directory   
AddSpacebetweenLetterAndDigitsOrSymbol    AddSpacebetweenLetterAndDigitsOrSymbol_(4 files) 008 Directory
b    b_( files)    009 Directory

- - -


%b_ >>> origin base name, without extension.

( >>> A literal '(' sign.

%T{%fa(s/$/\\_SirenNumFiles.txt/),1} >>>
>>> %T{file path,1} expression
>>> plus %fa expression as file path
>>> with an additional '(s///)'- RegEx search&replace on that %fa expression.

>>> match the end of the value of %fa
>>> and add an (here regex escaped) backslash
>>> plus a literal string (here: '_SirenNumFiles.txt')

files)  >>> literal ' files)' sings.


Thanks Rémi :thumpsup:

7 (edited by Stefan 2015-11-10 15:53:13)

Re: Rename dir with number of files?

Rémi wrote:
>I have already thought about expanding the expression syntax....
>Do not hesitate to give your ideas.

Just mention that trick to 'read files relative to folders' in the help file.
Read always first line from each file:
%T{  %fa(s/$/\\Text.txt/) , 1 }

Additional mention the way to 'read one file from Sirens' home folder':
%Pe the path to Siren's executable file 
Read lines from a single file, but relative to file selection in Siren list view:
%T{  %Pe(s/$/\\Text.txt/)     }
Of course, if you do not need this portable, relative path, you can also use the absolute path, like e.g.
%T{  "C:\\Tools\\SirenV3\\Text.txt"     }

- - -
the relative path form works also for me:
%T{ %f(s///) ,1}
%T{ %b(s///) ,1}
instead of
%T{ %fa(s///) ,1}

8 (edited by daffdaemon 2015-11-12 17:37:35)

Re: Rename dir with number of files?

Remi, Thanks, Works like a charm.

step 1, run from cmd prompt, creates numfiles.txt:
   for /D %i in (*) do @(dir /b /a-d "%i" | find  /c /v "" > "%i\numfiles.txt")

step 2, run in siren, rename Filename >> Filename (xN) where N = number of files.
Note: Have to subtract 1 for numfiles.txt or 2 if thumbs.db exists
  %b - %T{%fa(s/$/\\numfiles.txt/),1};%b[1,-1] (x%N{1,-1,-1})

step 3, run from cmd prompt, deletes numfiles.txt
  for /D %i in (*) do @(del "%i\numfiles.txt")

BTW: Sorry about misplacing the post. Yes, please, move the thread to proper place.

9 (edited by daffdaemon 2015-11-11 18:50:43)

Re: Rename dir with number of files?

Remi, if you recall, a few months ago, we were talking about renaming folder based on files in the folder.
This same technique we used here would work for that, too.

Example: I have a bunch of folders with non-descript names but the 1st file in each folder is a cover with a name in the form of "Date - Artist - Album.jpg"

step 1: for /D %i in (*) do @(dir /b /a-d /on "%i" > "%i\dirfiles.txt")
step 2: %T{%fa(s/$/\\dirfiles.txt/),1};%b
step 3: for /D %i in (*) do @(del "%i\dirfiles.txt")


One thing I noticed playing around with this. If the file I want to use as folder name is the last file, then the form:
Doesn't seem to work.

Re: Rename dir with number of files?

Yes, negative numbers are not implemented for %T variable.But you could had used SORT for /D %i in (*) do @(sort /R

Re: Rename dir with number of files?

Again cut post

(At least 60 seconds have to pass between posts. Please wait a while and try posting again.)

12 (edited by Stefan 2015-11-11 20:58:35)

Re: Rename dir with number of files?

Yes, negative numbers are not implemented for %T variable.

But you could had used SORT
for /D %i in (*) do @(sort /R "%i\dirfiles.txt" /O "%i\dirfiles.txt")

But DIR has also a reverse switch for the /O operator: -

I had two additional ideas:
1. prefix the temp file name  with zzz or 000 (zero) respective to sort them out
of the way, to be NOT the first / last file in folder.
2. add a (s///) modifier to the %T variable to remove that temp file name, if it is the only one in folder.

So I have testet this:

To get first file from folder (Note the zzz prefix) :
step 1:for /D %i in (*) do @(dir /b /a-d /on "%i" > "%i\zzzdirfiles.txt")
step 2: %T{%fa(s/$/\\zzzdirfiles.txt/),1}(s/zzzdirfiles.txt//);%b

To get last file from folder (Note the /o-n DIR switch and the 000 prefix):
step 1:for /D %i in (*) do @(dir /b /a-d /o-n "%i" > "%i\000dirfiles.txt")
step 2: %T{%fa(s/$/\\000dirfiles.txt/),1}(s/000dirfiles.txt//);%b

Of course we would use '000' and 'zzz' without extension as temp file name only, to make typing easier  cool

HTH?  big_smile

Re: Rename dir with number of files?


I noticed you seemed to start your DOS commands manually via a extern "cmd" command.
Have you tried using the Siren's "Tools" feature ?
I have made some tests and it seems to work correctly.
For example to create the "numfiles.txt" files I added a new entry in "Tools" :
Name : Create numfiles.txt
Command : cmd /C \"dir /b /a-d \"%f\" | find  /c /v \"\" > \"%f\\numfiles.txt\"\"

Just a reminder : the "tools" are executed on highlighted files, not the selected ones.


14 (edited by daffdaemon 2015-11-12 19:46:53)

Re: Rename dir with number of files?

Thank you all for the suggestions.
Remi, I've never tried the Tools option. When I get a chance I will give it a try.

So, perhaps you could add additional variable input & negative index numbers to your list of possible enhacements to %T.
Consider, too, returning multiple lines from the text file (ie 2-4, 0=all) for post-processing with regex (assuming the regex engine works on multi-line text strings, which I haven't tested)

Thanks again for the help.
I'm particularly pleased, now have an easy way to rename path from file names.