Using VB Script to read contents of .msg file - vbscript

I have a folder with thousands of Outlook .msg files.
I'd like to know if it's possible to write a VB Script that can read the sender and receiver from each file, and move the .msg file to a folder based on this info?
Thanks

You shouldn't ask yes/no questions unless you expect the answer to be either "yes" or "no".
Set ol = CreateObject("Outlook.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\some\folder").Files
If LCase(fso.GetExtensionName(f)) = "msg" Then
Set msg = ol.CreateItemFromTemplate(f.Path)
WScript.Echo msg.Sender.Name
For Each rcpt In msg.Recipients
WScript.Echo rcpt.Name
Next
End If
Next

For reading the contents of a .msg file, I have used the following approach.
use CreateItemFromTemplate for the msg file using the outlook object
Use this outlook object to save the data into a .txt file
Once the .txt file is created read it and use the data as required
Script:
Dim OL : Set OL=CreateObject("Outlook.Application")
Dim Msg ':Set Msg= CreateObject("Outlook.MailItem")
Set Msg = OL.CreateItemFromTemplate("C:\test.msg")
'MsgBox Msg.Subject
Msg.saveAs "C:\test.txt", olDoc
'The above statement will save the contents of .msg file into the designate .txt file
Set OL = Nothing
Set Msg = Nothing
Once the .txt file is created use it as required for your computations.

Related

Why is my readall function popping up a error? [duplicate]

I have 3 scripts wherein they all must be run in the right order repeatedly.
1st script - performs a process on a list of PCs (listed on a textfile input) depending if they are online/offline. It outputs a list of the PCs which were online and another list of the offline ones
2nd script - gets the PC difference from the original list and the output of the 1st script to know which machines have run the process from the 1st script
3rd script - using the differences, updates the input list
The script below is the 3rd script. Whenever I run it, it ends with an error "input past end of file". I've tried several modifications and it always ends that way.
My idea for the 3rd script is that the Differences.txt output from the 2nd file are the ones that still need to run the process form the first script, so I simply delete the original input file and rename this one into the new output file. However, I have to also keep track of the ones that were already done with the process so I have to list/append them to another text file.
Thanks in advance for any help.
Option Explicit
Dim objFso
Dim Fso
Dim firstfile,secondfile,file,fileText
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("Machines.ini") Then objFSO.DeleteFile("Machines.ini")
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "Differences.txt", "Machines.ini"
firstfile="Notified.txt"
secondfile="Notified-all.txt"
Set fso=CreateObject("Scripting.FileSystemObject")
Set file=fso.OpenTextFile(firstfile)
fileText = fileText & file.ReadAll() & vbCrLf
file.Close
Set file=fso.OpenTextFile(secondfile)
fileText=filetext & file.ReadAll()
file.Close
set file=fso.CreateTextFile(secondfile,true)
file.Write fileText
file.close
You get that error when you call ReadAll on an empty file. Check the AtEndOfStream property and read the content only if it's false:
If Not file.AtEndOfStream Then fileText = fileText & file.ReadAll

VBScript Text-To-Speech (to text)

I made a very simple text-to-speech program using VBScript (which I have limited experience with). It simply opens a command prompt, you type something, and Windows spvoice speaks the message.
I want this app to take the message input and add that text to a separate .txt file. So basically, this would be text-to-speech-to-text. Is VBScript too limited to read and write to text file?
Dim message, sapi
message=InputBox("Enter text:","Enter Text")
Set sapi=CreateObject("sapi.spvoice")
sapi.Speak message
You could use the following to write the message to a text file.
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile _
("C:\SpeechMessage.txt",2,true)
objFileToWrite.WriteLine(message)
objFileToWrite.Close
Set objFileToWrite = Nothing
VBScript Text Files: Read, Write, Append
EDIT:
Create the following to avoid permissions issues on the root of C:
C:\Speech\
And Try this code that will create the file if it does not exist and append to it if it does.
Set objFileToWrite = CreateObject("Scripting.FileSystemObject")
If objFileToWrite.FileExists("C:\Speech\Message.txt") then
Set objFileToWrite = objFileToWrite.OpenTextFile("C:\Speech\SpeechMessage.txt",8,true)
Else
Set objFileToWrite = objFileToWrite.CreateTextFile("C:\Speech\SpeechMessage.txt")
End If
objFileToWrite.WriteLine(message)
objFileToWrite.Close
Set objFileToWrite = Nothing
This seemed to work. I was running into issues with 800A0046 error at first. To fix this, I made a new folder in the C: drive and rooted to that.
Dim message, sapi, objFileToWrite
message=InputBox("What do you want me to say?","Speak to Me")
Set sapi=CreateObject("sapi.spvoice")
sapi.Speak message
Set objFileToWrite =
CreateObject("Scripting.FileSystemObject")
_.OpenTextFile("C:\Speech\LiveChat.txt",2,true)
objFileToWrite.WriteLine(message)
objFileToWrite.Close
Set objFileToWrite = Nothing

Using VBScript to examine properties of files within a zip file

I'm trying to use VBScript to examine the contents of several hundred .zip files. Essentially what I want to do is run through each .zip and find all of the files wihtin that zip file. For each one of these files within the zip, I want to record some information about it to an Oracle database. That information being: file name and file modified date.
So far, my solution has been extracting each zips folder structure to a temp folder then running through the temp folder with an fso object. However, this has been proven to be very slow.
Is there a way to accoplish this without unziping the zip files?
Ouch man. I have never heard of vbscript zip object. But it has been a long time since I have done vbscript. Is there anyway you can avoid it?
I did some googling for you. I did find this: http://www.example-code.com/vbscript/zip_List.asp Chilkat has done a lot of stuff I thought not possible. This gives me the impression - that what you are trying to do is not going to be painless.
If given the problem you have I would find a different solution than vbscript. But if you pull-it-off I would vote for you to be mayor of vb land
You can do it in place with Shell Objects. But it will be just as slow, maybe. If just name and date Explorer may get it direct from the zip directory (at the end of the file so the whole file still needs to be read).
This copies items in a folder to another folder. A zip file is a folder so it will copy in and copy out.
To Zip
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Unzip (note SrcFolder and DestFolder are reversed)
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set DestFldr=objShell.NameSpace(Ag(1))
Set SrcFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"
To Create a blank zip. (I should have used an ADODB binary stream rather than an FSO text stream, but it shouldn't matter)
Set Ag=Wscript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Ag(0), 8, vbtrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 to 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip

VBscript error input past end of file

I have 3 scripts wherein they all must be run in the right order repeatedly.
1st script - performs a process on a list of PCs (listed on a textfile input) depending if they are online/offline. It outputs a list of the PCs which were online and another list of the offline ones
2nd script - gets the PC difference from the original list and the output of the 1st script to know which machines have run the process from the 1st script
3rd script - using the differences, updates the input list
The script below is the 3rd script. Whenever I run it, it ends with an error "input past end of file". I've tried several modifications and it always ends that way.
My idea for the 3rd script is that the Differences.txt output from the 2nd file are the ones that still need to run the process form the first script, so I simply delete the original input file and rename this one into the new output file. However, I have to also keep track of the ones that were already done with the process so I have to list/append them to another text file.
Thanks in advance for any help.
Option Explicit
Dim objFso
Dim Fso
Dim firstfile,secondfile,file,fileText
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("Machines.ini") Then objFSO.DeleteFile("Machines.ini")
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "Differences.txt", "Machines.ini"
firstfile="Notified.txt"
secondfile="Notified-all.txt"
Set fso=CreateObject("Scripting.FileSystemObject")
Set file=fso.OpenTextFile(firstfile)
fileText = fileText & file.ReadAll() & vbCrLf
file.Close
Set file=fso.OpenTextFile(secondfile)
fileText=filetext & file.ReadAll()
file.Close
set file=fso.CreateTextFile(secondfile,true)
file.Write fileText
file.close
You get that error when you call ReadAll on an empty file. Check the AtEndOfStream property and read the content only if it's false:
If Not file.AtEndOfStream Then fileText = fileText & file.ReadAll

Copy folder contents to a created .zip file: 'file not found or no read permissions'

I'm trying to create a .zip file from an existing folder using JScript and it seems that my copyHere function is not copying to the .zip folder. Instead I get a popup box titled 'Compressed (zipped) Folder Error' with the message 'file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property (32).
Here is the script I'm using:
//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);
//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;
zipFolder = objShell.NameSpace(zipPath);
sourceItems = objShell.NameSpace(sourcePath).items();
if (zipFolder != null)
{
zipFolder.CopyHere(sourceItems);
WScript.Sleep(1000);
}
Now the CopyHere function works for copying the contents of the sourcePath to a normal folder but when I try to create a .zip file and copy the contents to that, nothing happens. Any ideas on why copyHere is not copying the contents of the sourcePath to the .zip?
An Example for calling this script would be:
cscript win-zip.js C:\desired\zip\file.zip C:\path\to\source\folder
And the desired outcome would be that file.zip was created and now contains the contents of the source folder. Could this be a problem with permissions? What might cause this behavior?
Side Note, using a vbScript and the same commands I can successfully create and populate a .zip, so why doesn't it work using jscript!
Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)
' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)
' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items
' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied
Any helpful comments are greatly appreciated, thanks!
I encountered the same problem (file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property).
The problem disapeared as soon as I found and suppress a 0 length file somewhere in the directory to be copied with the copyhere method.
As Raymond said, the problem was that I had an open reference to the .zip folder in the file variable that I created (which had a lock on the folder so I could not copy any contents to it). The solution is to call
file.Close();
after writing to the file, that way we can access the file to copy contents to it :)

Resources