• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14567
  • Last Modified:

VBScript to read file modified date

I'm startign with this basic script to read the last modifed date of a file.  I want to take this concept and expand it a bit.  This script gives me the date and time of the last time the file was modified.  IIs there a way to only report the date?  I want to be able to read the date modifed for all pdf and txt files in a specified directory and copying them to a different folder if the last modiefed date is equal to today's date.  
Set objFSO = Createobject ("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\test.txt")
wscript.echo objFile.DateLastModified

Open in new window

0
georgedschneider
Asked:
georgedschneider
  • 7
  • 5
  • 5
  • +1
1 Solution
 
takendownCommented:
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\test.txt")
wscript.echo FormatDateTime(objFile.DateLastModified,2)
0
 
georgedschneiderAuthor Commented:
How can I then read the date for all pdf files in a given directory and if the modified date is equal to today's date copy them to a different directory?
0
 
takendownCommented:
Dim sOriginFolder, sDestinationFolder, sFile, oFSO, oShell
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Wscript.Shell")
sOriginFolder = "c:\tp"
sDestinationFolder = "c:\tp2"


   For Each sFile In oFSO.GetFolder(sOriginFolder).Files
     If Not oFSO.FileExists(sDestinationFolder & "\" & oFSO.GetFileName(sFile)) Then
      wscript.echo FormatDateTime(Now(),2)
      if right(sFile.Name,3)="pdf" AND FormatDateTime(sFile.DateLastModified,2)=FormatDateTime(Now(),2) then
             oFSO.GetFile(sFile).Copy sDestinationFolder & "\" & oFSO.GetFileName(sFile),True
             WScript.Echo "Copying : " & Chr(34) & oFSO.GetFileName(sFile) & Chr(34) & " to " & sDestinationFolder
                 End if
     End If
   Next
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rejoinderCommented:
I know this isn't a VB solution but you can get away with this using XCOPY.
The command line could look like this;
xcopy "*.pdf" "C:\Temp\Destination" /D /E /C /I /Q /H /R /O /X /Y

The /D will copy only those that files that are newer than the destination.  Say for instance you are trying to backup a folder and it contains sub folders.  The above command will capture and recreate the folder system and permissions.   Also, the command line will copy files that are not in the destination folder  this means that if your script failed to run on the weekend, this command line would catch that and pull across the modified files where;
      1. The file is new.
      2. The file is newer than an existing one within the backup folder.
The command line will not overwrite files with the same name that are in separate folders because the folder structure is being maintained where a script such as the one mentioned above is not preserving the structure or ACLS however it is meeting your requirements.
I hope these are reasons enough to stick with xcopy.  Anyway, I'm basing my reasoning on an assumption rather than hard facts but the command should work very well.
0
 
RobSampsonCommented:
Also, this is pure VBScript. takendown's code requires VBA, because VBScript doesn't support the Format function.

Regards,

Rob.
Dim strOriginFolder, strDestinationFolder, objFile, objFSO, objShell 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
strOriginFolder = "c:\temp\temp\test script"
strDestinationFolder = "C:\temp\temp\Test script\Destination"
 
For Each objFile In objFSO.GetFolder(strOriginFolder).Files
	If Not objFSO.FileExists(strDestinationFolder & "\" & objFile.Name) Then
		dteFileDate = CDate(Day(objFile.DateLastModified) & "/" & MonthName(Month(objFile.DateLastModified)) & "/" & Year(objFile.DateLastModified))
		If Right(LCase(objFile.Name), 3) = LCase("pdf") And dteFileDate = Date Then
			objFSO.CopyFile objFile.Path, strDestinationFolder & "\" & objFile.Name, True
			WScript.Echo "Copying: " & VbCrLf & objFile.Path & VbCrLf & "to" & VbCrLf & strDestinationFolder & "\" & objFile.Name
		End If
	End If
Next

Open in new window

0
 
takendownCommented:
Really?  It appears to as far as I can tell.

 http://www.w3schools.com/vbScript/func_formatdatetime.asp

I'm seriously curious...would you mind explaining?
0
 
RobSampsonCommented:
Oh yeah, my mistake....you're using FormatDateTime, not just VBA's Format

I got mixed up there with Format, which allows you to specify a criteria for the output format.

In that case, takendown's code will work, and you can ignore mine.  Sorry.

Regards,

Rob.
0
 
takendownCommented:
No big deal!  You just had me second guessing myself for a bit.  :)
0
 
RobSampsonCommented:
Heh heh, sorry mate.....hey, welcome to EE by the way....

Just FYI, instead of making a call like this:
oFSO.GetFileName(sFile)

through the FileSystemObject, you only need to use
sFile.Name

as you've already the GetFile call, effectively, with
For Each sFile In objFSO.GetFolder(strFolder).Files

Regards,

Rob.
0
 
georgedschneiderAuthor Commented:
I'll give it a shot.
0
 
georgedschneiderAuthor Commented:
What is the Set command used for such as in the following lines:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
0
 
georgedschneiderAuthor Commented:
Am I correct in understanding in that the set command asigns a variable to soemthing we want the varianble to do then?
0
 
RobSampsonCommented:
Kind of...I guess...

You need to use the Set keyword to "create" an object, basically, of a class, that allows you to call the properties and methods of that class.

You don't use Set when you only assign a value that does *not* have extra properties and values.

For example
strMyString = "test string"

will assign a static string value to the variable strMyString

But this
Set objFSO = CreateObject("Scripting.FileSystemObject")

*sets* objFSO as an object, of the "Scripting.FileSystemObject" type, which allows you to call its method such as
objFSO.CreateTextFile

Hope that helps.

Regards,

Rob.
0
 
georgedschneiderAuthor Commented:
I've created the following script which works great the one aspect I'm stugling with is when it comes to  the copy command how can I insert logic for it to do an if exists check for the files in the destination folder?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
strMonth = Right("0" & Month(Date),2)
strDay = Right("0" & Day(Date),2)
strYear = Right(Year(Date),2)
strOriginFolder = "c:\test"
strDestinationFolder = "C:\test2"
If objFSO.FolderExists(strOriginFolder & "\" & year(date) & strmonth & strday) = True Then 
	For Each objFile In objFSO.GetFolder(strOriginFolder).Files 
	dteFileDate = CDate(Month(objFile.DateLastModified) &  "/" & Day(objFile.DateLastModified) & "/" & Year(objFile.DateLastModified)) 
		If objfile.type = "Text Document" and dteFileDate = date Then
		objFSO.CopyFile objFile.Path, strDestinationFolder & "\" & objFile.Name
		end if 
	Next
 
End If

Open in new window

0
 
georgedschneiderAuthor Commented:
If my logic is correct I think this will do.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strMonth = Right("0" & Month(Date),2)
strDay = Right("0" & Day(Date),2)
strYear = Right(Year(Date),2)
strOriginFolder = "c:\test"
strDestinationFolder = "C:\test2"
If objFSO.FolderExists(strOriginFolder & "\" & year(date) & strmonth & strday) = True Then 
	For Each objFile In objFSO.GetFolder(strOriginFolder).Files 
	dteFileDate = CDate(Month(objFile.DateLastModified) &  "/" & Day(objFile.DateLastModified) & "/" & Year(objFile.DateLastModified)) 
		If objfile.type = "Text Document" and dteFileDate = date Then
			If Not objFSO.FileExists(strDestinationFolder & "\" & objFile.Name) Then
			objFSO.CopyFile objFile.Path, strDestinationFolder & "\" & objFile.Name
		End if
			End if 
	Next
 
End If

Open in new window

0
 
RobSampsonCommented:
Hi, yes, this is right.

                  If Not objFSO.FileExists(strDestinationFolder & "\" & objFile.Name) Then
                  objFSO.CopyFile objFile.Path, strDestinationFolder & "\" & objFile.Name
            End if


Regards,

Rob.
0
 
georgedschneiderAuthor Commented:
Thanks for all your help on this.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now