3

I'm trying to copy files from a directory where the last modified date is within 24hours of the current date. I'm using a wildcard in the filepath as it changes every day I'm using;

option explicit

dim fileSystem, folder, file
dim path 

path = "d:\x\logs"

Set fileSystem = CreateObject("Scripting.FileSystemObject")
Set folder = fileSystem.GetFolder(path)

for each file in folder.Files    


           If DateDiff("d", file.DateLastModified, Now) < 1 Then


   filesystem.CopyFile "d:\x\logs\apache_access_log-*", "d:\completed logs\"

        WScript.Echo file.Name & " last modified at " & file.DateLastModified
    end if
next

Unfortunately this seems to be copying all files, and not just the recently modified ones. Can anyone point me in the right direction?

many thanks

Martin.

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
Martin North
  • 31
  • 1
  • 1
  • 2

3 Answers3

3

How about:

filesystem.CopyFile "d:\x\logs\" & file.name, "d:\completed logs\"
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
1

Change line to:

filesystem.CopyFile file, "d:\completed logs\" 

You were copying every file in the directory as soon as one file matched your criteria

Jibba
  • 426
  • 4
  • 14
  • The code above will probably cause an error because the file variable is a file object and not the string representing the file's path, which is what the CopyFile method is looking for. VBS is fairly forgiving... so it may work, but my bet is that it causes an error. You can quickly fix this by using the file objects "path" attribute. filesystem.CopyFile file.Path, "d:\completed logs\" – PopeDarren Jul 01 '16 at 21:56
0

It looks like you are copying all files if any file satisfies the DateDiff comparison.

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164