I need a file renamer script

Discuss and share scripts and script files...
Post Reply
xyka
Posts: 22
Joined: 21 Oct 2010 22:17

I need a file renamer script

Post by xyka »

Hi!

I'm a new user of XYplorer.
I need your help.

I have got

Folder1
Folder2
Folder3

where files in Folder1:
-video-TAG.avi
-Asub.srt
-Esub.srt
-Hsub.srt

I need a script (or button) which does this:

if I select video-TAG.avi and Asub.srt

and press the button

then the script renames Asub.srt to video-TAG.ENG.srt



.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: I need a file renamer script

Post by TheQwerty »

Give this a try:

Code: Select all

"Rename All SRT to Base_of_AVI.ENG.SRT"
  $items = Get('CountSelected');                                 //Get number of items.
  End $items < 2, 'You must select at least two files...';       //End if 2 items are not selected.
  $report = Report("{Ext}|{Basename}|{Fullname}<crlf>", 1);      //Get info for selected items.

  $newName = '';                                                 //Base name of AVI.
  $toRename = '';                                                //List of items to rename.
  $i = 0;
  while ($i < $items) {                                          //Loop through lines.
    $i++;
    $line = GetToken("$report", "$i", "<crlf>");                 //Extract line
    $ext = GetToken("$line", 1, '|');                            //Extract extension from line
    if ("$ext" LikeI "avi") {                                    //If extension is avi...
      $newName = GetToken("$line", 2, '|');                      //Store base name as new name.
    } elseif ("$ext" LikeI "srt") {                              //Else if extension is srt...
      $toRename = "$toRename|" . GetToken("$line", 3, '|');      //Add item to list to be renamed.
    }
  }

  End "$newName" LikeI '', 'Name of AVI file was not found...';  //End if name of AVI file was not found.
  End "$toRename" LikeI '', 'No SRT files found...';             //End if no SRT files were found.
  
  $toRename = SubStr("$toRename", 1);                            //Strip leading '|'.
  Rename 'b', "$newName.ENG", 'p', "$toRename";                  //Rename
If your selection includes multiple SRT files it will rename them all (and give them numbered suffixes per XY's usual).
If your selection includes multiple AVI files it will use the name of the lowest in the list.
If your selection includes anything that's not an AVI or SRT it will be ignored.

If your selection does not contain at least one AVI and SRT it will quit.

HTH...

kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

Re: I need a file renamer script

Post by kotlmg »

your code is perfectly working. but how to use the same code for different file extensions like mp4, mkv, vob and all media video formats?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: I need a file renamer script

Post by SkyFrontier »

You may want to consider a simple solution: personalize and cascade everything you want.
Edit the following section to match your needs:
if ("$ext" LikeI "avi") >> if ("$ext" LikeI "vob")
You may also want to edit the script captions:

Code: Select all

   "Rename All SRT to Match .VOB files"
and add a separator between the last entry and the new one:

Code: Select all

  Rename 'b', "$newName.ENG", 'p', "$toRename";                  //Rename
-
"Rename All SRT to Match .VOB files"
Even icons can turn your menu into a practical thing to deal with:
"Rename All SRT to Match .VOB files|:myco"
"Rename All SRT to Match .mp4 Files|:previewfull"

In your case, this is what it look like:

Code: Select all

"Rename All SRT to Match .avi files|:tag2"
  $items = Get('CountSelected');                                 //Get number of items.
  End $items < 2, 'You must select at least two files...';       //End if 2 items are not selected.
  $report = Report("{Ext}|{Basename}|{Fullname}<crlf>", 1);      //Get info for selected items.

  $newName = '';                                                 //Base name of AVI.
  $toRename = '';                                                //List of items to rename.
  $i = 0;
  while ($i < $items) {                                          //Loop through lines.
    $i++;
    $line = GetToken("$report", "$i", "<crlf>");                 //Extract line
    $ext = GetToken("$line", 1, '|');                            //Extract extension from line
    if ("$ext" LikeI "avi") {                                    //If extension is avi...
      $newName = GetToken("$line", 2, '|');                      //Store base name as new name.
    } elseif ("$ext" LikeI "srt") {                              //Else if extension is srt...
      $toRename = "$toRename|" . GetToken("$line", 3, '|');      //Add item to list to be renamed.
    }
  }

  End "$newName" LikeI '', 'Name of AVI file was not found...';  //End if name of AVI file was not found.
  End "$toRename" LikeI '', 'No SRT files found...';             //End if no SRT files were found.
 
  $toRename = SubStr("$toRename", 1);                            //Strip leading '|'.
  Rename 'b', "$newName.ENG", 'p', "$toRename";                  //Rename
-
"Rename All SRT to Match .VOB files|:myco"
  $items = Get('CountSelected');                                 //Get number of items.
  End $items < 2, 'You must select at least two files...';       //End if 2 items are not selected.
  $report = Report("{Ext}|{Basename}|{Fullname}<crlf>", 1);      //Get info for selected items.

  $newName = '';                                                 //Base name of AVI.
  $toRename = '';                                                //List of items to rename.
  $i = 0;
  while ($i < $items) {                                          //Loop through lines.
    $i++;
    $line = GetToken("$report", "$i", "<crlf>");                 //Extract line
    $ext = GetToken("$line", 1, '|');                            //Extract extension from line
    if ("$ext" LikeI "vob") {                                    //If extension is avi...
      $newName = GetToken("$line", 2, '|');                      //Store base name as new name.
    } elseif ("$ext" LikeI "srt") {                              //Else if extension is srt...
      $toRename = "$toRename|" . GetToken("$line", 3, '|');      //Add item to list to be renamed.
    }
  }

  End "$newName" LikeI '', 'Name of AVI file was not found...';  //End if name of AVI file was not found.
  End "$toRename" LikeI '', 'No SRT files found...';             //End if no SRT files were found.
 
  $toRename = SubStr("$toRename", 1);                            //Strip leading '|'.
  Rename 'b', "$newName.ENG", 'p', "$toRename";                  //Rename
-
"Rename All SRT to Match .mp4 Files|:previewfull"
  $items = Get('CountSelected');                                 //Get number of items.
  End $items < 2, 'You must select at least two files...';       //End if 2 items are not selected.
  $report = Report("{Ext}|{Basename}|{Fullname}<crlf>", 1);      //Get info for selected items.

  $newName = '';                                                 //Base name of AVI.
  $toRename = '';                                                //List of items to rename.
  $i = 0;
  while ($i < $items) {                                          //Loop through lines.
    $i++;
    $line = GetToken("$report", "$i", "<crlf>");                 //Extract line
    $ext = GetToken("$line", 1, '|');                            //Extract extension from line
    if ("$ext" LikeI "mp4") {                                    //If extension is avi...
      $newName = GetToken("$line", 2, '|');                      //Store base name as new name.
    } elseif ("$ext" LikeI "srt") {                              //Else if extension is srt...
      $toRename = "$toRename|" . GetToken("$line", 3, '|');      //Add item to list to be renamed.
    }
  }

  End "$newName" LikeI '', 'Name of AVI file was not found...';  //End if name of AVI file was not found.
  End "$toRename" LikeI '', 'No SRT files found...';             //End if no SRT files were found.
 
  $toRename = SubStr("$toRename", 1);                            //Strip leading '|'.
  Rename 'b', "$newName.ENG", 'p', "$toRename";                  //Rename
-
"Rename All SRT to Match .MKV files|:preview"
  $items = Get('CountSelected');                                 //Get number of items.
  End $items < 2, 'You must select at least two files...';       //End if 2 items are not selected.
  $report = Report("{Ext}|{Basename}|{Fullname}<crlf>", 1);      //Get info for selected items.

  $newName = '';                                                 //Base name of AVI.
  $toRename = '';                                                //List of items to rename.
  $i = 0;
  while ($i < $items) {                                          //Loop through lines.
    $i++;
    $line = GetToken("$report", "$i", "<crlf>");                 //Extract line
    $ext = GetToken("$line", 1, '|');                            //Extract extension from line
    if ("$ext" LikeI "mkv") {                                    //If extension is avi...
      $newName = GetToken("$line", 2, '|');                      //Store base name as new name.
    } elseif ("$ext" LikeI "srt") {                              //Else if extension is srt...
      $toRename = "$toRename|" . GetToken("$line", 3, '|');      //Add item to list to be renamed.
    }
  }

  End "$newName" LikeI '', 'Name of AVI file was not found...';  //End if name of AVI file was not found.
  End "$toRename" LikeI '', 'No SRT files found...';             //End if no SRT files were found.
 
  $toRename = SubStr("$toRename", 1);                            //Strip leading '|'.
  Rename 'b', "$newName.ENG", 'p', "$toRename";                  //Rename
...of course, until something better may come out of scripters' minds... :wink:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

Re: I need a file renamer script

Post by kotlmg »

your script worked like a charm. thanks a lot. if you can modify the above script as follows.
when two files are selected and above script is executed, both these files will be shifted or moved to new folder with the base name and subtitle name should change as per your code. why i am asking the above code because, some players if they find different subtitles in the same folder, other subtitles may be played with the media file instead of genuine subtitle file. and one more request is 1)is it possible to add the above script in the category with name subtitles so that when i click on subtitles, this code will be executed or loaded. for example, teracopy was added to the category with the following option.
::load "C:\Program Files\XYplorer\Scripts\Teracopy.xys"

i want the above feature so that i need not to load the script each time, but i want the category option like teracopy.

have a very good night. thanks in advance.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: I need a file renamer script

Post by TheQwerty »

Take 2, respecting DRY prinicples...

Code: Select all

"Rename and Move Video and Subtitle Files"
  $items = Get('CountSelected');                                 //Get number of items.
  End $items < 2, 'You must select at least two files...';       //End if 2 items are not selected.
  $vidExts = 'avi|vob|mp4|mkv|';                                 //List of acceptable video extensions (| must follow each one)
  $subExts = 'srt|';                                             //List of acceptable subtitle extensions (| must follow each one)
  
  $report = Report("{Ext}|{Basename}|{Fullname}<crlf>", 1);      //Get info for selected items.

  $newName = '';                                                 //Base name of AVI.
  $toRename = '';                                                //List of items to rename.
  $i = 0;
  while ($i < $items) {                                          //Loop through lines.
    $i++;
    $line = GetToken("$report", "$i", "<crlf>");                 //  Extract line
    $ext = GetToken("$line", 1, '|');                            //  Extract extension from line
    if (StrPos("$vidExts", "$ext|") >= 0) {                      //  If extension implies video...
      $newName = GetToken("$line", 2, '|');                      //    Store base name as new name.
    } elseif (StrPos("$subExts", "$ext|") >= 0) {                //  Else if extension implies subtitles...
      $toRename = "$toRename|" . GetToken("$line", 3, '|');      //    Add item to list to be renamed.
    }
  }

  End "$newName" LikeI '', "Name of video file was not found...<crlf>This script supports videos of types:<crlf>$vidExts";  //End if name of video file was not found.
  End "$toRename" LikeI '', "No subtitle files found...<crlf>This script supports subtitles of types:<crlf>$subExts";       //End if no subtitle files were found.
 
  $toRename = SubStr("$toRename", 1);                            //Strip leading '|'.
  Rename 'b', "$newName.ENG", 'p', "$toRename";                  //Rename

  $items = ListFolder("<curpath>", "$newName*", 1);              //Guess which items were originally selected. (Neccessary because Rename deselects.)
  $items = Report("+{Fullname}|", "$items");                     //Format items for InputSelect.
  $items = InputSelect("Select the items to move to '$newName\'.<crlf>Cancel will leave all items where they are.", "$items",, 3);  //Allow user to verify/adjust items that will be moved.
  if ("$items") {
    MoveTo("$newName", "$items");                                   //Move items to 'Base_of_Video\'
  }

If you want it to accept more extensions you can edit these two variables:

Code: Select all

  $vidExts = 'avi|vob|mp4|mkv|';                                 //List of acceptable video extensions (| must follow each one)
  $subExts = 'srt|';                                             //List of acceptable subtitle extensions (| must follow each one)

The Rename deselects items so the last four lines attempt to guess which items you want moved based on any items that match "BaseName*". What this really means is that there is a chance that it will want to move more items than your original selection. Thus, I've inserted an InputSelect so that the user must verify the items that will be moved and has the opportunity to uncheck any that shouldn't be moved.


You can save the script to a file in the scripts directory (Go -> Go to Scripts Folder) and then use ::Load('FileName'); in the catalog, or create a new catalog item, open its properties, click Edit (next to location) and paste the script there.

kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

Re: I need a file renamer script

Post by kotlmg »

thanks a lot. your script is working like a charm.

kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

Re: I need a file renamer script

Post by kotlmg »

hello sir, right now i am getting file rename options with right click, but i want to access the file rename options using the script and also from the catalog. can you please give me the link to file rename script or can you please add rename script?
thanks a lot in advance.
Last edited by kotlmg on 28 Oct 2010 10:30, edited 1 time in total.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: I need a file renamer script

Post by TheQwerty »

I'm not sure I understand what you want...

If you want to right click on the selection in the list and select the script from the displayed context menu that is not possible.
Do you mean something else?

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: I need a file renamer script

Post by SkyFrontier »

...in the spirit of "I get you right", you may want to get easier access to the script using the Menu for Favorites Menu script.
-Load it into the UDC;
-Assign a CKS to it (SHIFT+F, for Favorites);
-Put this "SRT handler" script into a .txt and save it say as "SRThandler.XYS", into <xyscripts> folder;
-Call SHIFT+F and choose in which menu you'll want it to be displayed; edit it via "Edit/Check Favorites File"/"Edit/Check PERMAFavorites File", adding the following line:

Code: Select all

"SRT Handler|:steps" load SRThandler
("SRT Handler|:steps" stands for "Caption|icon"...);
-Now you can invoke the script via menu, too...
(I'd find it easier to access it via UDC/CKS, anyway...)
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

xyka
Posts: 22
Joined: 21 Oct 2010 22:17

Re: I need a file renamer script

Post by xyka »

thank you very much

you are fantastic



I need a help again

I need script that mass renames SRTs using AVI file basename

Code: Select all

http://www.xyplorer.com/xyfc/viewtopic.php?f=7&t=5777

-

Post Reply