GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Insert text into file names

user-interface
button
vbscript
scripting

#1

This button inserts the text you enter at a specific place of the selected file names.

When the button is clicked, it will present a dialog box where you can insert the position (number) in the filename after which you want to insert new characters, and the new text, separated by a comma.

Example:

  • You have a file named "New Textfile.txt".
  • Select it and click the button
  • Enter "3, Example" into the dialog box
  • The file will be renamed to "New Example Textfile.txt".

Download the Button:

This is a toolbar button, not a Rename preset, since it works well from a toolbar or hotkey but would not work well in the Rename dialog. (It shows its own dialog when you click the button.)

To add a button from a .dcf file to your toolbar:
(From: How to use buttons and scripts from this forum)

  1. Navigate to where the .dcf file is. (If it is inside a zip, extract it first.)
  2. Select Settings -> Customize Toolbars.
  3. Drag the .dcf file to your toolbar.
  4. Click OK in the Customize window.

Button Code:

This is just for reference, and people browsing the forum for techniques. If you just want to use the button, see the download section, above.

// This script inserts text into the selected file names at the character position you specify
// Last modified by John Zeman on May 8, 2010

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}"
@nodeselect 

@script vbscript
option explicit

Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )

   Dim intComPos   ' Location of the leftmost comma in dialog box entry
   Dim strText   ' Extracted Text from dialog box entry
   Dim strCount   ' Extracted number (of places from left) from dialog box entry

   ' Find the leftmost comma
   intComPos = InStr(strNewName,",")
   if intComPos > 0 then
      strCount = Trim(Left(strNewName, intComPos-1))
      strText = Right(strNewName,Len(strNewName)-intComPos)
   end if

   Dim Dots
   Dots = ""   

   Dim I
       I = 0
       While I < CInt(strCount)
      Dots = Dots & "."
      I = I+1
       Wend
   Dots = "(" & Dots & ")"
      
   Dim regex
     ' Create a RegExp object
     Set regex = new RegExp
   regex.Pattern = Dots & "(.*)"
   strNewName = regex.Replace(strFileName, "$1" & strText & "$2") '
End Function 

Alternatives:

  • If you are using Opus 12, you can do this kind of operation really easily by turning on Use preview list to build macros in the Rename dialog, and then simply clicking one of the names in the preview list, below the checkbox, and typing your text in there. Opus will automatically make the same changes to all of the other files at once.

  • Also in Opus 12, you could have a rename preset which adds fields to the Rename dialog where you can input the number of characters to change and the text to insert, if you wanted to have something similar to the button in this thread but which works within the Rename dialog. However, the macro mode is probably still best, unless you're using an older version.

Related:

Another Button from John which allows you to delete a number of characters at a specified position from the Name. You'll find that Button here:


REQUEST: Add (user input) to the beginning of file name
Remove characters from selected folder and file names
Advanced Rename Button (Capitalise, number, formats...)
REQUEST: Add (user input) to the beginning of file name
#4

@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>


Insert Predefined Text Into Filename?
#5

Thanks ! Exactly what I was looking for.

I talk about it here in Tips section


#6

Thanks works exactly how I wanted it to.