• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Problem with DateLastModified

Hello forum,

I want to manipulate image files by transferring them to another folder and changing the name. See the code used.

The problem is that on the instruction "ModifiedDate_MyFile MyFile.DateLastModified =" VBA responds with a message "Invalid Procedure".

Could a member of the forum help me to solve this problem.

Thank you in advance for your answers.

Harry

Code:
Sub ChangeFileName()
    Dim MyFile As String, OldPath As String, NewPath As String
    Dim OldName As String, NewName As String
    Dim ModifiedDate_MyFile As Date

    OldPath = "C:\Users\XX\Documents\YY 2015\"
    NewPath = "C:\Users\XX\Documents\YY\2015_03_02\"

    MyFile = Dir(OldPath & "DSC*.jpg")
        Do While Len(MyFile) > 0

        OldName = OldPath & MyFile
        ModifiedDate_MyFile = MyFile.DateLastModified
        NewName = NewPath & " 20150302 " & MyFile
       
        Name OldName As NewName

    Loop

End Sub
 
Hi,

First check this!

ModifiedDate_MyFile = (OldPath & MyFile).DateLastModified

2nd
The DateLastModified is a part of Scripting.FileSystemObject so you need to reference it or do late binding for the same.

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

ModifiedDate_MyFile = fso.GetFile(OldPath & MyFile).DateLastModified


3rd method is like as below without any ref.
ModifiedDate_MyFile = FileDateTime(OldPath & MyFile)


Some ref to you..

https://msdn.microsoft.com/en-us/library/office/gg251660.aspx

http://stackoverflow.com/questions/...st-modified-date-explorer-value-not-cmd-value
 
Hi Harry ,

Try this :
Code:
Sub MoveFiles()
'  ......................................................................
'  Include a reference to :
'  Microsoft Scripting Runtime
'  ......................................................................
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
   
    Dim SourcePath As String, DestPath As String
    Dim strFile As String

'  //Change the path to the source folder, accordingly
    SourcePath = "C:\Users\XX\Documents\YY 2015\"

'  //Change the path to the destination folder, accordingly
    DestPath = "C:\Users\XX\Documents\YY\2015_03_02\"
   
    If Right(SourcePath, 1) <> "\" Then SourcePath = SourcePath & "\"
    If Right(DestPath, 1) <> "\" Then DestPath = DestPath & "\"
   
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(SourcePath)
   
    strFile = Dir(SourcePath & "DSC*.jpg")
    Do While Len(strFile) > 0
      ModifiedDate_MyFile = SourceFolder.Files(strFile).DateLastModified
     
      OldName = SourcePath & strFile
      NewName = DestPath & " 20150302 " & strFile
     
      Name SourcePath & strFile As DestPath & strFile
     
      strFile = Dir
    Loop
End Sub
Narayan
 
Hello the forum,

Hi Deepak Narayan,

First of all, my apologies for this late reply.

Thank you to both of you for your response.

@Deepak

The first solution generates a compiler error

The second solution is good but more explicit in the Narayan’s code

The third solution works properly.

@Narayan

Thank’s for this ready-made solution. It’s exactly what I need.

Have a good day.


GiHem
 
Back
Top