Rename: How to remove xx characters from filenames in an easy way?

Main area for tech support, questions, suggestions and bug reports.
Forum rules
Please try the FAQs, forum search, and manual before posting.
Please link your account to your registration.
opw62
Registered Opus Pro User
Posts: 981
Joined: 30 Jul 2005, 01:34
OS: Windows 10 64-bit
Opus: Latest Beta
Within the existing rename panel and having files with different filename lengths.

How to remove xx characters from the name counting from the last position?
e.g. remove 7 trailing characters, different, so find and replace does not work.

I know and meanwhile did : using regex.

However maybe something could be added in the rename GUI some time in future?
Something like the below, removing / adding characters at any place and add text. Just a suggestion.

Thanks.

=
SnagIt-29012017 153320.png
SnagIt-29012017 153320.png (3.45 KiB) Viewed 531 times

=

abr
Registered Opus Pro User
Posts: 3653
Joined: 12 Apr 2007, 17:20
OS: Windows 10 64-bit
Opus: Latest Beta
Location: Hamburg, Germany
Some colleague from this forum once provided me these two codes, which might be helpful:


Code: Select all

Rename TO="{DlgStringS|AN welcher Stelle soll von hinten etwas eingefügt werden ?\n\ndurch ein Komma getrennt\n\nWAS soll eingefügt werden ?|2,Neuer Text}" AUTORENAME
@nodeselect

@script vbscript
option explicit

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

   Dim strNameOnly
   Dim strExtension
   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

   ' If we're renaming a file then remove the extension from the end and save it for later.
   if fIsFolder or 0 = InStr(strFileName,".") then
      strExtension = ""
      strNameOnly = strFileName
   else
      strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,".")-1))
      strNameOnly = Left(strFileName, InStrRev(strFileName,".")-1)
   end if


   ' 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 I
   Dim Dots
   Dots = ""   
   
   if CInt(strCount) > 0 then
       I = 1
       While I < CInt(strCount)
      Dots = "." & Dots
      I = I+1
       Wend
      Dots = "(" & Dots & ")"
   else
      ' Character position was not more than 0 - set LeftDots to an invalid value so rename fails
      Dots = "(8SCaO4fOzy5hy4Fs2CxBGCPy6oAl2Hp988ZH9C3toox0HWlmOA)"
   end if
     
   Dim regex
     ' Create a RegExp object
     Set regex = new RegExp
   regex.Pattern = "(.*)" & Dots
   strNameOnly = regex.Replace(strNameOnly, "$1" & strText & "$2") '
   strNewName = strNameOnly & strExtension
   
End Function


or

Code: Select all

Rename TO="{DlgStringS|An welcher Stelle soll von hinten etwas entfernt werden ?\n\ndurch ein Komma getrennt\n\nWIEVIELE Stellen sollen entfernt werden ?|2,5}" AUTORENAME
@nodeselect
@script vbscript
option explicit

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

   Dim strNameOnly
   Dim strExtension
   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

   ' If we're renaming a file then remove the extension from the end and save it for later.
   if fIsFolder or 0 = InStr(strFileName,".") then
      strExtension = ""
      strNameOnly = strFileName
   else
      strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,".")-1))
      strNameOnly = Left(strFileName, InStrRev(strFileName,".")-1)
   end if

      ' 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 = "(.*)" & ExtractDots & LeftDots
   'else            ' If it's a file -----------------------------------------
      'regex.Pattern = LeftDots & ExtractDots & "(.*\..*)"
   'end if

   strNameOnly = regex.Replace(strNameOnly, "$1$3") '
   strNewName = strNameOnly & strExtension
   
End Function

abr
Registered Opus Pro User
Posts: 3653
Joined: 12 Apr 2007, 17:20
OS: Windows 10 64-bit
Opus: Latest Beta
Location: Hamburg, Germany
Oh, & yes, it's a good idea to have a function like this built into Dopus. Overread that aspect, sorry.

opw62
Registered Opus Pro User
Posts: 981
Joined: 30 Jul 2005, 01:34
OS: Windows 10 64-bit
Opus: Latest Beta
Thank you very much for the code. I will give it a try later.
In my case I could solve it with a regex. The suggestion I came up with offers some extra flexibility.
I guess there is some space in the rename panel to add it... :-)

Anyway, thanks again.

=


Return to “Help & Support”