473,473 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Change modified date on msg file

10 New Member
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
11 5651
Killer42
8,435 Recognized Expert Expert
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
strychtur
10 New Member
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
Killer42
8,435 Recognized Expert Expert
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
strychtur
10 New Member
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
Killer42
8,435 Recognized Expert Expert
Ok how about this then? ...
Looks better. But does it work?
Jan 21 '08 #6
Killer42
8,435 Recognized Expert Expert
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
strychtur
10 New Member
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
Killer42
8,435 Recognized Expert Expert
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
Killer42
8,435 Recognized Expert Expert
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
strychtur
10 New Member
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
Killer42
8,435 Recognized Expert Expert
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

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

Similar topics

1
by: pythos | last post by:
How do you change a file's last-modified date and timestamp in Python?
1
by: Gary Paris | last post by:
When bringing up VS 2003, there is a list of projects on the projects tab which include the name of the project and date modified. Why doesn't the date modified change when I modify the project? ...
7
by: laredotornado | last post by:
Hello, Does anyone know how (or if) I can figure out the last time a web page was last modified if my only access to it is via HTTP (a web browser). Right now, I'm looking at "Page Info" options....
9
by: AP | last post by:
Is there anyway to determine what the modified or create date is for a file? In my example I am uploading a text file using the following to select the filename: GetOpenFileName Lib "COMDLG32.DLL"....
3
by: Cornjerker | last post by:
I see where I can change the date a file is created.... File.SetCreationTime(path) But how can I change the date a file is modified. Thanks, C
6
by: SQLcat | last post by:
I have a VBScript as follows: Dim xmlHTTP : Set xmlHTTP = CreateObject("Microsoft.XMLHTTP") Dim adoStream : Set adoStream = CreateObject("adodb.stream") Const bGetAsAsync = False ' wait for...
102
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem...
3
by: RAMohrmann | last post by:
Greetings, I am attempting to view all files in a directory and if those files have not been modified within the last couple days I will remove them. In order to do this I need to look at the...
3
by: Gretsch | last post by:
Web, html, javascript, Hi, I need to calculate the time since this .htm file was last modified. {which I can then use in a calculation, rather than display, so days&decimals format would be OK}...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.