Insert text into file names

@JohnZeman:

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=&quot;{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}&quot;</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 &apos; Location of the leftmost comma in dialog box entry</instruction> <instruction> Dim strText &apos; Extracted Text from dialog box entry</instruction> <instruction> Dim strCount &apos; Extracted number (of places from left) from dialog box entry</instruction> <instruction /> <instruction> &apos; If we&apos;re renaming a file then remove the extension from the end and save it for later.</instruction> <instruction> if fIsFolder or 0 = InStr(strFileName,&quot;.&quot;) then</instruction> <instruction> strExtension = &quot;&quot;</instruction> <instruction> strNameOnly = strFileName</instruction> <instruction> else</instruction> <instruction> strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,&quot;.&quot;)-1))</instruction> <instruction> strNameOnly = Left(strFileName, InStrRev(strFileName,&quot;.&quot;)-1)</instruction> <instruction> end if</instruction> <instruction /> <instruction> &apos; Find the leftmost comma</instruction> <instruction> intComPos = InStr(strNewName,&quot;,&quot;)</instruction> <instruction> if intComPos &gt; 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 = &quot;&quot; </instruction> <instruction /> <instruction> if CInt(strCount) &gt; 0 then</instruction> <instruction> I = 1</instruction> <instruction> While I &lt; CInt(strCount)</instruction> <instruction> Dots = Dots &amp; &quot;.&quot;</instruction> <instruction> I = I+1</instruction> <instruction> Wend</instruction> <instruction> Dots = &quot;(^&quot; &amp; Dots &amp; &quot;)&quot;</instruction> <instruction> else</instruction> <instruction> &apos; Character position was not more than 0 - set LeftDots to an invalid value so rename fails</instruction> <instruction> Dots = &quot;(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)&quot;</instruction> <instruction> end if</instruction> <instruction> </instruction> <instruction> Dim regex</instruction> <instruction> &apos; Create a RegExp object</instruction> <instruction> Set regex = new RegExp</instruction> <instruction> regex.Pattern = Dots &amp; &quot;(.*)&quot;</instruction> <instruction /> <instruction> strNameOnly = regex.Replace(strNameOnly, &quot;$1&quot; &amp; strText &amp; &quot;$2&quot;)</instruction> <instruction> strNewName = strNameOnly &amp; 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=&quot;{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}&quot;</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 &apos; Location of the leftmost comma in dialog box entry</instruction> <instruction> Dim strText &apos; Extracted Text from dialog box entry</instruction> <instruction> Dim strCount &apos; Extracted number (of places from left) from dialog box entry</instruction> <instruction /> <instruction> &apos; If we&apos;re renaming a file then remove the extension from the end and save it for later.</instruction> <instruction> if fIsFolder or 0 = InStr(strFileName,&quot;.&quot;) then</instruction> <instruction> strExtension = &quot;&quot;</instruction> <instruction> strNameOnly = strFileName</instruction> <instruction> else</instruction> <instruction> strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,&quot;.&quot;)-1))</instruction> <instruction> strNameOnly = Left(strFileName, InStrRev(strFileName,&quot;.&quot;)-1)</instruction> <instruction> end if</instruction> <instruction /> <instruction> &apos; Find the leftmost comma</instruction> <instruction> intComPos = InStr(strNewName,&quot;,&quot;)</instruction> <instruction> if intComPos &gt; 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 = &quot;&quot; </instruction> <instruction> </instruction> <instruction> if CInt(strCount) &gt; 0 then</instruction> <instruction> I = 1</instruction> <instruction> While I &lt; CInt(strCount)</instruction> <instruction> Dots = &quot;.&quot; &amp; Dots</instruction> <instruction> I = I+1</instruction> <instruction> Wend</instruction> <instruction> Dots = &quot;(&quot; &amp; Dots &amp; &quot;)&quot;</instruction> <instruction> else</instruction> <instruction> &apos; Character position was not more than 0 - set LeftDots to an invalid value so rename fails</instruction> <instruction> Dots = &quot;(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)&quot;</instruction> <instruction> end if</instruction> <instruction> </instruction> <instruction> Dim regex</instruction> <instruction> &apos; Create a RegExp object</instruction> <instruction> Set regex = new RegExp</instruction> <instruction> regex.Pattern = &quot;(.*)&quot; &amp; Dots</instruction> <instruction /> <instruction> strNameOnly = regex.Replace(strNameOnly, &quot;$1&quot; &amp; strText &amp; &quot;$2&quot;)</instruction> <instruction> strNewName = strNameOnly &amp; strExtension</instruction> <instruction> </instruction> <instruction>End Function </instruction> </function> </button> </button>