Hi John,
I modified your Button a little bit for my needs and the suggestion of a member of the german forum (Joerg765):
-
To avoid to insert characters into 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. -
The text is now inserted AT the choosen position instead of AFTER the choosen position to have the same behaviour as the other Button you provided here:
[url]Remove characters from selected folder and file names]
So "0" is now an illegal input and does nothing wheras "1" inserts at the beginning of the filename. -
I added a second version of the Button that inserts text AT the choosen position counted from the end of the filename.
This is useful if you want to rename files with long names or insert text right before a suffix.
I put both versions in a three-way-button in which a leftclick inserts text at a position counted from the beginning of the name and a rightclick inserts text counting from the end of the name backwards.
Here's the Code of the three-way-button:
Insert Text 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>Insert Text</label>
<icon1>#default:script</icon1>
<button backcol="none" display="both" textcol="none">
<label>Insert Text (count from left)</label>
<tip>Insert text a set number of characters in from left side of selected file names</tip>
<icon1>#default:script</icon1>
<function type="normal">
<instruction>// This script inserts text into the selected file names at the character position you specify</instruction>
<instruction>// Last modified by John Zeman on May 8, 2010</instruction>
<instruction />
<instruction>Rename TO="{DlgStringS|Enter the number of filename characters from left\n(the text insertion point)\n\n a comma\n\nand then the text to insert|2,Text to Insert}"</instruction>
<instruction>@nodeselect </instruction>
<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 strText ' Extracted Text from dialog box entry</instruction>
<instruction> Dim strCount ' Extracted number (of places from left) 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</instruction>
<instruction> intComPos = InStr(strNewName,",")</instruction>
<instruction> if intComPos > 0 then</instruction>
<instruction> strCount = Trim(Left(strNewName, intComPos-1))</instruction>
<instruction> strText = Right(strNewName,Len(strNewName)-intComPos)</instruction>
<instruction> end if</instruction>
<instruction />
<instruction> Dim I</instruction>
<instruction> Dim Dots</instruction>
<instruction> Dots = "" </instruction>
<instruction />
<instruction> if CInt(strCount) > 0 then</instruction>
<instruction> I = 1</instruction>
<instruction> While I < CInt(strCount)</instruction>
<instruction> Dots = Dots & "."</instruction>
<instruction> I = I+1</instruction>
<instruction> Wend</instruction>
<instruction> Dots = "(^" & Dots & ")"</instruction>
<instruction> else</instruction>
<instruction> ' Character position was not more than 0 - set LeftDots to an invalid value so rename fails</instruction>
<instruction> Dots = "(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)"</instruction>
<instruction> end if</instruction>
<instruction> </instruction>
<instruction> Dim regex</instruction>
<instruction> ' Create a RegExp object</instruction>
<instruction> Set regex = new RegExp</instruction>
<instruction> regex.Pattern = Dots & "(.*)"</instruction>
<instruction />
<instruction> strNameOnly = regex.Replace(strNameOnly, "$1" & strText & "$2")</instruction>
<instruction> strNewName = strNameOnly & strExtension</instruction>
<instruction> </instruction>
<instruction>End Function </instruction>
</function>
</button>
<button backcol="none" display="both" textcol="none">
<label>Insert Text (count from right)</label>
<tip>Insert text a set number of characters in from right side of selected file names</tip>
<icon1>#default:script</icon1>
<function type="normal">
<instruction>// This script inserts text into the selected file names at the character position you specify</instruction>
<instruction>// Last modified by John Zeman on May 8, 2010</instruction>
<instruction />
<instruction>Rename TO="{DlgStringS|Enter the number of filename characters from left\n(the text insertion point)\n\n a comma\n\nand then the text to insert|2,Text to Insert}"</instruction>
<instruction>@nodeselect </instruction>
<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 strText ' Extracted Text from dialog box entry</instruction>
<instruction> Dim strCount ' Extracted number (of places from left) 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</instruction>
<instruction> intComPos = InStr(strNewName,",")</instruction>
<instruction> if intComPos > 0 then</instruction>
<instruction> strCount = Trim(Left(strNewName, intComPos-1))</instruction>
<instruction> strText = Right(strNewName,Len(strNewName)-intComPos)</instruction>
<instruction> end if</instruction>
<instruction />
<instruction> Dim I</instruction>
<instruction> Dim Dots</instruction>
<instruction> Dots = "" </instruction>
<instruction> </instruction>
<instruction> if CInt(strCount) > 0 then</instruction>
<instruction> I = 1</instruction>
<instruction> While I < CInt(strCount)</instruction>
<instruction> Dots = "." & Dots</instruction>
<instruction> I = I+1</instruction>
<instruction> Wend</instruction>
<instruction> Dots = "(" & Dots & ")"</instruction>
<instruction> else</instruction>
<instruction> ' Character position was not more than 0 - set LeftDots to an invalid value so rename fails</instruction>
<instruction> Dots = "(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)"</instruction>
<instruction> end if</instruction>
<instruction> </instruction>
<instruction> Dim regex</instruction>
<instruction> ' Create a RegExp object</instruction>
<instruction> Set regex = new RegExp</instruction>
<instruction> regex.Pattern = "(.*)" & Dots</instruction>
<instruction />
<instruction> strNameOnly = regex.Replace(strNameOnly, "$1" & strText & "$2")</instruction>
<instruction> strNewName = strNameOnly & strExtension</instruction>
<instruction> </instruction>
<instruction>End Function </instruction>
</function>
</button>
</button>