script to select "n" no of characters from the selected file

Discuss and share scripts and script files...
Post Reply
kotlmg
Posts: 298
Joined: 30 Jun 2010 17:14

script to select "n" no of characters from the selected file

Post by kotlmg »

is there any xyplorer script or command available for selecting "n" characters from the selected files keeping extension not changed?
ex:
selected files
loot sdf gsgfgf dfhhfh gfg.vob
kiss sfsgdhfh hfhfhfh fgfsd .blog. fsdf.avi
murder sdfshhhf fhf fs.mov

result should renamed as
loot.vob
kiss.avi
murder.mov
Last edited by kotlmg on 17 Nov 2011 18:03, edited 1 time in total.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: script to select "n" no of characters from the selected

Post by Stefan »

"n" chars, or first word?

Try if this comes near:

Code: Select all


   end (get("CountSelected") == 0, "Nothing selected");
   
   $SelectedItems = report("{BaseName}?{Ext}|",1);
   $out="";
  
   foreach($Item, $SelectedItems) {
      if($Item == "") { break; }
      if(exists(replace("<curpath>\$Item","?",".")) != 1) { continue; } //no folders

      $base = gettoken($Item, 1, "?");
      $exte = gettoken($Item, 2, "?");

      $FirstSevenChars = substr($base, 0, 7) . ".$exte";
      $out = $out . $FirstSevenChars . "<crlf>";

//      $FirstWord = regexreplace($base,"(\w+)(.+)","$1") . ".$exte";
//      $out = $out . $FirstWord . "<crlf>";

//      $FirstTwoWords = regexreplace($base,"(\w+)\s(\w+)\s(.+)","$1 $2") . ".$exte";
//      $out = $out . $FirstTwoWords . "<crlf>";

   }
  text $out;



EDIT: improvement. Use gettoken() instead of regex.

Code: Select all


   end (get("CountSelected") == 0, "Nothing selected");
      
   $SelectedItems = report("{BaseName}?{Ext}|",1);
   $out="";
           
   foreach($Item, $SelectedItems) {
      if($Item == "") { break; }
      if(exists(replace("<curpath>\$Item","?",".")) != 1) { continue; } //no folders
              
      $base = gettoken($Item, 1, "?");
      $exte = gettoken($Item, 2, "?");

      //$FirstSevenChars = substr($base, 0, 7);
      //$out = $out . $FirstSevenChars  . ".$exte" . "<crlf>";
            
      $FirstWord  = gettoken($base, 1, " ");
      $SecondWord = gettoken($base, 2, " ");
      $out = $out . "$FirstWord $SecondWord.$exte" . "<crlf>";
   }
    
  text $out;



EDIT: more universal. Wrapped "GetWord" into an function.

Adjust "$WordCount = " to the amount of words you wanna get.

Code: Select all


   end (get("CountSelected") == 0, "Nothing selected");
     
   $SelectedItems = report("{BaseName}?{Ext}|",1);
   $out="";
           
   foreach($Item, $SelectedItems) {
      if($Item == "") { break; }
             
      $base = gettoken($Item, 1, "?");
      $exte = gettoken($Item, 2, "?");

      ////Get chars:
        //$FirstSevenChars = substr($base, 0, 7);
        //$out = $out . $FirstSevenChars  . ".$exte" . "<crlf>";

       ////Get words:
       $WordCount = 5;
       $words="";
       $Loop = 1;
       while( $Loop <= $WordCount){ 
         $words = $words .  gettoken($base, $Loop, " ") . " ";
         $Loop++;
        }
       $out =  $out . regexreplace($words, "(.+\b)\s*", "$1") . ".$exte<crlf>";


   }
   
  text $out;


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

Re: script to select "n" no of characters from the selected

Post by kotlmg »

hello stefan sir,
your first code is working for me. i have modified your code to suit my needs as follows interactively.

Code: Select all

$nochar =input("Enter no of characters to be selected");
   end (get("CountSelected") == 0, "Nothing selected");
   $SelectedItems = report("{BaseName}?{Ext}|",1);
   $out="";
  
   foreach($Item, $SelectedItems) {
      if($Item == "") { break; }
      if(exists(replace("<curpath>\$Item","?",".")) != 1) { continue; } //no folders

      $base = gettoken($Item, 1, "?");
      $exte = gettoken($Item, 2, "?");
      

      $FirstSevenChars = substr($base, 0, $nochar) . ".$exte";
      $out = $out . $FirstSevenChars . "<crlf>";

//      $FirstWord = regexreplace($base,"(\w+)(.+)","$1") . ".$exte";
//      $out = $out . $FirstWord . "<crlf>";

//      $FirstTwoWords = regexreplace($base,"(\w+)\s(\w+)\s(.+)","$1 $2") . ".$exte";
//      $out = $out . $FirstTwoWords . "<crlf>";

   }
  text $out;


with the above code no of characters selected are being displayed in another window.
how to rename the all the selected files with the no of selected words either from beginning or from end of file name excluding the extension?

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: script to select "n" no of characters from the selected

Post by Stefan »

Here is the code to rename the file after getting the chars.
Incl. a few other ideas.
* remove n chars from start
* remove n chars from end
* keep n chars from start, remove the rest
* keep n chars from end, remove the rest
* simple preview

Not that elegant implemented but the code should work.
Take what you want and drop the rest.



But for complicated modification you should rather use an real renamer like Siren.
See >> http://www.xyplorer.com/xyfc/viewtopic. ... 059#p57059

Or use File > Rename Special > RegEx Rename...
where you get an better preview overview.


Code: Select all


//// Get the first or the last few chars to rename the file to.

   end (get("CountSelected") == 0, "Nothing selected");   
   $CharAmount = input("Enter no of characters.<crlf>Use an leading minus - to get from the end of string.",,-3);
   $Methode = inputselect(Choose methode for that $CharAmount chars:, "+RemoveThisChars_KeepRest|KeepThisChars_RemoveRest", , 2);

   $SelectedItems = report("{BaseName}?{Ext}|",1);
   $out="";
  
   foreach($Item, $SelectedItems) {
        if($Item == "") { break; }
        if(exists(replace("<curpath>\$Item","?",".")) != 1) { continue; } //no folders
        
        $base = gettoken($Item, 1, "?");
        $exte = gettoken($Item, 2, "?");
        if ($exte==""){continue;}
        
           
          
        if ($Methode=="RemoveThisChars_KeepRest"){
           if strpos($CharAmount, "-") == -1){
               $GetChars = substr($base, $CharAmount);
           }else{
               $GetChars  = substr($base, 0, $CharAmount);
           }
        }
          
             
              
        if ($Methode=="KeepThisChars_RemoveRest"){
           if strpos($CharAmount, "-") == -1){
               $GetChars = substr($base, 0, $CharAmount);
           }else{
               $GetChars  = substr($base, $CharAmount);
           }
        }
            
             
        $oldname = replace($Item, "?", ".");
        $SKIP = input("Rename or cancel? - $Methode", , "$oldname<crlf>$GetChars.$exte", m, "SKIP");
        if ($SKIP=="SKIP"){continue;}
            
            
        renameitem( "$GetChars.$exte", $oldname, 0, "_001" );
   }


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

Re: script to select "n" no of characters from the selected

Post by kotlmg »

thanks sir. your code is working.

Post Reply