Page 1 of 1

Replace dot / underscores in file- and foldernames?

Posted: 10 Mar 2011 23:04
by highend
Hi,

I frequently have files and sometimes folders that contain "." or "_" as separators between words instead of spaces.

Can anybody share a script that replaces all occurences of these separators into spaces?

It would be nice if it works with several selected files.

Regarding files: the last dot as the separator for the file extension should not be replaced :)

Tia,
Highend

Re: Replace dot / underscores in file- and foldernames?

Posted: 10 Mar 2011 23:07
by zer0
Piece of cake. Code below takes care of both situations.

Code: Select all

rename s, "./ ";
  rename s, "_/ ";

Re: Replace dot / underscores in file- and foldernames?

Posted: 10 Mar 2011 23:10
by highend
Ok, a lot easier than I though it would be.

Thank you, zer0!

Re: Replace dot / underscores in file- and foldernames?

Posted: 10 Mar 2011 23:20
by highend
Mh, maybe I was a bit too fast...

e.g.: Firefox.Portable.x86.v3.66.5.zip

Is there a way to rename it to:

Firefox Portable x86 v3.66.5.zip

Maybe via a regexp replace?

All dots between a v (case insensitive) and digits or several digits without a v (version information) shouldn't be replaced ;)

Re: Replace dot / underscores in file- and foldernames?

Posted: 11 Mar 2011 07:54
by Stefan
highend wrote:All dots between a v (case insensitive) and digits or several digits without a v (version information) shouldn't be replaced ;)
And who should guess what part is the version and what belongs to the name?
You need some anchor for the script to know what to do.

Firefox.Portable.x86.v3.66.5.zip
Is that in your case always ^(all signs till)(v followed by some digits)(ext)$ ?

Then what about
Firefox.Portable.x86.3.66.5.zip ?
Here the expression would be
^(all signs till x86)(followed by an dot and some digits)(ext)$ ?

How many cases do you have else?

-

Do you want to rename several files in one go or can you rename file by file?
If the later you can have an script to prompt you to select the name-part to work on only,
or the version number to exclude from change, or prompt you for each single dot to replace or break the script...

-

Re: Replace dot / underscores in file- and foldernames?

Posted: 11 Mar 2011 10:19
by highend
About 95% of the files follow these formatting rules:

name.description1.description2.descriptionX.v(versionnumber with digits).ext
or
name_description1_description2_descriptionX_v(versionnumber with digits).ext

Without the brackets ofc.

So the v followed by digits (and dots) until the last dot for the extension can be the anchor
for the script.

A script that renames all selected files at once would be prefered and I can live
with the 5% of files where I have to reenter a few dots when the v as the delimiter
is not found and the version number dots are replaced with spaces.

Regards,
Highend

Re: Replace dot / underscores in file- and foldernames?

Posted: 11 Mar 2011 13:45
by Stefan
OK, i had something like this in mind:

FROM:

Code: Select all

name2_description1_description2_descriptionX86_v1.2.3.ext
name2_description1_description2_descriptionX86_v1.2.ext
name2_description1_description2_descriptionX86_v1.ext
name2_description1_description2_descriptionX_1.2.3.ext
name2_description1_description2_descriptionX_v1.2.ext
name2_description1_description2_descriptionX_v1.ext
name.description1.description2.descriptionX86.v1.2.3.ext
name.description1.description2.descriptionX86.v1.2.ext
name.description1.description2.descriptionX86.v1.ext
name.description1.description2.descriptionX.1.2.ext
name.description1.description2.descriptionX.v1.2.3.ext
name.description1.description2.descriptionX.v1.ext

TO

Code: Select all

name2 description1 description2 descriptionX86 v1.2.3.ext
name2 description1 description2 descriptionX86 v1.2.ext
name2 description1 description2 descriptionX86 v1.ext
name2_description1_description2_descriptionX_1.2.3.ext
name2 description1 description2 descriptionX v1.2.ext
name2 description1 description2 descriptionX v1.ext
name description1 description2 descriptionX86 v1.2.3.ext
name description1 description2 descriptionX86 v1.2.ext
name description1 description2 descriptionX86 v1.ext
name description1 description2 descriptionX v1.2.3.ext
name.description1.description2.descriptionX.1.2.ext
name description1 description2 descriptionX v1.ext

DO:
USE THIS SCRIPT WITH TESTFILES ONLY/FIRST

The char "v" followed by digits (and dots) until the last dot for the extension is the anchor for this script.
When the "v" as the delimiter is not found the renaming is skipped.

Code: Select all

//v9.90.0304 - 2011-02-13 ,  + Scripting: Added foreach loops
//
//http://www.xyplorer.com/xyfc/viewtopic.php?p=57514#p57514
//The char "v" followed by digits (and dots) until the last dot for the extension is the anchor for this script.
//When the "v" as the delimiter is not found the renaming is skipped.
//
//


global $FileName, $InStrRev, $Sign;
  // $ITEMS = get("SelectedItemsPathNames", "|");
  foreach($token, <get SelectedItemsNames |>)
  {
      $FileName = $token;  //ForEach don't set value to *global* vars?!
      
      //split extension:
      $Sign = "."; sub "_InStrRev";
      $Base = substr($FileName, 0, $InStrRev -1);
      $Exte = substr($FileName, $InStrRev);
         
      //split name base part into two parts at "v[:Digits:]":
      $Part1 = regexreplace($Base, "(.+?)[_.](v[\d.]+)", "$1");
      $Part2 = regexreplace($Base, "(.+?)[_.](v[\d.]+)", "$2");
      
      //only if the regex did match:
      if ($Part1 != $Part2)
      {
          //do replacing in part1:
          $Part1 = regexreplace($Part1, "[._]", " ");
          
         //debug:
         msg "$FileName<crlf 2>$Part1 $Part2.$Exte<crlf 3>Press cancel to stop this script...",1;
        
         //do the renaming while leave the extension as it is:
         rename, "$Part1 $Part2", , "<curpath>\$FileName"; 
      }
  }
  
  
"_InStrRev" //Function

/*
  "_InStrRev" gives last possition of $Sign in $FileName

  Syntax: 
  $Sign = ".";  
  sub "_InStrRev";
  Return: last pos of $Sign in $FileName
*/

   global $FileName, $InStrRev, $Sign;
   $Index=0;
   while($Index < strlen($FileName))
   {
     If ($FileName==""){break;}
     $check = strpos( $FileName, "$Sign", strlen($FileName) - $Index );
     If ($check > -1){$InStrRev = $check +1; break;}
     $Index++;
   }

-----
EDIT:

first try to rename the remaining files too:

Not ready.... needs some tests... use with care...

Code: Select all

//v9.90.0304 - 2011-02-13 ,  + Scripting: Added foreach loops
global $FileName, $InStrRev, $Sign;
  // $ITEMS = get("SelectedItemsPathNames", "|");
  foreach($token, <get SelectedItemsNames |>)
  {
     $FileName = $token;  //ForEach don't set value to *global* vars?!
     $FilePath = "<curpath>";
     
     //split extension:
     $Sign = "."; sub "_InStrRev";
     $Base = substr($FileName, 0, $InStrRev -1);
     $Exte = substr($FileName, $InStrRev);
        
     //split name base part into two parts at "v[:Digits:]":
     $Part1 = regexreplace($Base, "(.+?)[_.](v[\d.]+)", "$1");
     $Part2 = regexreplace($Base, "(.+?)[_.](v[\d.]+)", "$2");
      
     //only if the regex did match:
     if ($Part1 != $Part2)
     {
        //do replacing in part1:
        $Part1 = regexreplace($Part1, "[._]", " ");
         
        //debug:
        //msg "$FileName<crlf 2>$Part1 $Part2.$Exte<crlf 3>Press cancel to stop this script...",1;
        
        //do the renaming:
        rename, "$Part1 $Part2", , $FilePath\$FileName; 
     }
     else
     {
        ////////////// ADDITIONAL
        //rename files without match criteria "v[:Digits:]" too:
        //replace all underscore by an space
        rename s, "_/ " , , $FilePath\$FileName; 
        //replace all dots by an space (you have to reenter the file/ext delimiter)
        rename s, "./ " , , $FilePath\$FileName; 
     }
  }


"_InStrRev"
//"_InStrRev" Function gives last possition of $Sign in $FileName
//Syntax:   $Sign = ".";   sub "_InStrRev";   Return: last pos of $Sign in $FileName
   global $FileName, $InStrRev, $Sign;
   $Index=0;
   while($Index < strlen($FileName))
   {
      If ($FileName==""){break;}
      $check = strpos( $FileName, "$Sign", strlen($FileName) - $Index );
      If ($check > -1){$InStrRev = $check +1; break;}
      $Index++;
   }

Re: Replace dot / underscores in file- and foldernames?

Posted: 11 Mar 2011 21:24
by highend
Hi Stefan,

tried both versions but the only thing I get when I have selected a single file in the active pane and use Scripting - Run Script (and copy & paste your code) is a popup with:

Output for your edited code:

global $FileName, $InStrRev, $Sign;forea...
}

XYplorer 9.90.0404

Re: Replace dot / underscores in file- and foldernames?

Posted: 11 Mar 2011 21:31
by Stefan
Hmm, did your script looks exactly like mine? With all the indents?

Note:

Common questions, mistakes and pitfalls
from
http://www.xyplorer.com/xyfc/viewtopic. ... 240#p47240
Common questions, mistakes and pitfalls
- - - - - - - - - - - - - - - - - -
Note that for each script only the first line starts at column position 1!
All other following line are indented by at least one space/blank.

Re: Replace dot / underscores in file- and foldernames?

Posted: 11 Mar 2011 21:44
by highend
After inserting the copied code into the text editor I'm using (EditPlus),

the second line
global $FileName, $InStrRev, $Sign;

and the last line
}

weren't intended.

After intending them with one space in front and starting the script once more I get an "XYplorer Scripting" window.

line 6 is highlighted in light blue: sub "_InStrRev";
and the lower part (under Error & current command (parsed and resolved):
I get:

The list of labels did not match any script in Script resource.
sub
_InStrRev

If I click on "Continue" the script is resumed and the file is renamed (but not correctly) :)

Excel.Berechnungen.v1.246.3.zip
Excel Berechnungenzi v1.246.3.zi.zip

Re: Replace dot / underscores in file- and foldernames?

Posted: 12 Mar 2011 21:59
by Stefan
OK ;-) i have to be more clear:


My statement about "first line" was not exactly enough.
I will try to describe it better:
Summarized:
  • Only the first line of an script are allowed to (and should) start at position 1.
    Script "Caption"-lines MUST start at position 1.
          Following lines MUST be indented.
    Comment line could start at position 1 since the are ignored.
    An exception is the "Heredoc Syntax".
See XYplorer help "Advanced Topics > Scripting" for more.
I have also updated my explanations and examples at http://www.xyplorer.com/xyfc/viewtopic.php?p=47240


>>the second line
>>   global $FileName, $InStrRev, $Sign;
>>and the last line
>>   }
>>weren't intended.

The "second line" is the first CODE line because //comment lines are ignored/skipped.
So this line are allowed to start at pos 1.

The "}" has to be indented too, that's right.
That it is not above in my script is because the forum swallows first space from pasted lines
and i have to take care to indent by two spaces at least. It seams i have missed that "}". Sorry.
I will go editing my script above right now.




>>The list of labels did not match any script in Script resource.
>>sub
>> _InStrRev

The caption of an script must not be indented and has to start at pos 1.
Then the second script ("_InStrRev") will be found.



>>If I click on "Continue" the script is resumed and the file is renamed (but not correctly)
>>Excel.Berechnungen.v1.246.3.zip
>>Excel Berechnungenzi v1.246.3.zi.zip

It's because the InStrRev function was not found and so the substr() command did an wrong calculation.



In general, as long as one didn't know the rules, there script has to look exactly as the posted scripts (if the coder has did it right :roll: )

HTH? :D

Re: Replace dot / underscores in file- and foldernames?

Posted: 12 Mar 2011 22:32
by highend
Thank you Stefan!

Long explanation how intending has to be done to get scripts working but I've never seen these rules before and was wondering in the past why some of my small scripts weren't working correctly *g* Now I've got it.

Renaming works fine so far with your second script (and copying the InStrRev function from the first one into it).

One minor thing which you probably have a solution for, too?

If I have a file like Excel_Berechnungen_2.0.0.4317.zip (the v is missing)
the script will rename it correctly too but I an error message with
"Rename failed"; "The system cannot find the file..."

The reason is the else statement in the script

Code: Select all

	else
	{
		////////////// ADDITIONAL
		//rename files without match criteria "v[:Digits:]" too:
		//replace all underscore by an space
		rename s, "_/ " , , $FilePath\$FileName; 
		//replace all dots by an space (you have to reenter the file/ext delimiter)
		rename s, "./ " , , $FilePath\$FileName; 
	}
It will find two underscores in the file and rename it afterwards and then it'll try to rename it a second time (and therefore doesn't find the original file).

Is there a way to check for the new file name before the second rename statement is executed?

It will not fail with files that have only dots (and no underscores in it) but it wouldn't be only a cosmetic problem if a file includes dots and underscores :)

Only if it isn't too difficult / takes too much time^^

Thanks & regards,
Highend

Re: Replace dot / underscores in file- and foldernames?

Posted: 13 Mar 2011 13:13
by highend
My temporary solution (which will only work with a single file) is:

Code: Select all

	else
	{
		////////////// ADDITIONAL
		//rename files without match criteria "v[:Digits:]" too:
		//replace all underscore by an space
		rename s, "_/ " , , $FilePath\$FileName;
		//will only work with single files:
		$FileNameBefore = get("SelectedItemsNames", "|");

		//replace all dots by an space (you have to reenter the file/ext delimiter)
		rename s, "./ " , , $FilePath\$FileNameBefore;
	}
Is there a way to parse the rename command into a variable and use that name instead?

I know the following code isn't working but maybe it can be changed?

Code: Select all

	else
	{
		////////////// ADDITIONAL
		//rename files without match criteria "v[:Digits:]" too:
		//replace all underscore by an space
		$FileNameBefore = rename s, "_/ " , , $FilePath\$FileName;
		rename s, "_/ " , , $FilePath\$FileName;

		//replace all dots by an space (you have to reenter the file/ext delimiter)
		rename s, "./ " , , $FilePath\$FileNameBefore;
	}

Re: Replace dot / underscores in file- and foldernames?

Posted: 14 Mar 2011 18:06
by zer0
If you got it to work with a single file, can't you loop through the list of selected files and apply the necessary command to each item?