Page 1 of 1

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

Posted: 17 Nov 2011 04:33
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

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

Posted: 17 Nov 2011 07:51
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;


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

Posted: 17 Nov 2011 17:55
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?

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

Posted: 17 Nov 2011 22:24
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" );
   }


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

Posted: 18 Nov 2011 03:59
by kotlmg
thanks sir. your code is working.