Page 1 of 1

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

Posted: 29 Jan 2017, 06:39
by opw62
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 537 times

=

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

Posted: 29 Jan 2017, 06:54
by abr
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

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

Posted: 29 Jan 2017, 07:07
by abr
Oh, & yes, it's a good idea to have a function like this built into Dopus. Overread that aspect, sorry.

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

Posted: 29 Jan 2017, 07:44
by opw62
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.

=