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. - On Error Resume Next
-
-
Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName
-
Set olkApp = GetObject(,"Outlook.Application")
-
If TypeName(olkApp) <> "Application" Then
-
Set olkApp = CreateObject("Outlook.Application")
-
End If
-
Set objFSO = CreateObject("Scripting.FileSystemObject")
-
For Each varFile In WScript.Arguments
-
Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
-
varNewFileName = ReplaceIllegalCharacters(olkMessage.SenderName & "-" & olkMessage.Subject) & ".msg"
-
Set objFile = objFSO.GetFile(varFile)
-
objFile.Name = varNewFileName
-
ModFileDT varNewFileName,olkMessage.ReceivedTime
-
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 (strFileName, DateTime)
-
Dim objShell, objFolder
-
Set objShell = CreateObject("Shell.Application")
-
objFolder.Items.Item(strFileName).ModifyDate = DateTime
-
End Sub
Cheers
Strychtur
11 5651
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.
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?
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.)
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? - ' VBScript source code
-
On Error Resume Next
-
-
Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName, Dir
-
-
Set olkApp = GetObject(,"Outlook.Application")
-
-
If TypeName(olkApp) <> "Application" Then
-
Set olkApp = CreateObject("Outlook.Application")
-
End If
-
-
Set objFSO = CreateObject("Scripting.FileSystemObject")
-
-
For Each varFile In WScript.Arguments
-
Set olkMessage = olkApp.CreateItemFromTemplate(varFile)
-
varNewFileName = ReplaceIllegalCharacters(olkMessage.SenderName & "-" & olkMessage.Subject) & ".msg"
-
Set objFile = objFSO.GetFile(varFile)
-
objFile.Name = varNewFileName
-
Call ModFileDT (objFile.Drive, objFile.Name, olkMessage.ReceivedTime)
-
-
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
-
-
-
Function ModFileDT(strDir, strFileName, DateTime)
-
Dim objShell, objFolder
-
Set objShell = CreateObject("Shell.Application")
-
Set objFolder = objShell.NameSpace(strDir)
-
objFolder.Items.Item(strFileName).ModifyDate = DateTime
-
End function
Cheers
Strychtur
Ok how about this then? ...
Looks better. But does it work?
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... - Sub ModFileDT(DateTime)
-
objFile.ModifyDate = DateTime
-
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?
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... - Sub ModFileDT(DateTime)
-
objFile.ModifyDate = DateTime
-
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.
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.
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.
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. - '===================
-
' 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
-
'===================
I'm not sure how to debug the code. I tried using MS script debugger most I can't step through it.
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?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: pythos |
last post by:
How do you change a file's last-modified date and timestamp in Python?
|
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?
...
|
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....
|
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"....
|
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
| |
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...
|
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...
|
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...
|
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}...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |