Change modified date on msg file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • strychtur
    New Member
    • Oct 2007
    • 10

    Change modified date on msg file

    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.

    [CODE=vb]On Error Resume Next

    Dim olkApp, olkMessage, objFSO, objFile, varFile, varNewFileName
    Set olkApp = GetObject(,"Out look.Applicatio n")
    If TypeName(olkApp ) <> "Applicatio n" Then
    Set olkApp = CreateObject("O utlook.Applicat ion")
    End If
    Set objFSO = CreateObject("S cripting.FileSy stemObject")
    For Each varFile In WScript.Argumen ts
    Set olkMessage = olkApp.CreateIt emFromTemplate( varFile)
    varNewFileName = ReplaceIllegalC haracters(olkMe ssage.SenderNam e & "-" & olkMessage.Subj ect) & ".msg"
    Set objFile = objFSO.GetFile( varFile)
    objFile.Name = varNewFileName
    ModFileDT varNewFileName, olkMessage.Rece ivedTime
    Next
    Set objFile = Nothing
    Set objFSO = Nothing
    Set olkMessage = Nothing
    Set olkApp = Nothing
    WScript.Quit

    Function ReplaceIllegalC haracters(strSu bject)
    Dim strBuffer
    strBuffer = Replace(strSubj ect, ":", "")
    strBuffer = Replace(strBuff er, "\", "")
    strBuffer = Replace(strBuff er, "/", "")
    strBuffer = Replace(strBuff er, "?", "")
    strBuffer = Replace(strBuff er, Chr(34), "'")
    strBuffer = Replace(strBuff er, "|", "")
    ReplaceIllegalC haracters = strBuffer
    End Function

    Sub ModFileDT (strFileName, DateTime)
    Dim objShell, objFolder
    Set objShell = CreateObject("S hell.Applicatio n")
    objFolder.Items .Item(strFileNa me).ModifyDate = DateTime
    End Sub[/CODE]

    Cheers
    Strychtur
    Last edited by Killer42; Jan 17 '08, 03:00 AM.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    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.

    Comment

    • strychtur
      New Member
      • Oct 2007
      • 10

      #3
      Originally posted by Killer42
      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?

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by strychtur
        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.)

        Comment

        • strychtur
          New Member
          • Oct 2007
          • 10

          #5
          Originally posted by Killer42
          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?
          Code:
          ' 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

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by strychtur
            Ok how about this then? ...
            Looks better. But does it work?

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              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...
              [CODE=vb]Sub ModFileDT(DateT ime)
              objFile.ModifyD ate = DateTime
              End Sub[/CODE](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?

              Comment

              • strychtur
                New Member
                • Oct 2007
                • 10

                #8
                Originally posted by Killer42
                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...
                [CODE=vb]Sub ModFileDT(DateT ime)
                objFile.ModifyD ate = DateTime
                End Sub[/CODE](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.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by strychtur
                  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 DateLastModifie d property is read-only. Bummer.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    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.

                    Comment

                    • strychtur
                      New Member
                      • Oct 2007
                      • 10

                      #11
                      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.
                      [CODE=vb]'============== =====
                      ' VBScript source code


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

                      Set objFSO = CreateObject("S cripting.FileSy stemObject")
                      On Error Resume Next
                      Set olkApp = GetObject(,"Out look.Applicatio n")
                      On Error GoTo 0

                      If TypeName(olkApp ) <> "Applicatio n" Then
                      Set olkApp = CreateObject("O utlook.Applicat ion")
                      End If

                      For Each varFile In WScript.Argumen ts
                      If LCase(objFSO.Ge tExtensionName( varFile)) = "msg" Then
                      Set olkMessage = olkApp.CreateIt emFromTemplate( varFile)
                      Set objFile = objFSO.GetFile( varFile)
                      varNewFileName = ReplaceIllegalC haracters(olkMe ssage.SenderNam e & " - " & Replace(objFile .Name,".msg","" )) & ".msg"
                      objFile.Name = varNewFileName
                      Call ModFileDT (objFile.Parent Folder, objFile.Name, olkMessage.Rece ivedTime)
                      End If
                      Next
                      Set objFile = Nothing
                      Set objFSO = Nothing
                      Set olkMessage = Nothing
                      Set olkApp = Nothing
                      WScript.Quit

                      Function ReplaceIllegalC haracters(strSu bject)
                      Dim strBuffer
                      strBuffer = Replace(strSubj ect, ":", "")
                      strBuffer = Replace(strBuff er, "\", "")
                      strBuffer = Replace(strBuff er, "/", "")
                      strBuffer = Replace(strBuff er, "?", "")
                      strBuffer = Replace(strBuff er, Chr(34), "'")
                      strBuffer = Replace(strBuff er, "|", "")
                      ReplaceIllegalC haracters = strBuffer
                      End Function

                      Sub ModFileDT(strDi r, strFileName, DateTime)
                      Dim objShell, objFolder, objFile
                      Set objShell = CreateObject("S hell.Applicatio n")
                      Set objFolder = objShell.NameSp ace(CStr(strDir ))
                      Set objFile = objFolder.Parse Name( CStr(strFileNam e) )
                      objFile.ModifyD ate= CStr(DateTime)
                      Set objShell = Nothing
                      Set objFolder = Nothing
                      Set objFile = Nothing
                      End Sub
                      '============== =====[/CODE]
                      I'm not sure how to debug the code. I tried using MS script debugger most I can't step through it.
                      Last edited by Killer42; Jan 24 '08, 01:20 AM.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        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?

                        Comment

                        Working...