Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS to check file date stamp and copy....

Status
Not open for further replies.

tEkHEd

IS-IT--Management
Jan 29, 2003
261
GB
Hi!

Can anyone help me to create a VBS that will:

1: Check to see if a file exists on a remote pc via a UNC
2: Check the file created date to see if it matches TODAYS date
3: If the file was created today, copy it from the source (UNC) to the dest (UNC).
4: Message (net send or email) a user/email address, to inform that a new file has arrived.

This would be run everyday to check if a new dump had been given for the day.

I don't really know any VBS syntax so am floundering in the dark a little here...

Thanks in adv :)
 
oops.. forgot to include the real simple VBS that I am working on:

Code:
Const OverwriteExisting = True

Dim strSrcPth As String
Dim strDestPth As String
Dim strFile As String

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set strSrcPth = "\\servername\share\"
Set strDestPth = "\\servername\share\"
Set strFile = "file name.zip"

If FileExists (strFile) Then
    objFSO.CopyFile "strSrcPth" & "strFile" , "strDestPth", OverwriteExisting
    Else Wscript.Echo "File doesn't exist"
End If

 
Well I guess it helps if I use VBS syntax instead of VBA for a start... :)

I am trying something like this now...

Code:
Const OverwriteExisting = True

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(\\servername\share\file name DDMMYY.zip")

If objFSO.FileExists (objFile) Then
    Wscript.Echo "File Exists, Checking created date..."
    Wscript.Echo objFSO.DateCreated
    objFSO.CopyFile objFile , "C:\temp\tempfile DDMMYY.zip"
    Wscript.Echo "Copied: " & objFile
Else
    Wscript.Echo objFile & "does not exist, exiting"
End If
[code]

the source file name includes the date DDMMYY, so I want to be able to take the system date as a variable to change the source/destination filenames (hence objFSO.DateCreated).

So that I can use something like:

setobjFile = objFSO.GetFile ("\\servername\share\filename" & SYSTEMDATE & ".zip")

but also to include some further checking of the source file to make sure that the copy only goes ahead if the "filename DATE.zip" is equal to SystemDate. (i.e. if the file was created on the day that the script is run, it copies, if not it doesn't)
 
Code:
Const OverwriteExisting = True

Dim strSrcPth As String
Dim strDestPth As String
Dim strFile As String

strSrcPath = "\\servername\share\"
strDestPath = "\\servername\share\"
strFile = "file name.zip"

If Dir(strFile) <> &quot;&quot; Then 'If file exists
    If DateDiff(&quot;d&quot;, FileDateTime(strSrcPath & strFile), Date) = 0 Then 'And date is today
        If OverwriteExisting Then 'And we are overwriting
            FileCopy strSrcPath & strFile, strDestPath & strFile 'Copy the file
        Else
            MsgBox &quot;File exists, not allowed to overwrite&quot;
        End If
    Else
        MsgBox &quot;File exists, but was not created today&quot;
    End If
Else
    MsgBox &quot;File does not exist&quot;
End If
 
Thanks for the pointers Norris68.. this is still in VBA syntax rather than VBs though..

I m getting quite a few errors with this code.. I have adjusted it a little, but am still having probs as I don't seem able to set the variable for the destination path... ;o/

Code:
Const OverwriteExisting = True

Dim objSrcPath
Dim objDestPath
Dim objFile

set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

'set objSrcPath = &quot;\\server\share&quot; ' - [b]the script bombs here with the error &quot;object required&quot; I guess this means that I need to set as objFSO.CmdToSetSrcPath ?? [/b]

set objDestPath = &quot;\\server\share\folder1\folder2\folder3\&quot;
wscript.echo objdestpath ' - [b] And again I think I need an objFSO.SetDestPathCmd syntax here[/b]

set objFile = objFSO.GetFile (&quot;\\server\share\filename.zip&quot;)
wscript.echo objfile ' - [b]this echos the correct path[/b]

If objFSO.FileExists(objFile) <> &quot;&quot; Then 'If file exists
    If DateDiff(&quot;d&quot;, FileDateTime(objSrcPath & objFile), Date) = 0 Then 'And date is today - [b]I get an error here &quot;Type Mismatch: &quot;FileDateTime&quot;&quot; so am unable to test the rest of the script...[/b]
        If OverwriteExisting Then 'And we are overwriting
            FileCopy (objSrcPth & objFile), (objDestPath & objFile) 'Copy the file
        Else
            MsgBox &quot;File exists, not allowed to overwrite&quot;
        End If
    Else
        MsgBox &quot;File exists, but was not created today&quot;
    End If
Else
    MsgBox &quot;File does not exist&quot;
End If

I guess that the errors are cos of VBA syntax??
 
>this is still in VBA syntax rather than VBs

Well, this is the VB5/6 forum...
 
Thanks for that useful info strongm

I posted here as I couldn't find a VBS forum....
 
The search facility at the top of the page defaults to Find a Forum - typing in VBScript gives * TADA *

forum329
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top