Arithmetic on number in file name?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
spikey
Member
Member
Posts: 123
Joined: 2005-07-20, 08:37 UTC

Arithmetic on number in file name?

Post by *spikey »

I have a few hundred jpegs whose file name ends with a number.

Is it possible for TC to increase the number portion of each file name by 1 (or some other value)?

Simply renumbering all the file would lose information because the numbers are not sequential.
User avatar
Stefan2
Power Member
Power Member
Posts: 4133
Joined: 2007-09-13, 22:20 UTC
Location: Europa

VBS: _ForEachSelFileDo - Rename Math Increase Number Digit.vbs

Post by *Stefan2 »

spikey wrote: 2019-07-06, 18:22 UTC
Is it possible for TC to increase the number portion of each file name by 1 (or some other value)?
Yes, by utilizing a system script. TC will support you by parameters for selected files.


Example:
'//---------------------------
'//Old : Test 001.txt
'//New: Test 002.txt
'//---------------------------
'//OK
'//---------------------------
'//---------------------------
'//Old : Test 2345.txt
'//New: Test 2346.txt
'//---------------------------
'//OK
'//---------------------------


All file names must follow the same pattern (like here: xxxx ddd.ext)
You have to modify the code and adjust the regex pattern at the USER SETTINGS to match the digits and also adust the wanted "TheMath" rule.

'// U S E R S E T T I N G S
' File name: "xxxx ddd.ext"
RegExPattern = "(\w+\s*)(\d+)"
TheMath = +1


Code: Select all

    
    
' Total Commander VBScript "ForEachSelectedFileDo -smtg.vbs" by Stefan
'   Found at: https://ghisler.ch/board/viewtopic.php?p=355257#p355257  2014-07-24, v0.01
'   Found at: https://ghisler.ch/board/viewtopic.php?p=357183#p357183  06.07.2019, v0.01 // 07.07.2019, v0.02
' Usage:
'    1. Create this script ForEachSelFileDo.vbs as plain text file.
'    2. Create the below button.
'    3. Select some files and click that new button.
'
'       TotalCommander Button code:
'       CMD:     "D:\rive\path to my\ForEachSelFileDo.vbs"  (or portable: "%COMMANDER_PATH%\TOOLs\VBS\ForEachSelFileDo.vbs")
'       PARAM:   "%F"
'       START:
'       ICON:    C:\Windows\System32\WScript.exe,2
'       TOOLTIP: ForEachSelFileDo VBS
'
'For parameters see > Help > Operation > Menus > Menu: Configuration > Change Button Bar > Configuration - Change button bar.
'    %L / %F : Create a list file in the TEMP directory with the names of the selected files and directories.

'// U S E R    S E T T I N G S
' File name pattern example: "xxxx ddd.ext"
'                             Test001.txt
'                             Test 2345.txt
'                             Test 23454719.txt
RegExPattern = "(\w+\s*)(\d+)"
TheMath = +1

'// T H E    C O D E
If Wscript.arguments.count < 1 Then MsgBox "Error: TC-Param missing..." : WScript.Quit
strTCtempList = Wscript.arguments.Item(0) ' The TC temp file due to the "%F" parameter
Set FSO = CreateObject("Scripting.FileSystemObject")
If  FSO.FileExists(strTCtempList) Then
        Set oTextStream = FSO.OpenTextFile(strTCtempList,1)
        Do Until oTextStream.AtEndOfStream
            strLine    = oTextStream.ReadLine
            If(Right(strLine,1) <> "\") Then
                sBase = FSO.GetBaseName(strLine)
                sExte = FSO.GetExtensionName(strLine)
                sLeft  = RegExReplace(sBase,RegExPattern,"$1",false,true,false)
                sRight = RegExReplace(sBase,RegExPattern,"$2",false,true,false)
               
                If(sRight<>"ERROR")Then 
                    sRight = Right("000000000"&sRight+TheMath,Len(sRight))
                    strNewFilename = sLeft & sRight &"."& sExte
                    If not (FSO.FileExists(strNewFilename)) Then
                        MsgBox "Old : " & strLine &vbLF& "New: " & strNewFilename
                        '//---------------------------
                        '//Old : Test 001.txt
                        '//New: Test 002.txt
                        '//---------------------------
                        '//OK   
                        '//---------------------------

                        '//FSO.MoveFile strLine, strNewFilename
                    End If
                End If
            End If
        Loop
        oTextStream.Close
Else
        MsgBox "Input file  strTCtempList  not found. Check button parameter and quoting."_
                &vbLF&vbLF & strTCtempList,,"TC-Script Error - ForEachSelFileDo"
End If

Function RegExReplace( strSource , strRegExprPattern, strReplace, bRelaceAllOccurrences, bIgnoreCase, bMultiLine )
        Set objRegEx = CreateObject("vbscript.regexp")
        objRegEx.IgnoreCase = bIgnoreCase
        objRegEx.Global = bRelaceAllOccurrences
        objRegEx.MultiLine = bMultiLine
        objRegEx.Pattern = strRegExprPattern
        If objRegEx.Test(strSource) Then
            RegExReplace = objRegEx.Replace(strSource, strReplace)
        Else
            'return original string
            RegExReplace = "ERROR" ''strSource
        End If
        Set objRegEx = Nothing
End Function





HTH? :D
User avatar
spikey
Member
Member
Posts: 123
Joined: 2005-07-20, 08:37 UTC

Re: Arithmetic on number in file name?

Post by *spikey »

Thanks for your help, Stefan, although using scripts is a bit beyond my capabilities.
Post Reply