Tonight I want to present my first VBScript. Before having lunch, I had some free time, so I surfed the net, when bumped into the developer section at Skype.com. I remembered, my every OS-reinstall destroyed Skype chat history. But to be honest, the default format of the history filer is very messy, so it is difficult to read. I decided to delve into the SkypeAPI technology and it turned out that making simple scripts is so easy. You can use PHP, JavaSctipt, JScript, Java and VBScript. I choose the last one, because it seemed to be the most easy to run in Windows. You just put in a file with vbs extension, and run. So here is the code I wrote, I'll tell how it works soon:
' Define global variables Dim oFSO, chat_file, folder_to_save ' Directory where You want to save history (you can modify it) ' Now it is relative, so it will be created where Your *.vbs script runs folder_to_save = "SkypeChatHistory" line_count = 0 ' Create FSO Set oFSO = CreateObject("Scripting.FileSystemObject") set_next_free_dir() ' Connect to Skype API via COM Set oSkype = WScript.CreateObject("Skype4COM.Skype", "Skype_") ' Open skype, if it is not running If Not oSkype.Client.IsRunning Then oSkype.Client.Start() End If WScript.Echo "Skype history will be saved. Found " & oSkype.Chats.Count & " chat group." ' Iterate chats For Each oChat In oSkype.Chats names = "" ' First name is You, so it is unnecessary to keep no_1st_flag = TRUE For Each oUser In oChat.Members If no_1st_flag Then no_1st_flag = FALSE Else names = names & "_" & oUser.FullName End If Next get_file("chat" & names & ".txt") chat_file.WriteLine(vbNewLine & "==== CHAT HISTORY (" & Replace(names, "_", "") & ") ====" & vbNewLine) line_count = line_count + oChat.Messages.Count ' Fix by an anonymous commenter If oChat.Messages.Count > 0 Then For Each oMsg In oChat.Messages ' Fix by Vadim Kravchenko On Error Resume Next chat_file.WriteLine(oMsg.FromDisplayName & " (" & oMsg.Timestamp & "): " & oMsg.Body) Next End If chat_file.Close Next WScript.Echo "Backup was finished (" & line_count & " line saved). You can find your chats in: ./" & folder_to_save ' Garbage collection SET chat_file = NOTHING SET folder_to_save = NOTHING SET oFSO = NOTHING SET oSkype = NOTHING ' Access to a file given by name Sub get_file(file_name) ' Parameter fix by: rommeech Set chat_file = oFSO.OpenTextFile(folder_to_save & "/" & file_name, 8, True, -1) End Sub ' Find an appropriate directory the logs to save, however, to avoid collision with former dirs Sub set_next_free_dir() If oFSO.FolderExists(folder_to_save) Then ext = 1 While oFSO.FolderExists(folder_to_save & "_" & ext) And ext < 100 ext = ext + 1 Wend folder_to_save = folder_to_save & "_" & ext End If oFSO.CreateFolder(folder_to_save) End Sub
Save it to your Desktop (*.vbs), and double click on it. At the first time, Skype will ask you, whether you are aware of the security issues, and really want to add permission to connect the SkypeAPI ... if you trust me, just click OK. It will creates a folder called SkypeChatHistory and some files in it. Every filename consists the other member of the chat sessions, so, if you talked 3 times with your friend, John Doe, there will be a 'chat_John Doe.txt' in SkypeChatHistory with 3 conversation in it. If you are keen-eyed, you will notice conversation lines are in reversed order. --homework-- :)
Now, I think, this code is quiet enough to save messages, and learn from it the way it works. If you can write a really small function to invert the collection (oChat.Messages), please comment it, and I will extend the code. Perhaps I make a gui for it with Visual Studio, I don't know. It must be dozen of good Skype backup applications already.
Update: eigenein did a fantastic job making a Windows application for the same purposes: http://eigenein.github.com/skype-historian-website/