Underscore Addition!

Any ideas about how to improve MediaMonkey? Let us know!

Moderator: Gurus

JonSenior
Posts: 9
Joined: Thu May 25, 2006 5:55 am

Underscore Addition!

Post by JonSenior » Fri May 26, 2006 6:40 am

As someone who's primary OS is Linux (MM for linux? Just adding my voice to the clamour!) I dislike spaces in filenames. All my previous methods for ripping CDs included some method for replacing spaces with underscore characters. I know that MM can remove underscores, but is there an existing method to replace them? It's probably the work of a script, but I loathe and detest Visual Basic (I've written software using it, and over four years later I still feel dirty!). Before I try and remember how to do it, I'd rather check that a) It doesn't already exist. b) No-one has written a script that already does it.

Please keep me from getting my hands dirty!

Jon

Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Post by Bex » Fri May 26, 2006 7:20 am

I'm not aware of any script that replace spaces with underscores in the filename. What you could do is to Auto-organise your files and include underscores in the mask.

/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

Steegy
Posts: 3452
Joined: Sat Nov 05, 2005 7:17 pm
Location: Belgium
Contact:

Post by Steegy » Fri May 26, 2006 7:29 am

The vbscript would need about max. 5 lines of code :-? :roll: :o
Extensions: ExternalTools, ExtractFields, SongPreviewer, LinkedTracks, CleanImport, and some other scripts (Need Help with Addons > List of All Scripts).

JonSenior
Posts: 9
Joined: Thu May 25, 2006 5:55 am

Post by JonSenior » Fri May 26, 2006 7:34 am

Bex: I may be being a little slow, but I can't see any way of using Auto-organise to replace spaces with underscores. Short of changing the tags before organising and changing them back afterwards!

Steegy: One quick and dirty hack on it's way then. <Shudders>

Jon[/b]

Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Post by Bex » Fri May 26, 2006 8:17 am

What I meant was to rename the files using Auto-organise.
But I see now what you mean. The underscores would only appear between the "tags" in the filename which is not what you asked for.
e.g.
01 - Rolling Stones - Sympathy For The Devil.mp3
Would become (with this mask <Track#>_<Artist>_<Title>):
01_Rolling Stones_Sympathy For The Devil.mp3

I'll guess you'll have to write a script then...

PS If you make a script it would be nice to make it work both ways and optionally include the full path. Even nicer if it also renamed accompanying files.

/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

JonSenior
Posts: 9
Joined: Thu May 25, 2006 5:55 am

Post by JonSenior » Fri May 26, 2006 8:36 am

OK. One quick 'n' dirty hack later:

Code: Select all

' A simple script that adds underscores to path names.
Sub AddUnderscores
  ' Define variables
  Dim list, itm, i, tmp, res, qty, message

  ' Get list of selected tracks from MediaMonkey
  Set list = SDB.CurrentSongList 

  ' Process all selected tracks
  res = SDB.MessageBox( "About to (possibly) modify " + CStr(list.count) + " files. Continue?", mtConfirmation, Array(mbOk, mbCancel))

  if res = mrOK then
    For i=0 To list.count-1
      Set itm = list.Item(i)

      tmp = Replace(itm.Path, " ", "_")
      itm.Path = tmp
      itm.UpdateDB
    Next
  End If
End Sub
'sgot a message box an' everything! ;-) Currently it doesn't remove empty directories (In fact... it's blissfully unaware of path structures) and will require user interaction to tidy the directory tree.

Bex: If I get a few minutes this afternoon I'll look at extending the functionality to reverse the process.

Jon

Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Post by Bex » Fri May 26, 2006 8:47 am

That was fast! (I hope the girls not complaining :wink: )

Looking forward to the next version but I really would like it to work on full paths since I got alot of unwanted underscores in my paths which is time consuming to remove manually.

Great work.
/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

Steegy
Posts: 3452
Joined: Sat Nov 05, 2005 7:17 pm
Location: Belgium
Contact:

Post by Steegy » Fri May 26, 2006 8:49 am

@Bex:
Why don't you fix the tags/library and then use auto-organise with the correct info?
Anyway, I was just wondering.
Extensions: ExternalTools, ExtractFields, SongPreviewer, LinkedTracks, CleanImport, and some other scripts (Need Help with Addons > List of All Scripts).

Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Post by Bex » Fri May 26, 2006 8:59 am

I often have information in my folders which isn't in any tags. But sometimes I regret it...

/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

JonSenior
Posts: 9
Joined: Thu May 25, 2006 5:55 am

Post by JonSenior » Fri May 26, 2006 9:07 am

Bex: Just for you... I've just reversed the process. When I said that it doesn't understand paths, what I meant was that it doesn't break down the path into directories and check to see if any of those directories have been emptied by it's actions. It takes the full path for a file and adds (Or in this case, removes) underscore characters.

Install as per any other script. Select one or more files and it'll tell you how many files it's going to operate on, and give you an option to bail out.

I would advise backing up a directory of files and testing it on them first!

At some point I might look at extending this if I find it useful, but to be honest, it'll only be of academic interest to me. Good luck

Jon

Code: Select all

' A simple script that adds underscores to path names.
Sub AddUnderscores
  ' Define variables
  Dim list, itm, i, tmp, res, qty, message

  ' Get list of selected tracks from MediaMonkey
  Set list = SDB.CurrentSongList 

  ' Process all selected tracks
  res = SDB.MessageBox( "About to (possibly) modify " + CStr(list.count) + " files. Continue?", mtConfirmation, Array(mbOk, mbCancel))

  if res = mrOK then
    For i=0 To list.count-1
      Set itm = list.Item(i)

      tmp = Replace(itm.Path, "_", " ")
      itm.Path = tmp
      itm.UpdateDB
    Next
  End If
End Sub

Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Post by Bex » Fri May 26, 2006 9:12 am

Thanks alot!

/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

JonSenior
Posts: 9
Joined: Thu May 25, 2006 5:55 am

Post by JonSenior » Fri May 26, 2006 10:23 am

OK. Boredom set in. Consequently it now trims empty directories after operating on the files.

Bex: To make this work for you, you need to replace

Code: Select all

      tmp = Replace(itm.Path, " - ", "-")
      tmp = Replace(tmp, " ", "_")
with

Code: Select all

      tmp = Replace(itm.Path, "_", " ")
This doesn't update the tree on the left hand side, but manually collapsing the tree and then re-opening it will cause a refresh. Not sure how to do this in code so it is left as an exercise for the reader. I'll crosspost this to the Scripts forum in case anyone else finds it useful.

Hope this helps.

Code: Select all

' A simple script that adds underscores to path names.
Sub AddUnderscores
  ' Define variables
  Dim list, itm, i, tmp, res, fso, dirList, key

  Set fso = CreateObject("Scripting.FileSystemObject")
  Set dirList = CreateObject("Scripting.Dictionary")

  ' Get list of selected tracks from MediaMonkey
  Set list = SDB.CurrentSongList 

  ' Process all selected tracks
  res = SDB.MessageBox( "About to (possibly) modify " + CStr(list.count) + " files. Continue?", mtConfirmation, Array(mbOk, mbCancel))

  if res = mrOK then
    For i=0 To list.count-1
      Set itm = list.Item(i)
      ' Get the folder in which itm resides
      key = fso.GetFile(itm.Path).ParentFolder
      If not dirList.exists(key) Then
        dirList.Add key, key
      End If

      tmp = Replace(itm.Path, " - ", "-")
      tmp = Replace(tmp, " ", "_")
      itm.Path = tmp
      itm.UpdateDB
    Next
  End If

  list = dirList.Items
  For each i in list
    Call DeleteDirectoryIfEmpty(fso.GetFolder(i))
  Next  

End Sub

' Recursively delete empty directories
Sub DeleteDirectoryIfEmpty(dir)
dim parent
    If dir.Files.Count = 0 and dir.SubFolders.Count = 0 Then
      Set parent = dir.ParentFolder
      dir.Delete
      Call DeleteDirectoryIfEmpty(parent)
    End If
End Sub

Plazma
Posts: 52
Joined: Thu Nov 03, 2005 9:59 am

Post by Plazma » Thu Nov 29, 2007 4:32 pm

Can anyone mod this script to change apostrophes from ' to ` ... if you could that would be great i tried but Im not sure how

thx in advance!

trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto » Fri Nov 30, 2007 4:04 am

Which field are you trying to update, just the song path?
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.

Plazma
Posts: 52
Joined: Thu Nov 03, 2005 9:59 am

Post by Plazma » Fri Nov 30, 2007 4:22 am

Ya - just the song's filename - i manually go in and replace ' with ` so it can be read on my server. Just wondering if anyone could add it to the script - if so id appreciate it alot!:D

Post Reply