#1 2013-01-20 14:52

alcestneige
Member
Registered: 2013-01-19
Posts: 28

Renaming all files in folder according the folder name or rev

Hi all!

I'm wondering if there is a way to rename all files in one folder according the folder name or vice versa, to rename the folder according one file name in the folder.


An example:

I have a folder called 'Hero', in this folder is a movie file 'Hero_01.avi' and subtitle file 'Hero_subtitle.srt'.

Hero
   Hero_01.avi
   Hero_subtitle.srt

I would like to rename the movie and the subtitle file according the folder name 'Hero' (Hero.avi , Hero.srt) or the opposite, to rename the folder 'Hero' and the subtitle file 'Hero_subtitle.srt' according the movie file name 'Hero_01' (Hero_01 , Hero_01.srt).


So there would be:

Hero
   Hero.avi
   Hero.srt

or

Hero_01
   Hero_01.avi
   Hero_01.srt



I just need ONE solution, but I don't know which one is real and could be implement or if there is a chance at all to do something like this.


Thank you very much!
Your help is much appreciated!
Jan

Last edited by alcestneige (2013-01-20 14:55)

Offline

#2 2013-01-20 21:12

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

Re: Renaming all files in folder according the folder name or rev

Hi Jan.

i don't do this myself, so please test my solution with dummy files first.



To rename files with name from folder:

* add your folder to ReNamer
You have to enable Filter settings "Add folders as files" then you can rename them.

*add rule Replace: Replace using wildcards "*" with ":File_FolderName:" (skip extension)

Also see our wiki >
http://www.den4b.com/wiki/ReNamer:Rules:Replace  >> "Insert meta tag "
http://www.den4b.com/wiki/ReNamer:Renaming_folders

(Thanks to the wiki maintainers. Good work!)




To rename folder with name from first file from that folder

Try this script

//Rename Folder with name from file

//ReNamer settings:
//[Filters]
//  AddFilesFromFolders=0
//  AddFoldersAsFiles=1
//  IncludeSubfolders=0


var
   Files: TStringsArray;
   Folder,FileMask,FirstFile: WideString;
   Recursive,IncludeHidden,IncludeSystem: Boolean;

begin
  // --- Get all files from current proceeded folder ---------------------------------
    SetLength(Files, 0);//reset array
    Folder:= FilePath;//Folder to scan. Use current proceeded folder.
      //WideScanDirForFiles() Settings:
        Recursive:=False;
        IncludeHidden:=False;
        IncludeSystem:=False;
        FileMask:='*';
    WideScanDirForFiles(Folder, Files, Recursive, 
           IncludeHidden, IncludeSystem, FileMask);


  // --- Use first found file name from current proceeded folder----------------------
    FirstFile  := '';//empty var to get rid of side-effects
    if Length(Files) > 0 then
       FirstFile := WideExtractBaseName(Files[0]);
    

  // --- Rename Folder with name from file ------------------
    FileName := FirstFile;
 
end.

HTH?


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

#3 2013-01-21 19:18

alcestneige
Member
Registered: 2013-01-19
Posts: 28

Re: Renaming all files in folder according the folder name or rev

Thank you Stefan for your work!

The script works just fine, except of one detail.
It creates a new folder with the proper name of the movie file in the root folder and moves all the files into that folder as well, but the old folder with the wrong name is still there.


To imagine what I mean:

Before the script I have:

Movie
----------------
  Hero (2001)
    Hero.avi
    Hero.srt


After I run the script I have


Movie
----------------
  Hero
    Hero.avi
    Hero.srt

  Hero (2001)



Legend:
------------------------------
Green Color - Root / Main folder
Yellow Color - Subfolder / Movie folder
Blue Color - Files in the Subfolder / Movie folder


Do you have any idea what could be the problem?

Thank you!
Jan

Offline

#4 2013-01-21 20:59

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

Re: Renaming all files in folder according the folder name or rev

Huh? Moving to folder?

I guess you have created your own script by including the script from that other thread?
>>ReNamer: Move a file with specific extension into a Subfolder >> http://www.den4b.com/forum/viewtopic.php?pid=7342#p7342



alcestneige wrote:

Do you have any idea what could be the problem?

Of course the problem is no one have told ReNamer to delete that old folder big_smile


The function for that is:

http://www.den4b.com/wiki/ReNamer:Pasca … Management
function WideDeleteFile(const FileName: WideString): Boolean;
Since a folder is nothing more then a file with an special attribute, that should work for folders too.





The problem is, you can delete this folder only if all files are moved to the new folder.




So i think we need an script like this dummy code:

Parent := WideExtractFileDir(FilePath); //  X:\path\Movie\Hero (2001)
... here in between  you move-file-code
...
... and at the end
Files := WideScanDirForFiles(Parent,x,x);
if (Files=0) then
    WideDeleteFile(Parent);



Maybe i find some time to created such a script (the creation is not that problem, but the testing, to tell no nonsense)


.


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 2013-01-21 21:15

alcestneige
Member
Registered: 2013-01-19
Posts: 28

Re: Renaming all files in folder according the folder name or rev

Hi Stefan,

No no, I used only your code which you have wrote above (black background).

Now I tried to update your code (earlier) with also your code big_smile just above.

Is this correct?



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

//Rename Folder with name from file

//ReNamer settings:
//[Filters]
//  AddFilesFromFolders=0
//  AddFoldersAsFiles=1
//  IncludeSubfolders=0


var
   Files: TStringsArray;
   Folder,FileMask,FirstFile: WideString;
   Recursive,IncludeHidden,IncludeSystem: Boolean;

begin
Parent := WideExtractFileDir(FilePath);

  // --- Get all files from current proceeded folder ---------------------------------
    SetLength(Files, 0);//reset array
    Folder:= FilePath;//Folder to scan. Use current proceeded folder.
      //WideScanDirForFiles() Settings:
        Recursive:=False;
        IncludeHidden:=False;
        IncludeSystem:=False;
        FileMask:='*';
    WideScanDirForFiles(Folder, Files, Recursive,
           IncludeHidden, IncludeSystem, FileMask);


  // --- Use first found file name from current proceeded folder----------------------
    FirstFile  := '';//empty var to get rid of side-effects
    if Length(Files) > 0 then
       FirstFile := WideExtractBaseName(Files[0]);
   

  // --- Rename Folder with name from file ------------------
    FileName := FirstFile;

Files := WideScanDirForFiles(Parent,x,x);
if (Files=0) then
    WideDeleteFile(Parent);

end.

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


Because if yes, then this is the error code sad

---------------------------
Error
---------------------------
Preview process has been terminated due to a critical error:

Pascal Script Compile:
[Line 16] Error: Unknown identifier 'Parent'
---------------------------
OK   
---------------------------


Please remember that I'm not a programmer sad sad

Thanks Stefan

Offline

#6 2013-01-21 21:36

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

Re: Renaming all files in folder according the folder name or rev

Just an quick reply, wink


Unknown identifier 'Parent'

means you don't have told the script to use this var name.


You have to list all used vars under 'var' first! 
And they are case-sensitive.



var
   Files: TStringsArray; // Array
   Folder,FileMask,FirstFile: WideString; //String
   Recursive,IncludeHidden,IncludeSystem: Boolean; //False or True
   ExampleCounter:Integer; //digit



The rest of your script i can check later the week.


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

#7 2013-01-28 20:05

alcestneige
Member
Registered: 2013-01-19
Posts: 28

Re: Renaming all files in folder according the folder name or rev

Hi Stefan,
Haave you had a chance to look on this ?
I still have the same 'parent' error problem.
I added this 'ExampleCounter:Integer;' to 'var', but same error sad

Thanks for your time!
Jan

Offline

#8 2013-01-28 22:27

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

Re: Renaming all files in folder according the folder name or rev

Hi.

I am not sure if remember correct where we were last time. But let's see how far we can go this time.


1.)
WideScanDirForFiles(Parent,x,x);

was only a pseudo/dummy code. YOu can take a look into the wiki what are the real parameters instead of x,x



2.)
of course you have to put 'parent' to the var declaration section.
And 'parent' is a String, or better WideString.



3.)
"WideDeleteFile" seems not to work for folders.


4.)
so that "delete empty folder" added should look somehow like this:

Note i have NOT tested this code. I will do it tomorrow in my lunch break.



//Rename Folder with name from file

deleted

end.


To make it really work we have remove the two "//" in front of  //ExecuteProgram
And at the end we can change "cmd /k" to "cmd /c"


.

Last edited by Stefan (2013-01-29 08:06)


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

#9 2013-01-29 08:45

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

Re: Renaming all files in folder according the folder name or rev

Today I guess we mixed up thing here?


The script rename the parent folder with name from first file in that folder.
There is no moving files at all. But now you want to delete an empty folder?


Either i need some sleep, or you mixed two scripts and don't tell what you have done.

Please tell again what you want.
- how do it looks now
- how should it look

Then we start fresh...


Thanks.


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

#10 2013-01-29 11:47

alcestneige
Member
Registered: 2013-01-19
Posts: 28

Re: Renaming all files in folder according the folder name or rev

Hi Stefan,
I see you were busy smile
I know that this topic is getting messy and unclear big_smile

So I focus on your comment now:

The script rename the parent folder with name from first file in that folder.
There is no moving files at all. But now you want to delete an empty folder?

The script rename the parent folder with name from first file in that folder - CORRECT, folder is renamed according the movie file.

There is no moving files at all - NOT CORRECT, unfortunately as described before 2013-01-21 20:18, by using your script from 2013-01-20 21:12 a new folder with the correct name is created and the old folder with incorrect name is present. The incorrect folder is empty and the folder with the correct name has all the files in it. Maybe there is no moving of files, but in that case a new empty folder with incorrect name is created, so either one or the other.

But now you want to delete an empty folder? - CORRECT, as mentioned above, because now I have two folders and one is empty and has wrong name, we have to get rid of it.



So, maybe there is something missing in your code, maybe there is a bug, maybe the ReNamer can't handle this issue, or maybe I do something wrong by copy & paste big_smile

I think, that everything else in this topic can be ignored now and we should focus mainly on your script.

And ohh, I almost forget, it should look like described here 2013-01-20 15:52. (I'm sorry that I'm using this redirection's, but I would repeat myself, as I think it's described quite well. If not please let me know, I could try to create a video from desktop.)

Please let me know if you have some other questions to this, I would like to help you with this smile

Thanks!
Jan

Last edited by alcestneige (2013-01-29 11:54)

Offline

Board footer

Powered by FluxBB