#1 2008-08-03 01:07

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

Folder Rename strictly using Pascal Rule

Hi, this may have already been covered... if it has been I apologize. I ultimately wanted to rename every unique subfolder to an incremented "Folder" name.

For example, given these:

C:\Main\SubfolderA\MyFile1.txt
C:\Main\SubfolderA\MyFile2.txt

C:\Main\SubfolderB\MyFile1.xls
C:\Main\SubfolderB\MyFile2.xls
C:\Main\SubfolderB\MyFile3.xls

C:\Main\SubfolderC\SubSubFolderD\MyFile1.mdb
C:\Main\SubfolderC\SubSubFolderD\MYFile2.mdb
C:\Main\SubfolderC\SubSubFolderD\MyFile3.mdb

I wanted an output of this:

C:\Folder1\renamedfile1.txt
C:\Folder1\renamedfile2.txt
C:\Folder2\renamedfile3.xls
C:\Folder2\renamedfile4.xls
C:\Folder2\renamedfile5.xls
C:\Folder3\renamedfile6.mdb
C:\Folder3\renamedfile7.mdb
C:\Folder3\renamedfile8.mdb

The number after "Folder" doesn't have any meaning, I just wanted it to be incremented, and unique for each new subfolder.

Another post had stated that you could append to the file's folder structure by:

FileName := 'Folder'(Incremented Number Here) + '\' + FileName;

Is there a way in Pascal Rule to get this incremented number (say, starting at 1) and going up one for each new subfolder encountered?

:/I had thought about somehow appending a counter to the subfolder, then use the following code, and afterwards getting rid of some unwanted path:

const
  DELIM = '\';
var
  I: Integer;
  Folders: TStringsArray;
begin
  Folders := WideSplitString(WideExtractFileDir(FilePath), '\');
  for I:=Length(Folders)-1 downto 1 do
    FileName := Folders[i] + DELIM + FileName;
end.

I would like to stick with Pascal Rule if possible. Thank-you for your time and help!

Offline

#2 2008-08-08 16:11

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

Re: Folder Rename strictly using Pascal Rule

Kindly clear some doubts for us first before we can help with the script:

1) How will C:\Main\SubfolderA become C:\Folder1 and similarly C:\Main\SubfolderB become C:\Folder2? What is happening to the Main folder?

2) Also, how will C:\Main\SubfolderC\SubSubFolderD become C:\Folder3? Does Folder3 correspond to SubfolderC or SubSubFolderD?

3) Do you want the following to take place instead:

C:\Main\SubfolderA\MyFile1.txt
C:\Main\SubfolderA\MyFile2.txt

C:\Main\SubfolderB\MyFile1.xls
C:\Main\SubfolderB\MyFile2.xls
C:\Main\SubfolderB\MyFile3.xls

C:\Main\SubfolderC\SubSubFolderD\MyFile1.mdb
C:\Main\SubfolderC\SubSubFolderD\MYFile2.mdb
C:\Main\SubfolderC\SubSubFolderD\MyFile3.mdb

should become

C:\Main\Folder1\renamedfile1.txt
C:\Main\Folder1\renamedfile2.txt

C:\Main\Folder2\renamedfile3.xls
C:\Main\Folder2\renamedfile4.xls
C:\Main\Folder2\renamedfile5.xls

C:\Main\Folder3\Folder4\renamedfile6.mdb
C:\Main\Folder3\Folder4\renamedfile7.mdb
C:\Main\Folder3\Folder4\renamedfile8.mdb

If this is what you actually want, then it should involve having two counters, one each for files and folders. They should be appended as required to folder or filenames and renaming carried out accordingly.

Offline

#3 2008-08-09 01:42

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

Re: Folder Rename strictly using Pascal Rule

Thank you for your time and help with this... yes, this is what I wanted. Your question #1, The main folder is staying there and remaining unchanged... I had forgotten to write that down in the output. For your question #2, "Folder3" corresponds to SubSubFolderD in my example.

Let me rewrite the example...

Given these folders/files:

C:\MainFld\MySubFldA\MyFile1.txt
C:\MainFld\MySubFldB\MyFile2.xls
C:\MainFld\MySubFldC\MySubSubFldA\MyFile3.doc
C:\MainFld\MySubFldC\MySubSubFldA\MyFile4.doc
C:\MainFld\MySubFldC\MySubSubFldB\MyFile5.mdb
C:\MainFld\MySubFldC\MySubSubFldB\MyFile6.mdb

I needed to have the folder structure changed to this:

C:\mainFld\Fld1\RenamedFile1.txt
C:\mainFld\Fld2\RenamedFile2.xls
C:\MainFld\Fld3\RenamedFile3.doc
C:\MainFld\Fld3\RenamedFile4.doc
C:\MainFld\Fld4\RenamedFile5.mdb
C:\MainFld\Fld4\RenamedFile6.mdb

Etc... for everything in C:\MainFld...

I do want the renamed filename to have an incremented counter attached... for example the 1-6 in the output of the example above...

The "Fld3" above corresponds to the MySubSubFldA and not the MySubFldC
The "Fld4" above corresponds to the MySubSubFldB and not the MySubFldC

Currently the Pascal script I use sends the subfolder path information... just the MySubFldA or the MySubFldC\MySubSubFldA part of the path... to a text file along with each renamed filename (delimited with a comma). It would be great if I could also get the new "Fld" name to go to the same text file... so I would get an output to the text file of "old file name, new file name, old subfolder name, new "Fld" folder name"... I guess it could involve just removing more of the full path, but I'm not sure how to write the Pascal script.

Finally, and I know that I keep adding on to this... I apologize... I also wanted to somehow output the last "Fld" number that was used after a run. For example, after running renamer once and going through all folders of the MainFld, it then sends the last "Fld" number used to a temp/txt file... then if the Pascal script is run again in the future (I plan to run it again on another set of subfolders), it reads the temp/txt file and starts the incrementing at that number...

Is this possible to do this using just Pascal script??

Thank you again for all your time and help. I greatly appreciate it.

Offline

#4 2008-08-09 17:06

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

Re: Folder Rename strictly using Pascal Rule

clapp wrote:

I needed to have the folder structure changed to this:

C:\mainFld\Fld1\RenamedFile1.txt
C:\mainFld\Fld2\RenamedFile2.xls
C:\MainFld\Fld3\RenamedFile3.doc
C:\MainFld\Fld3\RenamedFile4.doc
C:\MainFld\Fld4\RenamedFile5.mdb
C:\MainFld\Fld4\RenamedFile6.mdb

The "Fld3" above corresponds to the MySubSubFldA and not the MySubFldC
The "Fld4" above corresponds to the MySubSubFldB and not the MySubFldC

See, this is the point I wanted to clear up... What happens to MySubFldC after renaming the files? You want to rename the files and move them as well?

If you want the folders/files to be renamed to:

C:\MainFld\Fld3\Fld4\RenamedFile3.doc
C:\MainFld\Fld3\Fld4\RenamedFile4.doc
C:\MainFld\Fld5\Fld6\RenamedFile5.mdb
C:\MainFld\Fld5\Fld6\RenamedFile6.mdb

then that should be easy, but moving the files in MySubSubFldA and MySubSubFldB up one level might not be that easy. Plus, what if there are further sub-dirs. like MySubSubFldA\MySubSubFldAA and MySubSubFldB\MySubSubFldBB? What then?

clapp wrote:

Currently the Pascal script I use sends the subfolder path information... just the MySubFldA or the MySubFldC\MySubSubFldA part of the path... to a text file along with each renamed filename (delimited with a comma). It would be great if I could also get the new "Fld" name to go to the same text file... so I would get an output to the text file of "old file name, new file name, old subfolder name, new "Fld" folder name"...

Ok, that should be possible... So you want to rename the folders/files and also output the result to a .CSV (Comma Separated Values) file in the form "OldFileName, NewFileName, OldDirName, NewDirName", right?

clapp wrote:

For example, after running renamer once and going through all folders of the MainFld, it then sends the last "Fld" number used to a temp/txt file... then if the Pascal script is run again in the future (I plan to run it again on another set of subfolders), it reads the temp/txt file and starts the incrementing at that number...

Hmm, I'm not too sure re. this point. I'm finding my way around PascalScript as well, so maybe some of the other more experienced people on this forum can pitch in, but I'll certainly check and get back to you if it's possible. Trying it out is the best way IMO to learn new things! smile

Offline

#5 2008-08-09 21:31

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

Re: Folder Rename strictly using Pascal Rule

Hi... I believe the MySubfldC folder would still remain there under the MainFld (along with it's two folders MySubFldC\MySubSubFldA and MySubFldC\MySubSubFolderB)... these folders would then just be empty of files.

My thought was that following the running of the rename script... if, for example, the first file was renamed to "C:\MainFld\Fld1\RenamedFile1.txt" ... that the file would automatically be moved via the script... from C:\MainFld\MySubFldA to the newly created Fld1 folder. So I guess the MainFld would still have the 3 original subfolders (one of which has it's own 2 folders)... they would just be empty... and the MainFld would have 4 new folders (Fld1-Fld4) where the renamed files would now be.

So yes... I wanted to rename the files, and have them (consequently) moved as well... to those new "Fld" folders.

-----------

Second question... if there were further sub-directories... for example given these folders/files:

C:\MainFld\MySubFldC\MySubSubFldA\MyFile7.txt
C:\MainFld\MySubFldC\MySubSubFldB\MyFile8.doc
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldCC\MyFile9.xls
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldCC\MyFile10.xls
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldDD\MyFile11.mdb
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldDD\MyFile12.mdb

I would want the same type of output:

C:\MainFld\Fld5\RenamedFile7.txt
C:\MainFld\Fld6\RenamedFile8.doc
C:\MainFld\Fld7\RenamedFile9.xls
C:\MainFld\Fld7\RenamedFile10.xls
C:\MainFld\Fld8\RenamedFile11.mdb
C:\MainFld\Fld8\RenamedFile12.mdb

... so I guess the way to say this... I would want a unique "Fld" name for every unique folder structure or path encountered. Even if the difference is at the lowest possible level. For example, these two files:

C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldCC\MyFile9.xls
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldCC\MyFile10.xls

would get the same "Fld" number (be moved to the same Fld7 folder following renaming)... and these two:

C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldDD\MyFile11.mdb
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldDD\MyFile12.mdb

would be moved to the same Fld8 folder following the renaming.

-----------

From reading other posts, it seems one challenge would be to have certain files renamed before others. If it is necessary for the script to rename folders (as opposed to just renaming files and appending folder structure)... subfolders require renaming prior to parent folders. If parent folders are renamed first, subfolders fail to rename because their paths are then changed. I'm not sure though if it is necessary for the script to rename the folders... and if it is necessary, if there is a way to sort the folders prior to renaming via Pascal script.

-----------

I hope I haven't confused the original goal... I have searched on the net for a way to do this, but haven't really found anything. Thank you again for your time and help, and for your quick responses.:)

Offline

#6 2008-08-10 05:08

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

Re: Folder Rename strictly using Pascal Rule

Ok, this was my first (feeble) attempt at the required script:

var
  Initialized: Boolean;
  DirCnt, FileCnt : Integer;

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

  if(WideDirectoryExists(FilePath)) then
  begin
    DirCnt := DirCnt + 1;
    FileAppendContent('List.txt', FileName);
    FileName := 'Dir' + IntToStr(DirCnt);
    FileAppendContent('List.txt', ','+FileName+#13#10);
  end
  else
  begin
    FileCnt := FileCnt + 1;
    FileAppendContent('List.txt', FileName);
    FileName := 'File' + IntToStr(FileCnt);
    FileAppendContent('List.txt', ','+FileName+#13#10);
  end;
  FileWriteContent('Count.txt', 'Dir Count:'+#13#10+IntToStr(DirCnt)+#13#10+'File Count:'+#13#10+IntToStr(FileCnt));
end.

To run this, your Add Folders -> Filter Settings need to be as follows:

anomeq.png

and then you need to add the root folder (containing all the sub-folders and files to be renamed).

Note 1: This script doesn't tackle the moving problem yet.
Note 2: Main\A\B is renamed to Main\Dir1\Dir2 (instead of A being ignored and B being renamed to Dir1 to get Main\Dir1 as you want).
Note 3: Count.txt and List.txt are created wherever ReNamer.exe resides.

Unfortunately, while the preview works fine, the actual renaming doesn't! hmm roll This is because, as you correctly predicted, "subfolders require renaming prior to parent folders".

Another problem that could arise even with the final script (if we ever get there!) is that on every preview, the Count.txt and List.txt files are written to and read from, even though the other files haven't been renamed yet. I'm not sure yet if this can be termed a bug in ReNamer (have to ask Denis), but the fact is that FileReadLine(), FileAppendContent(), FileWriteContent() etc. run every time Preview is pressed.

So if the previous count in the Count.txt file is 10 for files and we run the script on 3 more files, if we just press Rename, the count continues from 11 onwards till 13 (which is correct). But if we press Preview before Rename, then the count in the file is updated to 13 even though the filenames haven't changed. Pressing Rename after this will actually name the files from 14 onwards till 16!

One final problem with regards to the proposed script - ReNamer and the WideScanDirForFiles()/WideScanDirForFolders() functions list files/folders in the order of creation apparently. So if MainDir\Dir2 is created before MainDir\Dir1, the order displayed is MainDir\Dir2 and then MainDir\Dir1. I suspect that this might mess up the proposed renaming as well (since we want to rename them in order), and so we might need to carry out some sorting as well.

Phew! This is not turning out to be as easy as I'd thought! wink Or maybe I'm going about this all wrong... hmm I'm still waiting for guys like krtek, Stefan, eR@SeR etc. to pitch in with their expertise!

Last edited by Andrew (2008-08-10 05:18)

Offline

#7 2008-08-11 17:30

eR@SeR
Senior Member
From: Земун, Србија
Registered: 2008-01-23
Posts: 354

Re: Folder Rename strictly using Pascal Rule

clapp wrote:

I would like to stick with Pascal Rule if possible. Thank-you for your time and help!

Andrew wrote:

I'm still waiting for guys like krtek, Stefan, eR@SeR etc. to pitch in with their expertise!

Well, I will readily help you and any other user if he have any question/problem with ReNamer but Pascal is mine really bad side i.e. I don't know "anything" about programming or scripts sad roll

I'm sorry sad


TRUTH, FREEDOM, JUSTICE and FATHERLAND are the highest morale values which human is born, lives and dies for!

Offline

#8 2008-08-11 19:58

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

Re: Folder Rename strictly using Pascal Rule

Hi,
A really tough problem...
And I have to ask you for few further things smile

I'll use your example:

C:\MainFld\MySubFldC\MySubSubFldA\MyFile7.txt
C:\MainFld\MySubFldC\MySubSubFldB\MyFile8.doc
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldCC\MyFile9.xls
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldCC\MyFile10.xls
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldDD\MyFile11.mdb
C:\MainFld\MySubFldC\MySubSubFldC\MySubSubSubFldDD\MyFile12.mdb


First question.
Is there any chance that there may be any files in folder MySubFldC or MySubSubFldC ?
In other words: Do files exist always only in the deepest folders or is it unknown and unpredictable?

Second question.
Is it of any importance for the MySubSubFldA to have lower number than MySubSubSubFldCC  in the new structure?
In other words: Do you need folders to be renamed in any particular (eg. alphabetical) order or maybe it is enough that they will have distinct names after renaming?



Andrew wrote:

Another problem that could arise even with the final script (if we ever get there!) is that on every preview, the Count.txt and List.txt files are written to and read from, even though the other files haven't been renamed yet. I'm not sure yet if this can be termed a bug in ReNamer (have to ask Denis), but the fact is that FileReadLine(), FileAppendContent(), FileWriteContent() etc. run every time Preview is pressed.

I guess we will need to use Yes/No question box to ask user if he/she wants to update Count.txt file. With warning that he/she should answer yes to that question only once per Rename operation...



I've got an idea of writing a script that will need pressing rename till script will tell that it finished renaming (it would check the depth of directory structure on the first go, and then with every go it would rename only folders of the deepest level untill it reach root folder).


Another idea is building separate directory tree for output. It would leave source directory tree untouched and walk through files to move them to new locations. After that operation you could delete old directory structure and replace it with the new one.
That script would need to create a map (for example in two TStringArrays) of old directories and their equivalents in new structure.
With every file script would have to check in the map if there already was created a directory in new structure for the directory the file lies in.


Tell me what you think about these ideas, guys.


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

#9 2008-08-12 17:56

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

Re: Folder Rename strictly using Pascal Rule

krtek wrote:

Second question.
Is it of any importance for the MySubSubFldA to have lower number than MySubSubSubFldCC  in the new structure?
In other words: Do you need folders to be renamed in any particular (eg. alphabetical) order or maybe it is enough that they will have distinct names after renaming?

If no order is required, then it will certainly make things much easier as we can make do without sorting the file list etc.

krtek wrote:

I guess we will need to use Yes/No question box to ask user if he/she wants to update Count.txt file. With warning that he/she should answer yes to that question only once per Rename operation...

I've got an idea of writing a script that will need pressing rename till script will tell that it finished renaming (it would check the depth of directory structure on the first go, and then with every go it would rename only folders of the deepest level untill it reach root folder).

All very nice, but it will also be very irritating for the user to go on pressing buttons in so many dialog boxes. That's what led to my suggestion #4 here.

krtek wrote:

Another idea is building separate directory tree for output. It would leave source directory tree untouched and walk through files to move them to new locations. After that operation you could delete old directory structure and replace it with the new one.

I was trying the same thing with WideScanDirForFiles()/WideScanDirForFolders() but got stuck up due to - i) the sorting issue and ii) the problem of arranging the rename list in such a way that innermost (children) files/folders are renamed before the outer ones (parents).

Also, regarding "deleting the old directory structure and replacing it with the new one", what if we have:

Main\A\1.txt
Main\B\2.txt
Main\C\D\3.txt
Main\Dir3

After renaming A to Dir1 and B to Dir2, do we rename D (skipping C) to Dir4? Or do we instead just move 3.txt into the existing Dir3?

krtek wrote:

That script would need to create a map (for example in two TStringArrays) of old directories and their equivalents in new structure.
With every file script would have to check in the map if there already was created a directory in new structure for the directory the file lies in.

Actually, I had thought of doing it with only one array. Every time a dir. is encountered, it is first checked if it already exists in the array. If so, it is skipped. If it doesn't, then it is numbered according to (current array length + 1) and then added to the array. So if the array contains 3 dirs. and a new 4th one is encountered, then we rename it to "Dir4" (3 + 1) and add it to the array.

Also, I think first the dirs. should be tackled and renamed and finally the files. What do you think?

Last edited by Andrew (2008-08-12 17:57)

Offline

#10 2008-08-12 18:28

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

Re: Folder Rename strictly using Pascal Rule

Sorry for not responding back sooner... and thank-you, everyone, for your time and help with this.

To answer your first question, the files will predictably only exist in the deepest folders.

Second question, the folders do not have to be renamed in any particular order... it is good enough that they have unique "Fld" names following renaming.

------------

Andrew wrote:


Another problem that could arise even with the final script (if we ever get there!) is that on every preview, the Count.txt and List.txt files are written to and read from, even though the other files haven't been renamed yet. I'm not sure yet if this can be termed a bug in ReNamer (have to ask Denis), but the fact is that FileReadLine(), FileAppendContent(), FileWriteContent() etc. run every time Preview is pressed.
I normally run Renamer from the command line... and I believe it just runs the rename with no preview necessary... so for me I don't believe this will be a problem.

------------

I had tried running another instance of Renamer (for example, Renamer_Folders.exe)... with different filter settings... to perform a serialization of the folders combined with a rename to "Fld". It seems (and I could be wrong) that renaming of the subfolders can be unpredictable... I clicked on the "Name" grey bar to sort so that the subfolders would be renamed prior to the parent folders... but still, every once in a while, it fails to rename a subfolder, because it renames the parent folder first. I ask if anyone knows of a way, either via Renamer or other way, to consistently ensure that all of the deepest subfolders are renamed (that their parent folders are not renamed first). Renamer does seem to save the folder sort... so when I add new folders it seems to attempt to sort them again with the deepest subfolders first... but still the sort/rename doesn't seem to be consistently deepest folders first... so a Pascal script (or some other solution) that would ensure this would be a lifesaver.

Another problem with this way, though I can avoid renaming the root folder via the filter settings, other subfolders (other than the deepest) are renamed... consequently, the deepest subfolders can be renamed to have a unique "Fld" name, however taken as a group, the deepest subfolders are then not sequential... the non-deepest subfolders are assigned some of the numbers of the rename...

... so if I look at just the deepest subfolders, though their new names are unique and ascend... numbers are skipped... for ex. Fld1, Fld2, Fld4, Fld5, Fld6, Fld8... if this makes sense. The problem is that I need just the deepest subfolders to be renamed... no other subfolders.

-------------

Thank-you all again for all of your time and hard work with this problem.

Offline

Board footer

Powered by FluxBB