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.

Saturday, April 14, 2007

Publishing My app: Part II

I've cleaned up most of my application. Created a new batch file to test the path of the VBS files and added an argument checked so all I'd need to do is type "runmod.bat 1" and end up running the Defragmentation check on all of our servers.

It was important to be delicate about this issue. I wouldn't want to run it by mistake, and risk stressing the servers during working hours. I created 5 schedules (1 for each module) to run during the early AM.

One of the Modules is a Windows Patch Version check. It transforms an exported XML from LANGuard Security Scanner to a generic XML file. I then have a small script called create_xsl.vbs which reads variables from a config file and creates an XSL file with the relevant information using a template. Sounds pretty tiresome but it works great since now I can write a VBS module file, a config file.. and easily add it to the HTA application without worrying about encoding or re-coding any of the core application.

Related Post: Publishing My App Part I

Wednesday, April 11, 2007

Multi Variable Batch

This is a great example for reading lines through a regular batch file and processing the input at tokens.

Lets say for example you have a file with the following input:

user1 homedir1
user1 homedir2


etc

If you want to, you could add this to your batch file;

for /f "tokens=1*" %%i in (filename.txt) do @echo %%j\%%i


Notice that %%j was not defined, but is automatically assigned a value alphabetically. The output will look something like;

homedir1\user1
homedir2\user2

Publishing My app

So my boss has suggested I start a tech blog about what I really do. So here it is. I'll start it with my task for the day.

I've created this massive HTA application to monitor our servers and produce pretty XML data for easy adaptability. It is mostly complete, now I have to move it to the main station to get it working without any glitches (especially those path errors.. *shivers*).

~Interrupted~

OK, now I have to work on some SQL statements.. sigh.