Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VBS - Check if a file is older than X days old  (Read 18200 times)

0 Members and 1 Guest are viewing this topic.

Helpmeh

    Topic Starter


    Guru

  • Roar.
  • Thanked: 123
    • Yes
    • Yes
  • Computer: Specs
  • Experience: Familiar
  • OS: Windows 8
VBS - Check if a file is older than X days old
« on: June 22, 2010, 07:05:38 PM »
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.
Where's MagicSpeed?
Quote from: 'matt'
He's playing a game called IRL. Great graphics, *censored* gameplay.

ghostdog74



    Specialist

    Thanked: 27
    Re: VBS - Check if a file is older than X days old
    « Reply #1 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.

    Helpmeh

      Topic Starter


      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: VBS - Check if a file is older than X days old
    « Reply #2 on: June 23, 2010, 05:10:02 PM »
    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.

    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS - Check if a file is older than X days old
    « Reply #3 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")
    I was trying to dereference Null Pointers before it was cool.

    Helpmeh

      Topic Starter


      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: VBS - Check if a file is older than X days old
    « Reply #4 on: June 23, 2010, 05:46:14 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
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS - Check if a file is older than X days old
    « Reply #5 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.
    I was trying to dereference Null Pointers before it was cool.

    Helpmeh

      Topic Starter


      Guru

    • Roar.
    • Thanked: 123
      • Yes
      • Yes
    • Computer: Specs
    • Experience: Familiar
    • OS: Windows 8
    Re: VBS - Check if a file is older than X days old
    « Reply #6 on: June 23, 2010, 06:02:55 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.
    Where's MagicSpeed?
    Quote from: 'matt'
    He's playing a game called IRL. Great graphics, *censored* gameplay.

    ghostdog74



      Specialist

      Thanked: 27
      Re: VBS - Check if a file is older than X days old
      « Reply #7 on: June 23, 2010, 07:26:41 PM »
      to get file properties, you can do this

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

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: VBS - Check if a file is older than X days old
      « Reply #8 on: June 23, 2010, 07:43:08 PM »
      to get file properties, you can do this

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


      You would need a fully qualified path.
      I was trying to dereference Null Pointers before it was cool.

      Salmon Trout

      • Guest
      Re: VBS - Check if a file is older than X days old
      « Reply #9 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?

      Helpmeh

        Topic Starter


        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: VBS - Check if a file is older than X days old
      « Reply #10 on: June 26, 2010, 07:04:14 PM »
      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?
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      Salmon Trout

      • Guest
      Re: VBS - Check if a file is older than X days old
      « Reply #11 on: June 27, 2010, 12:11:03 AM »
      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.

      Helpmeh

        Topic Starter


        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: VBS - Check if a file is older than X days old
      « Reply #12 on: June 27, 2010, 08:08:08 AM »
      Sorry, I misread the post, I was thinking of the "last accessed" date. Disabling this in NTFS can improve filesystem perfomance.

      Oh, ok.
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.