#1 2010-04-16 21:19

ozzii
Senior Member
Registered: 2007-07-04
Posts: 101

Folder with number of files

Hello,
Can someone provide me a pascal script (I thing that it can do it) for adding at the end of folder the number of txt files in it.
For example I have:
C:\TEST\test1.txt
C:\TEST\test2.txt
C:\TEST\test3.txt

And after I would like to have:
C:\TEST(3)\test1.txt
C:\TEST(3)\test2.txt
C:\TEST(3)\test3.txt

Thanks in advance.


_________________
Do, or do not. There is no 'try.'" --  Jedi Master Yoda

Offline

#2 2010-04-16 21:52

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Folder with number of files

Try with this

var
  Files: TStringsArray;
  BasePath: WideString;

begin
  BasePath := WideExcludeTrailingPathDelimiter(WideExtractFilePath(FilePath));
  
  SetLength(Files, 0);
  WideScanDirForFiles(BasePath, Files, False, False, False, '*.txt');
  
  FileName := BasePath + ' (' + IntToStr(length(Files)) + ')\' + FileName;
end.

If this software has helped you, consider getting your pro version. :)

Offline

#3 2010-04-17 08:56

ozzii
Senior Member
Registered: 2007-07-04
Posts: 101

Re: Folder with number of files

This is almost it.
One problem, I have a new folder created at each run with the new number.
OLD:

C:\TEST\test1.txt
C:\TEST\test2.txt
C:\TEST2\test1.txt
C:\TEST2\test2.txt

RESULT

C:\TEST\
C:\TEST(2)\test1.txt
C:\TEST(2)\test2.txt
C:\TEST2\
C:\TEST2(2)\test1.txt
C:\TEST2(2)\test2.txt

Any way to rename the existent folder or maybe to delete the old folder after the rename (only when the rename occur) ?
Am I clear ?!!? I'm not sure !!

Last edited by ozzii (2010-04-17 08:57)


_________________
Do, or do not. There is no 'try.'" --  Jedi Master Yoda

Offline

#4 2010-04-17 09:51

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Folder with number of files

What I would do is add the folder instead of the files. (Add folders as files: http://www.den4b.com/wiki/ReNamer:Filte … lters_menu)

And then use this to get the new name for the folder

var
  Files: TStringsArray;
  BasePath: WideString;

begin
  SetLength(Files, 0);
  WideScanDirForFiles(FilePath, Files, False, False, False, '*.txt');
  
  FileName := FileName + ' (' + IntToStr(length(Files)) + ')';
end.

With the first script if you forget adding files to the list, the result could be a chaos.  (For example, "Folder (3)" with only 2 files inside)

But with this last script If there are non TXT files inside the folders, them will also be in the renamed folder. (Well, but I don't know if you wanted to separate the TXT from the other files)


If this software has helped you, consider getting your pro version. :)

Offline

#5 2010-04-17 14:32

ozzii
Senior Member
Registered: 2007-07-04
Posts: 101

Re: Folder with number of files

My first attempt with the first script was with just folders and not files, and had as a result (0) for all folders.
This is why i had to use files instead of folders.
This new script is good now with just folders  big_smile

Another question: how to also add "doc" files for searching. I have tried with adding '*.doc' after .txt but no luck. Same with OR; no luck. Maybe something like WideScanDirForFiles +=
I don't know pascal sorry  hmm


_________________
Do, or do not. There is no 'try.'" --  Jedi Master Yoda

Offline

#6 2010-04-17 16:24

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Folder with number of files

Well, I had to put the line SetLength(Files, 0); because it was remembering the last files and not counting from 0 each time.

Knowing that it doesn't reset itself automatically we can take advantage by adding after the first WideScanDirForFiles line, another similar, but changing only *.txt to *.doc

WideScanDirForFiles(FilePath, Files, False, False, False, '*.txt');
WideScanDirForFiles(FilePath, Files, False, False, False, '*.doc');

It works but I don't know if there's a better solution...

Last edited by SafetyCar (2010-04-17 16:28)


If this software has helped you, consider getting your pro version. :)

Offline

#7 2010-04-17 17:07

ozzii
Senior Member
Registered: 2007-07-04
Posts: 101

Re: Folder with number of files

I didn't try two WideScanDirForFiles !!!!!
Shame on me  tongue
I just add a delete rule in the first place to delete the old number so I didn't have duplicates.

Thanks for your help SafetyCar.


_________________
Do, or do not. There is no 'try.'" --  Jedi Master Yoda

Offline

Board footer

Powered by FluxBB