Enumerate Tracks / Kill leading numbers

Download and get help for different MediaMonkey for Windows 4 Addons.

Moderators: Peke, Gurus

GuidoR13
Posts: 14
Joined: Sun Mar 13, 2005 7:38 am

Enumerate Tracks / Kill leading numbers

Post by GuidoR13 »

Hallo (3),

Update 200901 THIS IS OUT OF DATE, please look for RegExp Find & Replace at http://www.mediamonkey.com/forum/viewto ... =2&t=28510 for a much more sophistated script.

I correct :roll: my small script with 2 Subs - thanx for the feedback!
|>Enumerate the selected tracks, optionally add the correct formated track# to the title
|>Kill leading numbers (optionally with spaces around), an undo I once needed
Hope you'll find it usefull.

Code: Select all

' Use at your own risk,  V 1.1
' V1.1 less than 10 tracks -> error in String-Func; add to track now correct formated
' Enumerates the selected track in the displayed order
' If wanted track# (incl. leading  zeros according to number of tracks)
' can be added to the title, an undo sub is included ;-), Scripts.ini;
' 	[EnumerateTracks]
' 	FileName=EnumerateTracks.vbs
' 	ProcName=EnumerateTracks
' 	Order=3
' 	DisplayName=Enumerate Tracks
' 	Description=Enumerate the selected tracks
' 	Language=VBScript
' 	ScriptType=0
' 	[KillLeadingNumbers]
' 	FileName=EnumerateTracks.vbs
' 	ProcName=KillLeadingNumbers
' 	Order=4
' 	DisplayName=Kill leading Numbers
' 	Description=Kills all leading numbers in selected tracks´ titles
' 	Language=VBScript
' 	ScriptType=0
Option Explicit 

Sub EnumerateTracks 
	'Define variables & Constants
	Dim i, CurrentDigits, CurrentDecade, StringOfZeros, MaxDigitsToAdd, AddToTitle, Progress
	Set Progress = SDB.Progress : Progress.Text = SDB.Localize("Enumerate...")
	' Get Tracklist
	With SDB.SelectedSongList
		If .count < 2 Then SDB.MessageBox "Select more than 1 title...", mtWarning, Array(mbOk) : Exit Sub
		AddToTitle = msgbox("Also add Track# to the title?", vbYesNoCancel + vbDefaultButton2,"Enumerate Track")
		If AddToTitle = vbCancel then Exit Sub
		Progress.MaxValue = .count
		' int(log(.count)/log(10)) <=> digits of last track - 1 (no log10 defined in vbs?)
		MaxDigitsToAdd = int(log(.count)/log(10))
		StringOfZeros = String(MaxDigitsToAdd + 1, "0") 'V11: string func does not accept 0 as length
		CurrentDigits = 0 : CurrentDecade = 10 
		For i=1 To .count
			' check digits of counter i, don't know how fast the ^-op is .. better safe than sorry
			If i = CurrentDecade then 
				CurrentDigits = CurrentDigits + 1
				CurrentDecade = 10 ^ (CurrentDigits + 1) 'V11: + 1 added
			End If 
			.item(i-1).TrackOrder = i
			If AddToTitle = vbYes Then
				.item(i-1).Title = left(StringOfZeros, MaxDigitsToAdd - CurrentDigits) & i _
				& " " & .item(i-1).Title
			End If
			.item(i-1).UpdateDB
			Progress.Value = i 'V11: No +1
			if Progress.Terminate then Exit For
    		Next
	End With
	Set Progress = Nothing
End Sub

Sub KillLeadingNumbers
	Dim LeavingSpace, RegularExpression, i, Progress
	Set Progress = SDB.Progress : Progress.Text = SDB.Localize("Enumerate...")
	Set RegularExpression = New RegExp : RegularExpression.pattern = "^\s*\d*" ' Leadings spaces also
	With SDB.SelectedSongList
		If .count = 0 Then SDB.MessageBox "Select something...", mtWarning, Array(mbOk) : Exit Sub
		LeavingSpace = msgbox("Space(s)after leading numbers required?", vbYesNoCancel + vbDefaultButton1,"Kill Leading Numbers")
		If LeavingSpace = vbCancel then Exit Sub
		If LeavingSpace = vbYes then RegularExpression.pattern = "^\s*\d*\s+"
		' this will kill leading spaces even if no number present
		Progress.MaxValue = .count
		For i=1 To .count
			.item(i-1).Title = RegularExpression.Replace( .item(i-1).Title, "")
			.item(i-1).UpdateDB 
			Progress.Value = i+1
			if Progress.Terminate then Exit For
		Next
	End With
	Set Progress = Nothing
End Sub
Last edited by GuidoR13 on Fri Jan 09, 2009 12:24 pm, edited 3 times in total.
GuidoR13
Posts: 14
Joined: Sun Mar 13, 2005 7:38 am

Delete Leading Number incl. an optional "."

Post by GuidoR13 »

ResumeMan wrote me a message, how to extend the kill leading numbers.

Now track name:
"1. Songname"
changes to:
"Songname"

Use at your own risk.
Sub KillLeadingNumbers
Dim LeavingSpace, RegularExpression, i, Progress
Set Progress = SDB.Progress : Progress.Text = SDB.Localize("Enumerate...")
Set RegularExpression = New RegExp : RegularExpression.pattern = "^\s*\d*" ' Leadings spaces also
With SDB.SelectedSongList
If .count = 0 Then SDB.MessageBox "Select something...", mtWarning, Array(mbOk) : Exit Sub
LeavingSpace = msgbox("Optional . and Space(s)after leading numbers required?", vbYesNoCancel + vbDefaultButton1,"Kill Leading Numbers")
If LeavingSpace = vbCancel then Exit Sub
If LeavingSpace = vbYes then RegularExpression.pattern = "^\s*\d*\.?\s+"
' this will kill leading spaces even if no number present
Progress.MaxValue = .count
For i=1 To .count
.item(i-1).Title = RegularExpression.Replace( .item(i-1).Title, "")
.item(i-1).UpdateDB
Progress.Value = i+1
if Progress.Terminate then Exit For
Next
End With
Set Progress = Nothing
End Sub
ResumeMan
Posts: 40
Joined: Wed Feb 08, 2006 1:11 pm

Post by ResumeMan »

That works great. Thanks!
Trail_Runnr

Re: Enumerate Tracks / Kill leading numbers

Post by Trail_Runnr »

Any chance of finding a script that could put a 0 in front of a file name or title?

I often need to add a 0 (zero) in order to get audio books in their proper order. This is typically for file/title names 1-9

It looks like the Kill leading numbers might be able to be modified to do just that. (?)

Thanks for any help!
Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Re: Enumerate Tracks / Kill leading numbers

Post by Bex »

Advanced Duplicate Find & Fix Find More From Same - Custom Search. | Transfer PlayStat & Copy-Paste Tags/AlbumArt between any tracks.
Tagging Inconsistencies Do you think you have your tags in order? Think again...
Play History & Stats Node Like having your Last-FM account stored locally, but more advanced.
Case & Leading Zero Fixer Works on filenames too!

All My Scripts
Post Reply