Deleting Songs on the fly from Windows Media Player

When listening to audio in Windows Media Player (at least 10 and 11), it doesn’t present you with an easy way to delete whatever you are listening to. I’ve come up with a convoluted solution to remedy this. Although convoluted, I find it suits my needs.
First, I created a new playlist in WMP (File > Create Playlist) which I named ‘Deletables’. Next, I created a Python script in the same folder as the playlist, which will read it and delete all audio files listed in there. Finally, I created a shortcut file with a key shortcut to execute this Python script (see this post for more on creating key shortcuts in Windows XP). The shortcut file’s target will look like this: "python scriptfile.py" (add paths where necessary, of course). Also, make sure the starting path corresponds with the location of the script and the Deletables playlist, or the script will try to access files in your user folder (Windows-quirk, I guess).
Now, whenever I’m listening to some audio files and I encounter something I want to delete, I just have to right-click the item in WMP’s ‘Play now’-list, and select ‘Add to… > Deletables’. By using the shortcut to execute the Python script, the file will be deleted. The file will not stop playing though, as it is still loaded in memory.
You don’t have to call the script every time after you add an item to Deletables. I myself only call it once a week or so, and then it will delete all the files added to Deletables since the last time the script was called. This also gives me some time to change my mind on a delete, if that may happen. To undo a future delete, simply open the Deletable playlist, and remove the files that have been given a second chance.
The Python code I used:

import os, re

f = open('Deletables.wpl')
l = f.readline()
a = ''
failed = ''

while l:
  b = re.findall('<media src="(.*?)"',l)
  if not b:
    a += l
    l = f.readline()
    continue
  file = b[0].replace('&apos;','\'')
  tfile = os.path.realpath(file)
  if os.path.isfile(tfile):
    os.remove(tfile)
  else:
    failed += l.strip() + '\n'
  l = f.readline()

f.close()

f = open('Deletables.wpl','w')
f.write(a)
f.close()

if failed:
  f = open('failedDeletes.txt','a')
  f.write(failed)
  f.close()

Note: when the script is unable to delete a file (e.g. when the file was already deleted), it will write the location of said file to a text file, ‘failedDeletes.txt’. I find this useful, but if you do not, just remove the last four lines of code.

Advertisements
This entry was posted in Geen categorie. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s