Friday 16 January 2009

Save Skype chat history

Hi,

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/

Sleep well!

56 comments:

  1. It doesn't work to me. Restores just 3 files and makes error
    line 37
    char 3
    error: Invalid procedure call or agrument
    code 800A0005

    ReplyDelete
  2. Hi,

    Unfortunately I'm not a VB programmer, I don't know VB at all. The problem, I guess, the type conversion at line 39:

    chat_file.WriteLine(oMsg.FromDisplayName & " (" & oMsg.Timestamp & "): " & oMsg.Body)

    Target: oMsg.Body. oMsg is an IMessage object. It has a property, called Body contains the body of the chat message. However, it could be several types of messages, like status information, file sending, joins, quits... I tried to cast it (CStr()), but it didn't help at all, neither checking whether it is empty or null or anything. Maybe if it gets the type of the message could solve the problem but I didn't have much time for this. Although, if you solve it, I add your name and snippet to the post:) Some info:
    oMsg.Type contains the type of the message. The types:

    Enumerator:
    cmeUnknown Unknown message type.
    cmeCreatedChatWith Chat created.
    cmeSawMembers Saw chat members.
    cmeAddedMembers Added chat members.
    cmeSetTopic Chat topic changed.
    cmeSaid Sent message.
    cmeLeft Left the chat.
    cmeEmoted Sent emoted message.
    cmePostedContacts Posted contacts.
    cmeGapInChat Indicates gap in chat.
    cmeSetRole Member role changed.
    cmeKicked Member kicked from chat.
    cmeSetOptions Chat options changed.
    cmeKickBanned Member kicked and banned from chat.
    cmeJoinedAsApplicant Member joined as applicant.
    cmeSetPicture Chat picture changed.
    cmeSetGuidelines Chat guidelines changed.

    Anyway, thank you for reporting the bug! When I wrote the code, it worked, but now I get the same error message.

    Cheers,
    Peter

    ReplyDelete
  3. Peter, you are AWESOME!

    I just downloaded Skype v4.1.0.141 and your VBS code WORKS FLAWLESSLY! A directory called "SkypeChatHistory" is created with a text file "chat_.txt". As you had mentioned, the chat order will be "in reverse" (which is fine for me).

    If there are problems, it could be that the code from this blogspot website does not format correctly when copied. I had to copy-paste this code into WORDpad and THEN copy-paste that code into a NOTEpad text file. Then, I renamed the TXT extension into VBS.

    Keep up the good work.. peace, my brother!

    Sincerely,
    Tariq Rana - USA

    ReplyDelete
  4. Greet Job..

    Can u give me suggestion how to run your vb script file in button click event in html.I don't know vb script.

    Thanks
    Basanth.
    alluribasanth@gmail.com

    ReplyDelete
  5. When running I receive the following error message from Windows Script Host:
    Script: C:\...
    Line: 34
    Char: 2
    Error: Invalide procedure call argument
    Code: 800A0005
    Source: Microsoft VBScript runtime error

    ReplyDelete
  6. Hi,

    Great thanks to Vadim Kravchenko for his fix. oMsg.Type unable to handle file transfer annotation, so as quick fix it's better to step through in that cases. Thanks again.
    (Unfortunately I can't test it on a Windows machine, so I hope I insert the snippet at the right place).

    Regards,
    Peter

    ReplyDelete
  7. Excellent work!

    Works just FINE!

    ReplyDelete
  8. Copied the code into Notepad, copied and pasted the code into it and saved it as a vbs extension. Works excellent... Thanks!!

    ReplyDelete
  9. Excellent work Peter!
    I re-installed my OS, and spent ages trying to put the chat history back.
    With your little bit of code, at least now I can reference my chat history again.
    Thanks heaps! Mark

    ReplyDelete
  10. Thank you so much!!! I spent 3 hours on the net looking for a tool like yours.
    Just a thing: you need the extra stuff when you install skype on your computer (in order to use the Skype4COM dll in your code). I had to install a new version of skype to get it.

    ReplyDelete
  11. works great!! Cheers mate!
    I just copy and pasted straight into notepad and saved as vbs file - and it worked :)

    ReplyDelete
  12. I'm having a problem identifying which part of the code selects which chats to save, the one chat which I need to export on a regular basis to a text file is not exporting. Otherwise great piece of code!

    ReplyDelete
  13. Hello, I'm using Win 7, and it doesnt worked for me, because, it found an error in 34. lime:

    chat_file.WriteLine(vbNewLine & "==== CHAT HYSTORY (" & Replace(names, "_", "") & ") ====" & vbNewLine)

    I dont know what is the problem :S please help !

    ReplyDelete
  14. thanks. works great! this is what i need.

    ReplyDelete
  15. Try to replace following subroutine as shown:

    Sub get_file(file_name)
    file_name = Replace(file_name, "/", "-")
    Set chat_file = oFSO.OpenTextFile(folder_to_save & "/" & file_name, 8, True)
    End Sub

    ReplyDelete
  16. For fixing error in line 34 you can replace line 54 from

    Set chat_file = oFSO.OpenTextFile(folder_to_save & "/" & file_name, 8, True)

    to:

    Set chat_file = oFSO.OpenTextFile(folder_to_save & "/" & file_name, 8, True, -1)

    And your files will be saved in UTF-8

    ReplyDelete
  17. The fix is in the code, thanks rommeech ;)

    ReplyDelete
  18. I get an error saying:

    line: 13
    char: 1
    error: could not create object named "Skype4COM.Skype"
    code:80040151
    source: WScript.CreateObject

    my computer runs windows 7, i don't know if that's important..

    ReplyDelete
  19. Great stuff, thanks! Windows 7 Home Premium 64bit, saved as a .vbs file and it just worked, lovely :)

    Oh yeah, and it's Skype Beta 5.0.0.105

    Little typo correction:
    Line 34 Col 45: change the Y to an I to read "HISTORY", instead of "HYSTORY" ;)

    ReplyDelete
  20. Typo fixed, thanks Luke ;)

    ReplyDelete
  21. Hi, thanks v.much worked well - phil

    ReplyDelete
  22. Thank you very much!
    Could you give me a hint on how to extract chats from backed up files of a previous OS (WinXP)? Thanks

    ReplyDelete
  23. Do I just copy the code and save it as a vbs file with wordpad?

    ReplyDelete
  24. Hello,
    thanks for your work but i also cant run the script (windows 7 64 bit)

    error says: Line 13
    couldnt create the object "Skype4COM.Skype"
    Source: WScript.CreateObject

    any help ?

    ReplyDelete
  25. i have an error on line 55 char 1

    i dont know much about scripts, but i will try to solve this. i just want to let you know though.

    ReplyDelete
  26. Thank you very very very much!!!!
    It work perfect in WinXP with Skype 4.2.0.169

    ReplyDelete
  27. I receive the error that line 1 char 1 is an invalid character.
    Code 800A0408

    ReplyDelete
  28. Thanks Itarato, great simple program. Few people have actually written something like this.

    Those of you getting this error:
    error says: Line 13 couldnt create the object "Skype4COM.Skype"
    I'm guessing that you either didn't install the Extras Manager when you installed Skype, or you're not logged into Skype when you run the VBS script.

    By the way, for those with access to a Unix command line, there's always the "tac" command to reverse the lines in the files.

    ReplyDelete
  29. Hi, your script is cool, however, you have a problem in line 36. When oChat.Messagex.Count is equal to zero, the loop will cause a runtime error. The situation is not very probable, because you'll have to have at least one contact with no converstations at all. The modification is as follows:

    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

    It would be nice if you could include it in your original post, Peter.

    ReplyDelete
  30. Fix is added, great thanks :)

    ReplyDelete
  31. Simkl IM Chat & Voice Recorder http://download.simkl.com will help you record all Skype conversations automatically including Text and Voice conversations. The program will also store these conversations online for easy access through history.im website.

    ReplyDelete
  32. Thank you for posting this code, I have written a C# .NET app based on your idea:

    http://www.gammatwo.com/2010/12/18/save-skype-chat-history/

    Bye

    ReplyDelete
  33. great piece of code!
    worked fine for me!

    ReplyDelete
  34. Can you please update it for Skype 5 as it is saying that 'Could not locate automation class named "Skype4COM.Skype"'

    I've Skype 5 installed.

    ReplyDelete
  35. Brilliant, thank you, it worked a treat :-)

    ReplyDelete
  36. perfect work, it's so easy, you have prepared it just to put and launch. thanks!

    ReplyDelete
  37. Works fine. Thank you so much!

    ReplyDelete
  38. Can you please update it for Skype 5.5 as it is saying that 'Could not locate automation class named "Skype4COM.Skype"'

    ReplyDelete
  39. Thank you! I was inspired and have done this: http://eigenein.github.com/skype-historian-website/

    ReplyDelete
  40. Hi eigenein,

    That's great, I'm glad I could help:) I put the link into the blog so other people can find it.

    ReplyDelete
  41. Please give us a new code--new Skype does not have Extras Manager!

    Thanks :)

    ReplyDelete
  42. This comment has been removed by the author.

    ReplyDelete
  43. Having same problem as others in Win 7, 64 bit.

    In a dialog box titled, "Windows Script Host" the message contents are:

    Script: ...\chatHistSave.vbs
    Line: 13
    Char: 1
    Error: Could not create object named "Skype4COM.Skype".
    Code: 80040154
    Source: WScript.CreateObject

    Anyone have a fix for this yet?

    ReplyDelete
  44. thanx for info, but as for me, i usually record skype calls using this tool http://www.imcapture.com/IMCapture_for_Skype/, it's simple and nice!)

    ReplyDelete
  45. thanks a lot
    worked with Win7 and Skype 5.10.0.116
    (saved 1548 chat groups)

    ReplyDelete
  46. Thanks a lot for the script. It seems to work on Skype 5.10. on my Win7 64bit (with a little tweak)

    A note for 64bit Vista/7 users:
    The script won't run by itself out of the box because "Currently it's been decided that it's by design that Skype4COM works only for 32bit applications on 64bit OS."
    [Source: https://jira.skype.com/browse/SPA-420?page=com.atlassian.jira.plugin.system.issuetabpanels:changehistory-tabpanel]

    That said, you can still run it with the 32bit version of cscript.exe in the following way:

    \Windows\SysWOW64\cscript.exe SkypeLogDump.vbs

    You could make a .bat file with that command to retain it's "click & go" functionality...

    ReplyDelete
  47. Thanks for the script Peter, it works good in the Skype version 6.0.0 with the workaround proposed by IceDragon.
    Only for some chats cant collect the name of the other person, but thats minor :) and i pass well without that :D

    ReplyDelete
  48. My Windows is x64 and Skype 6.0.0.126

    I used the command "\Windows\SysWOW64\cscript.exe SkypeLogDump.vbs" and WORKS GREAT !!!

    THANKS !!!

    ReplyDelete
  49. Great Job Bro..

    Thanks a lot..

    ReplyDelete

Note: only a member of this blog may post a comment.