By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Change modified date on msg file

P: 10
Hi all,

I have a vbscript that renames msg files to Sender - Subject. What I am looking to do is change the modified date of the msg file to be the received date of the email. The thing is I could swear the following code was doing this last week. Upon double checking this week the name was changing but not the modified date. Any help would be great as this is my first vbscript.

Expand|Select|Wrap|Line Numbers
  1. On Error Resume Next
  2.  
  3. Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName
  4. Set olkApp = GetObject(,"Outlook.Application")
  5. If TypeName(olkApp) <> "Application" Then
  6.     Set olkApp = CreateObject("Outlook.Application")
  7. End If
  8. Set objFSO = CreateObject("Scripting.FileSystemObject")
  9. For Each varFile In WScript.Arguments
  10.     Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
  11.     varNewFileName = ReplaceIllegalCharacters(olkMessage.SenderName & "-" & olkMessage.Subject) & ".msg"   
  12.      Set objFile = objFSO.GetFile(varFile)      
  13.      objFile.Name = varNewFileName
  14.     ModFileDT varNewFileName,olkMessage.ReceivedTime 
  15. Next
  16. Set objFile = Nothing
  17. Set objFSO = Nothing
  18. Set olkMessage = Nothing
  19. Set olkApp = Nothing
  20. WScript.Quit
  21.  
  22. Function ReplaceIllegalCharacters(strSubject)
  23.     Dim strBuffer
  24.     strBuffer = Replace(strSubject, ":", "")
  25.     strBuffer = Replace(strBuffer, "\", "")
  26.     strBuffer = Replace(strBuffer, "/", "")
  27.     strBuffer = Replace(strBuffer, "?", "")
  28.     strBuffer = Replace(strBuffer, Chr(34), "'")
  29.     strBuffer = Replace(strBuffer, "|", "")
  30.     ReplaceIllegalCharacters = strBuffer
  31. End Function 
  32.  
  33. Sub ModFileDT  (strFileName, DateTime)
  34.     Dim objShell, objFolder    
  35.     Set objShell = CreateObject("Shell.Application")
  36.     objFolder.Items.Item(strFileName).ModifyDate = DateTime
  37. End Sub
Cheers
Strychtur
Jan 16 '08 #1
Share this Question
Share on Google+
11 Replies


Expert 5K+
P: 8,434
Just at a glance, it looks to me as though you might be missing a line that sets objFolder to the folder, in the ModFileDT routine.
Jan 17 '08 #2

P: 10
Just at a glance, it looks to me as though you might be missing a line that sets objFolder to the folder, in the ModFileDT routine.
Sorry,
But I'm not sure I follow. Could you explain please?
Jan 17 '08 #3

Expert 5K+
P: 8,434
Sorry,
But I'm not sure I follow. Could you explain please?
Well, you define objShell and objFolder. Then you set objShell to point to a new Shell object. Then you start to work with properties of objFolder, but objFolder is just sort of an "empty" object pointer, isn't it? It hasn't been set to anything.

(I've always been a bit vague on working with objects, so I may be completely wrong. It just looks odd.)
Jan 18 '08 #4

P: 10
Well, you define objShell and objFolder. Then you set objShell to point to a new Shell object. Then you start to work with properties of objFolder, but objFolder is just sort of an "empty" object pointer, isn't it? It hasn't been set to anything.

(I've always been a bit vague on working with objects, so I may be completely wrong. It just looks odd.)
Ok how about this then?
Expand|Select|Wrap|Line Numbers
  1. ' VBScript source code
  2. On Error Resume Next
  3.  
  4. Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName, Dir
  5.  
  6. Set olkApp = GetObject(,"Outlook.Application")
  7.  
  8. If TypeName(olkApp) <> "Application" Then
  9.     Set olkApp = CreateObject("Outlook.Application")
  10. End If
  11.  
  12. Set objFSO = CreateObject("Scripting.FileSystemObject")
  13.  
  14. For Each varFile In WScript.Arguments
  15.     Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
  16.     varNewFileName = ReplaceIllegalCharacters(olkMessage.SenderName & "-" & olkMessage.Subject) & ".msg"   
  17.     Set objFile = objFSO.GetFile(varFile)      
  18.     objFile.Name = varNewFileName   
  19.   Call ModFileDT (objFile.Drive,  objFile.Name, olkMessage.ReceivedTime)
  20.  
  21. Next
  22.  
  23. Set objFile = Nothing
  24. Set objFSO = Nothing
  25. Set olkMessage = Nothing
  26. Set olkApp = Nothing
  27. WScript.Quit
  28.  
  29. Function ReplaceIllegalCharacters(strSubject)
  30.     Dim strBuffer
  31.     strBuffer = Replace(strSubject, ":", "")
  32.     strBuffer = Replace(strBuffer, "\", "")
  33.     strBuffer = Replace(strBuffer, "/", "")
  34.     strBuffer = Replace(strBuffer, "?", "")
  35.     strBuffer = Replace(strBuffer, Chr(34), "'")
  36.     strBuffer = Replace(strBuffer, "|", "")
  37.     ReplaceIllegalCharacters = strBuffer
  38. End Function 
  39.  
  40.  
  41. Function ModFileDT(strDir, strFileName, DateTime)
  42.     Dim objShell, objFolder
  43.     Set objShell = CreateObject("Shell.Application")
  44.     Set objFolder = objShell.NameSpace(strDir)
  45.     objFolder.Items.Item(strFileName).ModifyDate = DateTime
  46. End function

Cheers
Strychtur
Jan 18 '08 #5

Expert 5K+
P: 8,434
Ok how about this then? ...
Looks better. But does it work?
Jan 21 '08 #6

Expert 5K+
P: 8,434
One thing I don't quite get. Unless it's just for the purpose of keeping the Sub independent of the caller, why couldn't your Sub just use your existing File object, something like this...
Expand|Select|Wrap|Line Numbers
  1. Sub ModFileDT(DateTime)
  2.   objFile.ModifyDate = DateTime
  3. End Sub
(Note I changed it to a Sub, since it doesn't return anything.)

And given that it's now a single line of code, why use a Sub (or Function) at all? Why not just set the ModifyDate property in your code?
Jan 21 '08 #7

P: 10
One thing I don't quite get. Unless it's just for the purpose of keeping the Sub independent of the caller, why couldn't your Sub just use your existing File object, something like this...
Expand|Select|Wrap|Line Numbers
  1. Sub ModFileDT(DateTime)
  2.   objFile.ModifyDate = DateTime
  3. End Sub
(Note I changed it to a Sub, since it doesn't return anything.)

And given that it's now a single line of code, why use a Sub (or Function) at all? Why not just set the ModifyDate property in your code?
Becuase the the modified date has to be changed using the shell app. Your sub will not work without it. I guess I could add all of it ti the main part.
Jan 21 '08 #8

Expert 5K+
P: 8,434
Becuase the the modified date has to be changed using the shell app. Your sub will not work without it. I guess I could add all of it ti the main part.
Ah, I see. Just checked my details, and of course found that the File object's DateLastModified property is read-only. Bummer.
Jan 21 '08 #9

Expert 5K+
P: 8,434
Your latest version may have a problem in that you're passing the drive rather than the path to the ModifyDT routine.

I do wonder whether the ModifyDate property might be read-only. The documentation doesn't seem to make it clear, one way or the other. But the main thing I'd check is the exact values of strDir, strFileName and DateTime on entry to the Sub.
Jan 21 '08 #10

P: 10
Ok for the most part this works. I say for the most part because if you pass it a bunch of msg file, it will change most but not all.
Expand|Select|Wrap|Line Numbers
  1. '===================
  2. ' VBScript source code 
  3.  
  4.  
  5. Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName
  6.  
  7. Set objFSO = CreateObject("Scripting.FileSystemObject")
  8. On Error Resume Next
  9. Set olkApp = GetObject(,"Outlook.Application")
  10. On Error GoTo 0
  11.  
  12. If TypeName(olkApp) <> "Application" Then
  13.     Set olkApp = CreateObject("Outlook.Application")
  14. End If
  15.  
  16. For Each varFile In WScript.Arguments
  17.       If LCase(objFSO.GetExtensionName(varFile)) = "msg" Then
  18.             Set olkMessage = olkApp.CreateItemFromTemplate(varFile)    
  19.             Set objFile = objFSO.GetFile(varFile)                   
  20.             varNewFileName = ReplaceIllegalCharacters(olkMessage.SenderName & " - " & Replace(objFile.Name,".msg","")) & ".msg"
  21.             objFile.Name = varNewFileName
  22.         Call ModFileDT (objFile.ParentFolder,  objFile.Name, olkMessage.ReceivedTime)
  23.       End If
  24. Next
  25. Set objFile = Nothing
  26. Set objFSO = Nothing
  27. Set olkMessage = Nothing
  28. Set olkApp = Nothing
  29. WScript.Quit
  30.  
  31. Function ReplaceIllegalCharacters(strSubject)
  32.     Dim strBuffer
  33.     strBuffer = Replace(strSubject, ":", "")
  34.     strBuffer = Replace(strBuffer, "\", "")
  35.     strBuffer = Replace(strBuffer, "/", "")
  36.     strBuffer = Replace(strBuffer, "?", "")
  37.     strBuffer = Replace(strBuffer, Chr(34), "'")
  38.     strBuffer = Replace(strBuffer, "|", "")
  39.     ReplaceIllegalCharacters = strBuffer
  40. End Function 
  41.  
  42. Sub ModFileDT(strDir, strFileName, DateTime)
  43.     Dim objShell, objFolder, objFile
  44.     Set objShell = CreateObject("Shell.Application")
  45.     Set objFolder = objShell.NameSpace(CStr(strDir))
  46.     Set objFile =  objFolder.ParseName( CStr(strFileName) ) 
  47.     objFile.ModifyDate= CStr(DateTime)
  48. Set objShell = Nothing
  49. Set objFolder = Nothing
  50. Set objFile = Nothing
  51. End Sub
  52. '===================
I'm not sure how to debug the code. I tried using MS script debugger most I can't step through it.
Jan 23 '08 #11

Expert 5K+
P: 8,434
Good point. I have no idea how one would go about debugging a VB Script. The "some but not all files" problem sounds a but worrying. You'd expect it would either work or produce an error.

Some thoughts on possible bugs...
  • Is it possible your On Error is bypassing a significant error? Can you try running without it to see what happens?
  • Maybe a file was open at the time or something, and couldn't be renamed.
  • When it "fails" does it change only the name, or only the time, or neither? Are you sure it's consistent in this?
Jan 24 '08 #12

Post your reply

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