Solved

VBScript question

Posted on 2006-07-10
9
383 Views
Last Modified: 2012-06-27
Can anyone assist with the below script? I am trying to modify the script so that it will move files from one location to another, however I want to check the files modified date and verify that it has not been changed in 30 minutes so that I do not move a file that is not completely updated.

dim fso as new FileSystemObject  
set fso = new FileSystemObject
fso.CopyFolder "C:\Transferred\GA070706\*", "C:\Transferred\Test\"
set fso = nothing


0
Comment
Question by:ITHCI
  • 4
  • 4
9 Comments
 
LVL 5

Assisted Solution

by:Pi7
Pi7 earned 25 total points
ID: 17074534
you must first test to know if the file has been modified in the last 30 min
to do this use the script

Dim DateCreated As Date
Dim DateModified as Date
Dim LastModified As Integer

DateCreated=fso.GetFile(File).DateCreated
DateModified=fso.GetFile(File).DateLastModified
LastModified=DateDiff("n",DateCreated,DateModified)

If LasModified<=30 Then
      ........'do not copy file
Else
     ........'add you code to copy file
End If

note file is the full path of the file to be copied
0
 
LVL 13

Expert Comment

by:jmundsack
ID: 17074543
   Dim fso As FileSystemObject
    Dim fld As Folder
    Dim fil As File
    Set fso = New FileSystemObject
    Set fld = fso.GetFolder("C:\Transferred\GA070706")
    For Each fil In fld
        If fil.DateLastModified < DateAdd("n", -30, Now) Then
            fil.Copy "C:\Transferred\Test", True
        End If
    Next fil
    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing
0
 

Author Comment

by:ITHCI
ID: 17074622
I have the below in a .vbs file and it comes back with the error line 1 char 9 expected end of statement.


Dim fso As FileSystemObject
Dim fld As Folder
Dim fil As File
Set fso = New FileSystemObject
Set fld = fso.GetFolder("C:\Transferred\GA070706")
For Each fil In fld
If fil.DateLastModified < DateAdd("n", -30, Now) Then
fil.Copy "C:\Transferred\Test", True
End If
Next fil
Set fil = Nothing
Set fld = Nothing
Set fso = Nothing
0
 
LVL 13

Expert Comment

by:jmundsack
ID: 17074816
If you're running this as a script you cannot use explicit object types, as VBScript is untyped--every variable is implicitly a Variant.  Revise as follows:

    Dim fso
    Dim fld
    Dim fil
    Set fso = WScript.CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder("C:\Transferred\GA070706")
    For Each fil In fld
        If fil.DateLastModified < DateAdd("n", -30, Now) Then
            fil.Copy "C:\Transferred\Test", True
        End If
    Next
    Set fil = Nothing
    Set fld = Nothing
    Set fso = Nothing
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ITHCI
ID: 17074865
Changed as shown and then I get Line 6 Char 5
error: Object doesnt support this property or method.
Am I doing something else wrong, I am trying to run it as a .vbs file from my desktop.
0
 
LVL 13

Expert Comment

by:jmundsack
ID: 17075005
My mistake...

    For Each fil In fld

...should be:

    For Each fil In fld.Files

0
 

Author Comment

by:ITHCI
ID: 17075095
sorry to be a pain, but one more issue. when I run the file I now get Line 8 Char 13 Error Permission denied. this seems like a directory permission issue but I set it to everyone full so not sure.
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 475 total points
ID: 17075180
I'm very sorry--I feel like a bit of an a** for not testing this code (leave it to us cocky programmers).  Anyway, the problem is you need to specify the full file path in the copy, as follows:

            fil.Copy "C:\Transferred\Test\" & fil.Name, True

0
 

Author Comment

by:ITHCI
ID: 17075280
Thanks works like a charm.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now