1.

Solve : VBS - Check if a file is older than X days old?

Answer»

I've seen a whole BUNCH of scripts that check through a folder (and possibly sub-folders) for files which are older than X days old, if so, then moves them to a different folder. This is not what I need.
I am after a script which will look at only 1 file, check whether or not it is older than X days old, and if so, create a new file in a temporary directory.

I am QUITE terrible in VBS, but still willing to learn. So if you would take the time to break down the code and explain, that would be greatly appreciated, otherwise, the code is still appreciated.you can take a look at EXAMPLE 1 here.
If you need just 1 file, remove the for loop and change strFolder variable to strFile that contains the path of you file.Quote from: ghostdog74 on June 23, 2010, 09:13:07 AM

you can take a look at example 1 here.
If you need just 1 file, remove the for loop and change strFolder variable to strFile that contains the path of you file.
I tried this, but I get an error. Line 4, Char 1. Object required: '[string: "netpass.txt"]'

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "T:\Documents and Settings\student\Desktop"
Set objFolder = objFS.GetFolder(strFolder)
set strFile = "netpass.txt"
If DateDiff("h",strFile.DateLastModified,Now) < 24 Then
strFileName = strFile.Name
WScript.Echo strFileName
WScript.Echo strFolder&"\"&strFileName
objFS.CopyFile strFolder&"\"&strFileName,"c:\tmp"
End If

Please explain what is wrong.

"netpass.txt" is a string, not an object.


I imagine that line is supposed to be something like

Code: [Select]Set strfile = objFolder.Files("netpass.txt")
Quote from: BC_Programmer on June 23, 2010, 05:29:12 PM
"netpass.txt" is a string, not an object.


I imagine that line is supposed to be something like

Code: [Select]Set strfile = objFolder.Files("netpass.txt")
Thank you! I LOOKED at your other example ghostdog, regarding 30 days instead of hours, I just want to confirm that this code will only do the commands if the file is older than 30 days:

Code: [Select]Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "T:\Documents and Settings\student\Desktop"
Set objFolder = objFS.GetFolder(strFolder)
Set strfile = objFolder.Files("testing.vbs")
If DateDiff("d",strFile.DateLastModified,Now) > 31 Then
strFileName = strFile.Name
WScript.Echo strFileName
WScript.Echo strFolder&"\"&strFileName
msgbox "IT IS OLD!"
'objFS.CopyFile strFolder&"\"&strFileName,"c:\tmp"
End If
the statements in the if block will only be executed if the last modified date of the file is at least 31 days before the current date.Quote from: BC_Programmer on June 23, 2010, 05:55:29 PM
the statements in the if block will only be executed if the last modified date of the file is at least 31 days before the current date.
AHH! Last modified...that would explain it! OK, thank you.to get file properties, you can do this

Code: [Select]Set objFile = objFS.GetFile( "netpass.txt" )
Quote from: ghostdog74 on June 23, 2010, 07:26:41 PM
to get file properties, you can do this

Code: [Select]Set objFile = objFS.GetFile( "netpass.txt" )


You would need a fully qualified path.In NTFS, aren't "last modified" dates/times something that can be turned off, and therefore not be assumed to exist on every system encountered?
Quote from: Salmon Trout on June 26, 2010, 11:35:26 AM
In NTFS, aren't "last modified" dates/times something that can be turned off, and therefore not be assumed to exist on every system encountered?

Would the "average Joe" do that? Why would someone do that anyway?Quote from: Helpmeh on June 26, 2010, 07:04:14 PM
Would the "average Joe" do that? Why would someone do that anyway?

Sorry, I misread the post, I was thinking of the "last accessed" date. Disabling this in NTFS can improve filesystem perfomance.
Quote from: Salmon Trout on June 27, 2010, 12:11:03 AM
Sorry, I misread the post, I was thinking of the "last accessed" date. Disabling this in NTFS can improve filesystem perfomance.

Oh, ok.


Discussion

No Comment Found