@JohnZeman:
Hi John,
I modified your Button a little bit for my needs:
-
To avoid to delete characters from the file extensions by mistake I used a technique leo often uses in Rename Scripts:
Stripping the extension before renaming files and adding it again at the end.
-
I added a second version of the Button that deletes characters at the choosen position counted from the end of the filename.
This is useful if you want to rename files with long names or delete characters right before a suffix.
I put both versions in a three-way-button in which a leftclick deletes characters at a position counted from the beginning of the name and a rightclick deletes characters counting from the end of the name backwards.
Here's the Code of the three-way-button:
Remove Characters Three-Way-Button (leftclick counting from left, rightclick counting from right side of the name)
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none" type="three_button">
<label>Remove Characters</label>
<icon1>#default:script</icon1>
<button backcol="none" display="both" textcol="none">
<label>Remove Characters (count from left)</label>
<tip>Removes a set number of characters from the selected file names at a position counted from left side of the names</tip>
<icon1>#default:script</icon1>
<function type="normal">
<instruction>// This script removes a set number of characters from the selected file and folder names</instruction>
<instruction>// Last modified by John Zeman on May 9, 2010</instruction>
<instruction />
<instruction>Rename TO="{DlgStringS|Enter a number that indicates the starting point\n\nThen add a comma,\n\nFinally add another the number which will be how many characters will be removed\n\n(Example: 2,5 would remove characters 2 through 6)|2,5}"</instruction>
<instruction>@nodeselect</instruction>
<instruction>@script vbscript</instruction>
<instruction>option explicit</instruction>
<instruction />
<instruction>Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )</instruction>
<instruction />
<instruction> Dim strNameOnly</instruction>
<instruction> Dim strExtension</instruction>
<instruction> Dim intComPos ' Location of the leftmost comma in dialog box entry</instruction>
<instruction> Dim strLeftNum ' Extracted left number from dialog box entry</instruction>
<instruction> Dim strRightNum ' Extracted right number from dialog box entry</instruction>
<instruction />
<instruction> ' If we're renaming a file then remove the extension from the end and save it for later.</instruction>
<instruction> if fIsFolder or 0 = InStr(strFileName,".") then</instruction>
<instruction> strExtension = ""</instruction>
<instruction> strNameOnly = strFileName</instruction>
<instruction> else</instruction>
<instruction> strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,".")-1))</instruction>
<instruction> strNameOnly = Left(strFileName, InStrRev(strFileName,".")-1)</instruction>
<instruction> end if</instruction>
<instruction />
<instruction> ' Find the leftmost comma in dialog box entry</instruction>
<instruction> intComPos = InStr(strNewName,",")</instruction>
<instruction />
<instruction> ' Extract the 2 numbers from dialog box entry</instruction>
<instruction> if intComPos > 0 then</instruction>
<instruction> strLeftNum = CInt(Trim(Left(strNewName, intComPos-1)))</instruction>
<instruction> strRightNum = CInt(Trim(Right(strNewName,Len(strNewName)-intComPos)))</instruction>
<instruction> end if</instruction>
<instruction />
<instruction> Dim I ' General counter variable</instruction>
<instruction> Dim LeftDots ' LeftDots will become the first RegEx tagged match</instruction>
<instruction> LeftDots = "" ' Set LeftDots to a null value to start</instruction>
<instruction />
<instruction> ' Assemble LEFT regEx dots ----------------------------------------</instruction>
<instruction> if strLeftNum > 0 then</instruction>
<instruction> I = 1</instruction>
<instruction> While I < strLeftNum</instruction>
<instruction> LeftDots = LeftDots & "."</instruction>
<instruction> I = I+1</instruction>
<instruction> Wend</instruction>
<instruction> LeftDots = "(^" & LeftDots & ")"</instruction>
<instruction> else</instruction>
<instruction> ' Character position was not more than 0 - set LeftDots to an invalid value so rename fails</instruction>
<instruction> LeftDots = "(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)"</instruction>
<instruction> end if</instruction>
<instruction> ' ----------------------------------------------------------------------------------------</instruction>
<instruction />
<instruction> Dim ExtractAmt ' ExtractAmt will be the number of characters to remove from the file name</instruction>
<instruction> ExtractAmt = strRightNum</instruction>
<instruction />
<instruction> ' Assemble EXTRACTION dots ----------------------------------------</instruction>
<instruction> Dim ExtractDots ' ExtractDots will become the second RegEx tagged match</instruction>
<instruction> ExtractDots = "" ' Set ExtractDots to a null value to start</instruction>
<instruction> I = 0</instruction>
<instruction> if ExtractAmt > 0 then</instruction>
<instruction> While I < ExtractAmt</instruction>
<instruction> ExtractDots = ExtractDots & "."</instruction>
<instruction> I = I+1</instruction>
<instruction> Wend</instruction>
<instruction> ExtractDots = "(" & ExtractDots & ")"</instruction>
<instruction> else</instruction>
<instruction> ' Amount to extract was not more than 0 - set ExtractDots to an invalid value so rename fails</instruction>
<instruction> ExtractDots = "(loY8TbEarTKFM5jKdd6pOoqAtm8ptx1zN2lx0ooB)"</instruction>
<instruction> end if</instruction>
<instruction> ' ----------------------------------------------------------------------------------------</instruction>
<instruction />
<instruction> Dim regex</instruction>
<instruction> ' Create a RegExp object</instruction>
<instruction> Set regex = new RegExp</instruction>
<instruction> regex.Pattern = LeftDots & ExtractDots & "(.*)"</instruction>
<instruction />
<instruction> strNameOnly = regex.Replace(strNameOnly, "$1$3") '</instruction>
<instruction> strNewName = strNameOnly & strExtension</instruction>
<instruction />
<instruction>End Function</instruction>
</function>
</button>
<button backcol="none" display="both" textcol="none">
<label>Remove Characters (count from right)</label>
<tip>Removes a set number of characters from the selected file names at a position counted from right side of the names</tip>
<icon1>#default:script</icon1>
<function type="normal">
<instruction>// This script removes a set number of characters from the selected file and folder names</instruction>
<instruction>// Last modified by John Zeman on May 9, 2010</instruction>
<instruction />
<instruction>Rename TO="{DlgStringS|Enter a number that indicates the starting point\n\nThen add a comma,\n\nFinally add another the number which will be how many characters will be removed\n\n(Example: 2,5 would remove characters 2 through 6)|2,5}"</instruction>
<instruction>@nodeselect</instruction>
<instruction>@script vbscript</instruction>
<instruction>option explicit</instruction>
<instruction />
<instruction>Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )</instruction>
<instruction />
<instruction> Dim strNameOnly</instruction>
<instruction> Dim strExtension</instruction>
<instruction> Dim intComPos ' Location of the leftmost comma in dialog box entry</instruction>
<instruction> Dim strLeftNum ' Extracted left number from dialog box entry</instruction>
<instruction> Dim strRightNum ' Extracted right number from dialog box entry</instruction>
<instruction />
<instruction> ' If we're renaming a file then remove the extension from the end and save it for later.</instruction>
<instruction> if fIsFolder or 0 = InStr(strFileName,".") then</instruction>
<instruction> strExtension = ""</instruction>
<instruction> strNameOnly = strFileName</instruction>
<instruction> else</instruction>
<instruction> strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,".")-1))</instruction>
<instruction> strNameOnly = Left(strFileName, InStrRev(strFileName,".")-1)</instruction>
<instruction> end if</instruction>
<instruction />
<instruction> ' Find the leftmost comma in dialog box entry</instruction>
<instruction> intComPos = InStr(strNewName,",")</instruction>
<instruction />
<instruction> ' Extract the 2 numbers from dialog box entry</instruction>
<instruction> if intComPos > 0 then</instruction>
<instruction> strLeftNum = CInt(Trim(Left(strNewName, intComPos-1)))</instruction>
<instruction> strRightNum = CInt(Trim(Right(strNewName,Len(strNewName)-intComPos)))</instruction>
<instruction> end if</instruction>
<instruction />
<instruction> Dim I ' General counter variable</instruction>
<instruction> Dim LeftDots ' LeftDots will become the first RegEx tagged match</instruction>
<instruction> LeftDots = "" ' Set LeftDots to a null value to start</instruction>
<instruction />
<instruction> ' Assemble LEFT regEx dots ----------------------------------------</instruction>
<instruction> if strLeftNum > 0 then</instruction>
<instruction> I = 1</instruction>
<instruction> While I < strLeftNum</instruction>
<instruction> LeftDots = "." & LeftDots</instruction>
<instruction> I = I+1</instruction>
<instruction> Wend</instruction>
<instruction> LeftDots = "(" & LeftDots & ")"</instruction>
<instruction> else</instruction>
<instruction> ' Character position was not more than 0 - set LeftDots to an invalid value so rename fails</instruction>
<instruction> LeftDots = "(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)"</instruction>
<instruction> end if</instruction>
<instruction> ' ----------------------------------------------------------------------------------------</instruction>
<instruction />
<instruction> Dim ExtractAmt ' ExtractAmt will be the number of characters to remove from the file name</instruction>
<instruction> ExtractAmt = strRightNum</instruction>
<instruction />
<instruction> ' Assemble EXTRACTION dots ----------------------------------------</instruction>
<instruction> Dim ExtractDots ' ExtractDots will become the second RegEx tagged match</instruction>
<instruction> ExtractDots = "" ' Set ExtractDots to a null value to start</instruction>
<instruction> I = 0</instruction>
<instruction> if ExtractAmt > 0 then</instruction>
<instruction> While I < ExtractAmt</instruction>
<instruction> ExtractDots = "." & ExtractDots</instruction>
<instruction> I = I+1</instruction>
<instruction> Wend</instruction>
<instruction> ExtractDots = "(" & ExtractDots & ")"</instruction>
<instruction> else</instruction>
<instruction> ' Amount to extract was not more than 0 - set ExtractDots to an invalid value so rename fails</instruction>
<instruction> ExtractDots = "(loY8TbEarTKFM5jKdd6pOoqAtm8ptx1zN2lx0ooB)"</instruction>
<instruction> end if</instruction>
<instruction> ' ----------------------------------------------------------------------------------------</instruction>
<instruction />
<instruction> Dim regex</instruction>
<instruction> ' Create a RegExp object</instruction>
<instruction> Set regex = new RegExp</instruction>
<instruction> regex.Pattern = "(.*)" & ExtractDots & LeftDots</instruction>
<instruction />
<instruction> strNameOnly = regex.Replace(strNameOnly, "$1$3") '</instruction>
<instruction> strNewName = strNameOnly & strExtension</instruction>
<instruction />
<instruction>End Function</instruction>
</function>
</button>
</button>