473,383 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Rename MSG file

Ok, I don’t have any code that does archiving. The way it works here is users get project emails sent to their inbox. Then they move the emails to the corresponding project on a drive in an email or correspondense folder. What I am looking for is writing an app or script that will rename the MSG file to sender, subject, date.msg. Something set as service that will run at night.

So, after the emails have been moved manually to the project folder, I would like my code to go through the drive and through the project folders and the email and corespondens folders, look for .MSG files and rename them.
Thanks for your time, any information to point me in the right direction would be greatly appreciated.

Cheers
Strychtur
Jan 3 '08 #1
6 4219
balabaster
797 Expert 512MB
Ok, I don’t have any code that does archiving. The way it works here is users get project emails sent to their inbox. Then they move the emails to the corresponding project on a drive in an email or correspondense folder. What I am looking for is writing an app or script that will rename the MSG file to sender, subject, date.msg. Something set as service that will run at night.

So, after the emails have been moved manually to the project folder, I would like my code to go through the drive and through the project folders and the email and corespondens folders, look for .MSG files and rename them.
Thanks for your time, any information to point me in the right direction would be greatly appreciated.

Cheers
Strychtur
I might be tempted to get the VSTO (Visual Studio Tools for Office) and write an Outlook AddIn which you could effectively use for the whole export process...it wouldn't be automated, although I guess it could be such that each time you close Outlook it would prompt the user to export their mail at which point it would ask them for the directory to export to. In the VSTO, you would have access to be able to read the message header before using the component parts of the header to save the message.
Jan 3 '08 #2
I might be tempted to get the VSTO (Visual Studio Tools for Office) and write an Outlook AddIn which you could effectively use for the whole export process...it wouldn't be automated, although I guess it could be such that each time you close Outlook it would prompt the user to export their mail at which point it would ask them for the directory to export to. In the VSTO, you would have access to be able to read the message header before using the component parts of the header to save the message.

However there is talk about the new image we are going to use and it may use Outlook 2007. So if that happens, what impact will it have?

I have been thinking about this as well as another approach.
Let the users place the emails in the project folders as is. Then at midnight or so the app or script as a service runs through the drive and projects folders. It will go through the email and correspondence folder for each project looking for .msg files. When it finds one it renames it and flags it as renamed. This way I can cut the user out and not worry about which user is doing it and which user is not.

1. go to drive J:
2. go through project folders
3. go to email and correspondence folder ( 2 separate folders in each project folder. At least there will be one of them there)
4. go to files
5. check extension for .msg
6. check for rename flag
7. if not flagged
8. open file
9. get data
10. rename and flag file

What do you think?

Again thanks for your response and sorry if I am confusing
Jan 3 '08 #3
balabaster
797 Expert 512MB
However there is talk about the new image we are going to use and it may use Outlook 2007. So if that happens, what impact will it have?

I have been thinking about this as well as another approach.
Let the users place the emails in the project folders as is. Then at midnight or so the app or script as a service runs through the drive and projects folders. It will go through the email and correspondence folder for each project looking for .msg files. When it finds one it renames it and flags it as renamed. This way I can cut the user out and not worry about which user is doing it and which user is not.

1. go to drive J:
2. go through project folders
3. go to email and correspondence folder ( 2 separate folders in each project folder. At least there will be one of them there)
4. go to files
5. check extension for .msg
6. check for rename flag
7. if not flagged
8. open file
9. get data
10. rename and flag file

What do you think?

Again thanks for your response and sorry if I am confusing
Are the Outlook 2003/2007 file formats the same? I'm not sure, but if you add a reference to Microsoft Outlook 11.0 Object Library (That's COM, not .NET) which will add your interop references to your application.

Then use something like:
Expand|Select|Wrap|Line Numbers
  1. Imports OL = Microsoft.Office.Interop.Outlook
  2. Module Module1
  3.     Sub Main()
  4.         Dim app As New OL.Application
  5.         Dim oNS As OL.NameSpace = app.GetNamespace("MAPI")
  6.         Dim inboxFld As OL.MAPIFolder = oNS.GetDefaultFolder(OL.OlDefaultFolders.olFolderInbox)
  7.         For Each Msg As Object In inboxFld.Items
  8.             If TypeOf Msg Is OL.MailItem Then
  9.                 Console.WriteLine(Format(Msg.ReceivedTime, "yyyy/mm/dd hh:mm:ss") & " " & Msg.SenderName & ", " & Msg.Subject)
  10.             End If
  11.         Next
  12.         Console.ReadLine()
  13.     End Sub
  14. End Module
  15.  
This would require outlook to be open though...I have to admit, I've never had to open the msg files from the FAT/NTFS though, so I'm not sure what the file structure is...my guess is that they would be of type MailItem and can be processed accordingly...I'll try and have a play and see what I can figure out.
Jan 3 '08 #4
balabaster
797 Expert 512MB
Okay the following code basically opens the MSG file and spits out the relevant info for you...I'll leave you to check to see if it was previously renamed and rename it...this just gives you a report of all msg files in your My Documents folder...
Expand|Select|Wrap|Line Numbers
  1. Imports OL = Microsoft.Office.Interop.Outlook
  2. Module Module1
  3.     Sub Main()
  4.         Dim oApp As New OL.Application
  5.         Dim oDocPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  6.         For Each oFile As String In System.IO.Directory.GetFiles(oDocPath)
  7.             Dim oFileInfo As New System.IO.FileInfo(oFile)
  8.             If oFileInfo.Extension.ToLower = ".msg" Then
  9.                 'Add code to check if renamed already
  10.                 Try
  11.                     Dim oItem As OL.MailItem = oApp.CreateItemFromTemplate(oFileInfo.FullName)
  12.                     Console.WriteLine(oItem.ReceivedTime & " " & oItem.SenderName & " - " & oItem.Subject)
  13.                 Catch Ex As Exception
  14.                     'Ignore any errors
  15.                 End Try
  16.             End If
  17.         Next
  18.         Console.ReadLine()
  19.     End Sub
  20. End Module
  21.  
There is one flaw in that it complains that you need to allow access to outside software to access your mail contacts...I'm sure there's a way to overcome it but that should point you in the right direction at least...
Jan 3 '08 #5
Okay the following code basically opens the MSG file and spits out the relevant info for you...I'll leave you to check to see if it was previously renamed and rename it...this just gives you a report of all msg files in your My Documents folder...
Expand|Select|Wrap|Line Numbers
  1. Imports OL = Microsoft.Office.Interop.Outlook
  2. Module Module1
  3.     Sub Main()
  4.         Dim oApp As New OL.Application
  5.         Dim oDocPath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  6.         For Each oFile As String In System.IO.Directory.GetFiles(oDocPath)
  7.             Dim oFileInfo As New System.IO.FileInfo(oFile)
  8.             If oFileInfo.Extension.ToLower = ".msg" Then
  9.                 'Add code to check if renamed already
  10.                 Try
  11.                     Dim oItem As OL.MailItem = oApp.CreateItemFromTemplate(oFileInfo.FullName)
  12.                     Console.WriteLine(oItem.ReceivedTime & " " & oItem.SenderName & " - " & oItem.Subject)
  13.                 Catch Ex As Exception
  14.                     'Ignore any errors
  15.                 End Try
  16.             End If
  17.         Next
  18.         Console.ReadLine()
  19.     End Sub
  20. End Module
  21.  
There is one flaw in that it complains that you need to allow access to outside software to access your mail contacts...I'm sure there's a way to overcome it but that should point you in the right direction at least...
Ok my boss just informed me, requirements have changed a little. I hate it when they do that. The users will move the emails out of Outlook to wherever. Then they will highlight or select one or more .msg files. When the user right clicks , where it says cut, copy, paste, etc. I would like to add something like ‘RenameMSG’. When that is selected I want my code to go through each selected email by the user and rename it by going into the file itself and pulling out the appropriate data. I will need to roll this out to all users.

Pseudo Code:
Start
Is selected file extension .msg?
No: goto next file.
Yes:
• Get data
o From:
o To:
o Subject:
o Date received
• Rename file
• Change file date to received date
Go to next file
End
I have some questions.
1) Would I be able to do this in VB.Net or C#
2) What kind of project would be best? Windows Service App maybe?
I was also looking at your code.
1) What kind of project did you start? The import line gives me an error

I have to get this done by the 21 of Jan, so hopefully it’s not too complicated.

Thanks a ton and more for your help.
Strychtur
Jan 7 '08 #6
Ok I have this so far and it works for the most part. The thing is if a bunch of email are passed it does not rename all of them, only some. When the rest are passed they are renamed.
'===================
' VBScript source code


Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName

Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set olkApp = GetObject(,"Outlook.Application")
On Error GoTo 0

If TypeName(olkApp) <> "Application" Then
Set olkApp = CreateObject("Outlook.Application")
End If

For Each varFile In WScript.Arguments
If LCase(objFSO.GetExtensionName(varFile)) = "msg" Then
Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
Set objFile = objFSO.GetFile(varFile)
varNewFileName = ReplaceIllegalCharacters(olkMessage.SenderName & " - " & Replace(objFile.Name,".msg","")) & ".msg"
objFile.Name = varNewFileName
Call ModFileDT (objFile.ParentFolder, objFile.Name, olkMessage.ReceivedTime)
End If
Next
Set objFile = Nothing
Set objFSO = Nothing
Set olkMessage = Nothing
Set olkApp = Nothing
WScript.Quit

Function ReplaceIllegalCharacters(strSubject)
Dim strBuffer
strBuffer = Replace(strSubject, ":", "")
strBuffer = Replace(strBuffer, "\", "")
strBuffer = Replace(strBuffer, "/", "")
strBuffer = Replace(strBuffer, "?", "")
strBuffer = Replace(strBuffer, Chr(34), "'")
strBuffer = Replace(strBuffer, "|", "")
ReplaceIllegalCharacters = strBuffer
End Function

Sub ModFileDT(strDir, strFileName, DateTime)
Dim objShell, objFolder, objFile
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(CStr(strDir))
Set objFile = objFolder.ParseName( CStr(strFileName) )
objFile.ModifyDate= CStr(DateTime)
Set objShell = Nothing
Set objFolder = Nothing
Set objFile = Nothing
End Sub
'===================

Also I'm not sure how to debug the code and step through it. I tried MS script debugger with no dice. Debugger just hangs doing nothing.
Jan 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Tom | last post by:
I'm having a problem using a path with spaces as a parameter to os.rename() in a program on WinXP. This works fine at the command line (where the folder "c:\aa bb" exists) > os.rename( "c\aa...
5
by: jez123456 | last post by:
Hi, I’ve written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete C:\test1.mdb and rename C:\temp.mdb as...
1
by: Jay at SCA | last post by:
I've been having the following issue with a windows service I've created in vb.net (.net fw 1.1): Basically, my service monitors a folder for the creation of any new files of a particular type...
2
by: Bruce Russell | last post by:
This may sound stupid but I can't rename the WebForm1.aspx in the solution explorer. The file is located in my local web server at C:\Inetpub\wwwroot\Lab3-VB-Starter\WebForm1.aspx Is there...
5
by: Rothariger | last post by:
Hello.... i want to know if its posible to rename multiple files like windows does.. example: file zzzzzzz.doc file asdasd.doc file esfsefse.doc
5
by: Tony Meyer | last post by:
On Windows, if I do os.rename(old, new) where old is a file that is in-use (e.g. python itself, or a dll that is loaded), I would expect that an error would be raised (e.g. as when os.remove is...
5
by: VB Programmer | last post by:
My web hoster says that I have to rename my ASPNETDB.MDF SqlExpress db for my 2.0 site to work. Is this accurate? If so, how can I rename it without breaking anything on my site? FYI: This is...
12
by: mantrid | last post by:
Im trying to move a file but am having luck my code is below. The temp and target paths are valid as they echo correctly. but I cant get the copy() function to work, or the rename() function ...
1
by: spacehopper_man | last post by:
no "rename" operation in C# !!! - this has been covered in this group before, but I can't find any good answers. what I am trying to do is refresh the content in a file with minimum...
2
by: =?iso-8859-1?b?cultaQ==?= | last post by:
Hi, I would like to rename files (jpg's ones) using a text file containing the new names... Below is the code that doesn't work : ***** #!/usr/bin/python #-*- coding: utf-8 -*- from os...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.