Monday, April 30, 2007

Python: unknown Encoding

I nearly pulled my hair out while trying to find the solution for this error:

LookupError: unknown encoding: cp720

FROM this following code fragment:

print "Name: ", objItem.Name

It was obvious, since my Windows System is an Arabic locale, with English default text, that I was surly to have some kind of problem.

I managed to find a lot of information regarding codecs, encoding, local, setlocale, etc. However, I managed to get it solved by using the str() function!

print "Name: ", str(objItem.Name)

Voi la..

Windows Grep Functionality

Windows has a find command which can be used in batch files and in the cmd prompt to search for strings in files or in output. My most recent discoveries is the type command and using find in a grep like functionality.

Say you have a text file with lines of code/numbers/names/etc and you'd like to see the line which has the text you are looking for. You'd type the following:

> type file.txt | find /I "something"

Replace file.txt with the filename you want to search in, and something with the text you are looking for.

The /I switch is to make the search case insensitive. Type find /? at the command prompt for more options.

Here is an example. If you want to find out if your python bin directory is in your path you would type:

> SET | find /I "python"
results:
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Common Files\GTK\2.0\bin;C:\bin;C:\Python24;C:\Program Files\Windows Script Encoder;C:\Program Files\7-Zip
Note: The SET command prints out your environment variables. Much like env does on *ix.

Follow here, for more Useful Windows command prompt commands.

Monday, April 23, 2007

Revelation

"The uglier the code, the prettier the program."
- Transparently

Friday, April 20, 2007

Renaming Script

I have a ton of mp3 files in different folders which are in the format below;

Track # - Song Name (Artist Name).mp3
ex. 01 - La Fille De Pekin (Frederick Rousseau).mp3


I wanted to rename all the files so I could import them easily using the following format;

Track # - Artist Name - Song Name.mp3
ex. 01 - Frederick Rousseau - La Fille De Pekin.mp3

This format helps me to adjust the meta data while importing. I wrote the following VBScript to rename the files in a directory :D



Option Explicit
On Error Resume Next

Dim a: a = WScript.Arguments.item(0)
Dim fso: Set fso=CreateObject("Scripting.FileSystemObject")
Dim lFolder: Set lFolder = fso.GetFolder(a)
Dim lFiles: Set lFiles = lFolder.Files
Dim lFile, numFiles
numFiles = 0
For each lFile in lFiles
RenameFile(lFile)
Next

Function RenameFile(fileObject)
Dim oldName, newName, fullnewName
Dim posDash, posParOpen, posParClose
Dim artistName, songName, trackNum

oldName = fileObject.Name
'get location of the dash
posDash = InStr(oldName, "-")
'get location of the first (
posParOpen = InStrRev(oldName,"(")
'get location of last )
posParClose = InStrRev(oldName, ")")

'get the track number
trackNum = Mid(oldName,1,posDash)
trackNum = Replace(trackNum,"-","")
trackNum = trim(trackNum)

'get the song name
songName = Mid(oldName,posDash,Cint(posParOpen - PosDash))
songName = Replace(songName,"-","")
songName = Trim(songName)

'get the artist name
artistName = Mid(oldName,posParOpen,Cint(posParClose - PosParOpen))
artistName = Replace(artistName,"(","")
artistName = Trim(artistName)

'new name formatted
newName = trackNum & " - " & artistName & " - " & songName & ".mp3"
fullNewName = fileObject.ParentFolder & "\" & newName

WSCript.Echo
WScript.Echo "Renaming.. " & fileObject.Name
WScript.Echo "To........ " & newName
'comment the following line to test it only.
fileObject.Move fullNewName
WScript.Echo
numFiles = numFiles + 1
End Function


WScript.Echo "Total Files Modified: " & numFiles
Set fso = Nothing
Set lFolder = Nothing
Set lFiles = Nothing

All you have to do to use it is copy paste into a vbs file "reformat.vbs" for example. Then run it using the command line:

C:\> cscript reformat.vbs "C:\Music\Some Messed up Album\"

Here is a link to the script, if you wish to download it. Just rename the file.
Link: Reformat.txt

Thursday, April 19, 2007

Thursday Work

I don't usually work on thursdays. Today however is an exception. I'm doing critical work, kind of confidential. So if I tell you, I'll have to kill you :p

Wednesday, April 18, 2007

Automation Frustration

When I mention computers, how do you feel? I bet you feel a chill, a little frustration, maybe a little confusion as well. It turns out my Automation of the Patch scanning software does not save the report correctly. Now its time to fix this problem and figure out what is going on. Another obstacle is that it is automated to run way early in the AM, after that follows a script which formats the output. I'll need to find a way to timestamp the date on the XML file to figure out if it is the most recent report or simply an outdated copy.

Sunday, April 15, 2007

Publishing Hiccup

To my surprise I logged in this morning to check on my application since I scheduled it to run a little after midnight. I found out it ran, but nothing happened! I went through the batch file, only to find my debug code on.. (the echo before cscript command) *sigh* Its good I arrived to work early. I re-ran the applications. Which are proving to be quite hefty on the system but definitly worth the overhead.