GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Remove characters from selected folder and file names

user-interface
button
vbscript
scripting

#1

This button removes a specific number of characters from the selected file and folder names at a point you specify.

When the button is clicked, it will present a dialog box where you can specify the position to start removing characters, and how many characters to remove, separated by a comma.

200dpi_wc10303_dopus

Example:

  • You have a file named “New Textfile.txt”.
  • Select it and click the button
  • Enter “4,9” into the dialog box
    The file will be renamed to “New.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)

  • Navigate to where the .dcf file is saved.
  • Select Settings -> Customize Toolbars.
  • Drag the .dcf file to your toolbar.
  • 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 removes a set number of characters from the selected file and folder names
// Last modified by John Zeman on May 9, 2010

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}"
@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 strLeftNum      	' Extracted left number from dialog box entry
	Dim strRightNum		' Extracted right number from dialog box entry

   	' Find the leftmost comma in dialog box entry
	intComPos = InStr(strNewName,",")

	' Extract the 2 numbers from dialog box entry
	if intComPos > 0 then
		strLeftNum = CInt(Trim(Left(strNewName, intComPos-1)))
		strRightNum = CInt(Trim(Right(strNewName,Len(strNewName)-intComPos)))
	end if

	Dim I			' General counter variable
	Dim LeftDots		' LeftDots will become the first RegEx tagged match
	LeftDots = ""		' Set LeftDots to a null value to start

	' Assemble LEFT regEx dots ----------------------------------------
	if strLeftNum > 0 then
		I = 1
		While I < strLeftNum
			LeftDots = LeftDots & "."
			I = I+1
		Wend
		LeftDots = "(^" & LeftDots & ")"
	else
		' Character position was not more than 0 - set LeftDots to an invalid value so rename fails
		LeftDots = "(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)"
	end if
	' ----------------------------------------------------------------------------------------

	Dim ExtractAmt      	' ExtractAmt will be the number of characters to remove from the file name
	ExtractAmt = strRightNum

	' Assemble EXTRACTION dots ----------------------------------------
	Dim ExtractDots		' ExtractDots will become the second RegEx tagged match
	ExtractDots = ""	' Set ExtractDots to a null value to start
	I = 0
	if ExtractAmt > 0 then
		While I < ExtractAmt
			ExtractDots = ExtractDots & "."
			I = I+1
		Wend
      		ExtractDots = "(" & ExtractDots & ")"
   	else
		' Amount to extract was not more than 0 - set ExtractDots to an invalid value so rename fails
		ExtractDots = "(loY8TbEarTKFM5jKdd6pOoqAtm8ptx1zN2lx0ooB)"
	end if
	' ----------------------------------------------------------------------------------------

	Dim regex
	' Create a RegExp object
	Set regex = new RegExp

	if fIsFolder = True then	' If it's a folder ---------------------------------------
		regex.Pattern = LeftDots & ExtractDots & "(.*)"
	else				' If it's a file -----------------------------------------
		regex.Pattern = LeftDots & ExtractDots & "(.*\..*)"
	end if

	strNewName = regex.Replace(strFileName, "$1$3") '
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 deleting part of the filename 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 position and number of characters to remove, 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 allows you to insert text into a specified position. You'll find that button here:


Rename and overwrite button
Insert text into file names
Regex rename - insert space between uppercase/lowercase
Advanced Rename Button (Capitalise, number, formats...)
Insert text into file names
#2

@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=&quot;{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}&quot;</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 &apos; Location of the leftmost comma in dialog box entry</instruction> <instruction> Dim strLeftNum &apos; Extracted left number from dialog box entry</instruction> <instruction> Dim strRightNum &apos; Extracted right number 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 in dialog box entry</instruction> <instruction> intComPos = InStr(strNewName,&quot;,&quot;)</instruction> <instruction /> <instruction> &apos; Extract the 2 numbers from dialog box entry</instruction> <instruction> if intComPos &gt; 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 &apos; General counter variable</instruction> <instruction> Dim LeftDots &apos; LeftDots will become the first RegEx tagged match</instruction> <instruction> LeftDots = &quot;&quot; &apos; Set LeftDots to a null value to start</instruction> <instruction /> <instruction> &apos; Assemble LEFT regEx dots ----------------------------------------</instruction> <instruction> if strLeftNum &gt; 0 then</instruction> <instruction> I = 1</instruction> <instruction> While I &lt; strLeftNum</instruction> <instruction> LeftDots = LeftDots &amp; &quot;.&quot;</instruction> <instruction> I = I+1</instruction> <instruction> Wend</instruction> <instruction> LeftDots = &quot;(^&quot; &amp; LeftDots &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> LeftDots = &quot;(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)&quot;</instruction> <instruction> end if</instruction> <instruction> &apos; ----------------------------------------------------------------------------------------</instruction> <instruction /> <instruction> Dim ExtractAmt &apos; ExtractAmt will be the number of characters to remove from the file name</instruction> <instruction> ExtractAmt = strRightNum</instruction> <instruction /> <instruction> &apos; Assemble EXTRACTION dots ----------------------------------------</instruction> <instruction> Dim ExtractDots &apos; ExtractDots will become the second RegEx tagged match</instruction> <instruction> ExtractDots = &quot;&quot; &apos; Set ExtractDots to a null value to start</instruction> <instruction> I = 0</instruction> <instruction> if ExtractAmt &gt; 0 then</instruction> <instruction> While I &lt; ExtractAmt</instruction> <instruction> ExtractDots = ExtractDots &amp; &quot;.&quot;</instruction> <instruction> I = I+1</instruction> <instruction> Wend</instruction> <instruction> ExtractDots = &quot;(&quot; &amp; ExtractDots &amp; &quot;)&quot;</instruction> <instruction> else</instruction> <instruction> &apos; Amount to extract was not more than 0 - set ExtractDots to an invalid value so rename fails</instruction> <instruction> ExtractDots = &quot;(loY8TbEarTKFM5jKdd6pOoqAtm8ptx1zN2lx0ooB)&quot;</instruction> <instruction> end if</instruction> <instruction> &apos; ----------------------------------------------------------------------------------------</instruction> <instruction /> <instruction> Dim regex</instruction> <instruction> &apos; Create a RegExp object</instruction> <instruction> Set regex = new RegExp</instruction> <instruction> regex.Pattern = LeftDots &amp; ExtractDots &amp; &quot;(.*)&quot;</instruction> <instruction /> <instruction> strNameOnly = regex.Replace(strNameOnly, &quot;$1$3&quot;) &apos;</instruction> <instruction> strNewName = strNameOnly &amp; 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=&quot;{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}&quot;</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 &apos; Location of the leftmost comma in dialog box entry</instruction> <instruction> Dim strLeftNum &apos; Extracted left number from dialog box entry</instruction> <instruction> Dim strRightNum &apos; Extracted right number 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 in dialog box entry</instruction> <instruction> intComPos = InStr(strNewName,&quot;,&quot;)</instruction> <instruction /> <instruction> &apos; Extract the 2 numbers from dialog box entry</instruction> <instruction> if intComPos &gt; 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 &apos; General counter variable</instruction> <instruction> Dim LeftDots &apos; LeftDots will become the first RegEx tagged match</instruction> <instruction> LeftDots = &quot;&quot; &apos; Set LeftDots to a null value to start</instruction> <instruction /> <instruction> &apos; Assemble LEFT regEx dots ----------------------------------------</instruction> <instruction> if strLeftNum &gt; 0 then</instruction> <instruction> I = 1</instruction> <instruction> While I &lt; strLeftNum</instruction> <instruction> LeftDots = &quot;.&quot; &amp; LeftDots</instruction> <instruction> I = I+1</instruction> <instruction> Wend</instruction> <instruction> LeftDots = &quot;(&quot; &amp; LeftDots &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> LeftDots = &quot;(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)&quot;</instruction> <instruction> end if</instruction> <instruction> &apos; ----------------------------------------------------------------------------------------</instruction> <instruction /> <instruction> Dim ExtractAmt &apos; ExtractAmt will be the number of characters to remove from the file name</instruction> <instruction> ExtractAmt = strRightNum</instruction> <instruction /> <instruction> &apos; Assemble EXTRACTION dots ----------------------------------------</instruction> <instruction> Dim ExtractDots &apos; ExtractDots will become the second RegEx tagged match</instruction> <instruction> ExtractDots = &quot;&quot; &apos; Set ExtractDots to a null value to start</instruction> <instruction> I = 0</instruction> <instruction> if ExtractAmt &gt; 0 then</instruction> <instruction> While I &lt; ExtractAmt</instruction> <instruction> ExtractDots = &quot;.&quot; &amp; ExtractDots</instruction> <instruction> I = I+1</instruction> <instruction> Wend</instruction> <instruction> ExtractDots = &quot;(&quot; &amp; ExtractDots &amp; &quot;)&quot;</instruction> <instruction> else</instruction> <instruction> &apos; Amount to extract was not more than 0 - set ExtractDots to an invalid value so rename fails</instruction> <instruction> ExtractDots = &quot;(loY8TbEarTKFM5jKdd6pOoqAtm8ptx1zN2lx0ooB)&quot;</instruction> <instruction> end if</instruction> <instruction> &apos; ----------------------------------------------------------------------------------------</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; ExtractDots &amp; LeftDots</instruction> <instruction /> <instruction> strNameOnly = regex.Replace(strNameOnly, &quot;$1$3&quot;) &apos;</instruction> <instruction> strNewName = strNameOnly &amp; strExtension</instruction> <instruction /> <instruction>End Function</instruction> </function> </button> </button>


#3

Thanks ! Exactly what I was looking for.

I talk about it here in Tips section


#4

I use this script a lot. The one thing that doesn't work is removing characters from the end no matter how long the file name is from the starting point.
For example I have these 3 files and want to strip everything from pposition 10 so I get Filename1, Filename2 and Filename3:
Filename1a.txt
Filename2copy.txt
Filename3somerandomjunk.txt

For the first name I have to enter 10,1 (counting/deleting from the start)
For the second name I have to enter 10,4 and for the last one 10,14.

I rather just like to enter 10,999 so that it deletes everything after the 10th character. But that does not work with this script.

Has anyone adjusted this script in the past 6 years or knows another way to do this?

Preferably I want to have a dedicated button for it and not a rename preset. (Or there must be a way that rename presets can be converted to buttons).


#5

You don't need a script for this these days. Opus 12 lets you do it by editing the filenames in the preview list, applying the changes you make to all of the filenames at once when in macro mode:

New rename features in Opus 12.


#6

Editing the file names in de preview list does not work properly. At least not when it concerns deleting all characters from a specific position.

When I place the cursor at the position I want to delete from, a line appears. Pressing DEL key deletes the characters to the right of the line for all files. So far so good. But it stops deleting characters at the position where the cursor is blinking. That cursor position is a random file, not necessarily the longest file name.
To make this work properly I have to place the cursor at the desired start position in de longest file name. That's quite an awkward way.

Beyond that, I want to use a dedicated button which instead of fumbling in the rename window. Inserting end deleting text with the buttons from John Zeman works way faster than using the rename window.


#7

If you want to delete from/near the end, with the files right aligned, push the End key twice.


#8

Pressing the End key twice just right aligns the file names. This is not a solution to the problem. Far from it because now the starting position is different for every file.
A simple deletion of characters from a specific starting position is apparently not an easy thing to do, if at all possible.
I'll wait until someone has a scripted solution. Meanwhile I will select the longest file name in the rename window. That's the least worst way to do it right now.


#9

I see what you mean now.

If you look at what the "macro operation" field in the rename dialog displays when you do the edit the first way (left aligned), you can see it generates a string like L13-4 which means delete, left-aligned, from position 13, 4 characters.

If you change the 4 to 999 or something, it will delete everything to the end of the filenames.

You can type those codes in directly without having to generate them via editing the preview list, too. The syntax is here: Rename Macro Language.


#10

Thanks exactly what I was looking for. Works great.