Moving folders

Hi,

I would like to schedule a job that does the following:

1. Move the contents of one folder to another
2. Overwrite it if existed in the dest folder
3. send out an email if it is done; send out an email if failed (for whatever reson, such as the connection between two servers is dropped)

(DEST and SOURCE folder are on two different servers)
is this possible?

thx
mcrmgAsked:
Who is Participating?
 
Patrick MatthewsCommented:
> a quick question, in VBS, does it support error handler like in VB?

Great question.  As it turns out...no.  VBScript supports On Error Resume Next and On Error GoTo 0,
but not On Error GoTo SomeErrorHandler.

So, incorporating the miscellaneous fixes, and taking out the error handler...






Function MoveTheFiles(Source, Dest)

    ' Source and Dest are UNC paths to source folder and destination folder, terminated
    ' with backslash character (e.g.     \\server\share\folder\subfolder\)

    Dim fso
    Dim SourceFld
    Dim fil

    On Error Resume Next

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set SourceFld = fso.GetFolder(Source)

    For Each fil In SourceFld.Files
        If fso.FileExists(Dest & fil.Name) Then fso.DeleteFile Dest & fil.Name, True
        fil.Move Dest & fil.Name
        If Err <> 0 Then
            MoveTheFiles = False
            Exit For
        Else
            MoveTheFiles = True
        End If
    Next

    Set fil = Nothing
    Set SourceFld = Nothing
    Set fso = Nothing

End Function



Patrick
0
 
David LeeCommented:
Greetings, mcrmg.

Yes, it is possible.  Sending the email is the slightly tricky part.  What email capability do you have from the comptuer the script would run on?

Cheers!
0
 
mcrmgAuthor Commented:
We use pop3...thx
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Patrick MatthewsCommented:
This takes care of the file transfer:


Function MoveTheFiles(Source As String, Dest As String) As Boolean

    ' Source and Dest are UNC paths to source folder and destination folder, terminated
    ' with backslash character (e.g.     \\server\share\folder\subfolder\

    Dim fso As Object
    Dim SourceFld As Object
    Dil fil As Object

    On Error GoTo ErrHandler

    Set fso = CreateObject("FileSystemObject")
    Set SourceFld = fso.GetFolder(Source)

    For Each fil In SourceFld.Files
        If fso.FileExists(Dest & fil.Name) Then fso.DeleteFile Dest & fil.Name, True
        fil.Move Dest & fil.Name
    Next

    MoveTheFiles = True

    GoTo Cleanup

ErrHandler:
    MoveTheFiles = False

Cleanup:
    Set fil = Nothing
    Set SourceFld = Nothing
    Set fso = Nothing

End Function



Patrick
0
 
Patrick MatthewsCommented:
I am thinking that you will have a 'master' sub; that master sub will call the function above to effect
the transfer.  The return value of the function tells you whether the operation succeeded (True) or
failed (False), and you can pass that info on to whatever processes sends the notification email.

Patrick
0
 
mcrmgAuthor Commented:
I am getting
Line:2
Char:30
Error: Expected ')'

help...thx
0
 
Patrick MatthewsCommented:
Please paste back exactly what you have now.
0
 
mcrmgAuthor Commented:
actually, I just copied and pasted into a vbs file and save it, the error appears when I double clicks on the file...thx
0
 
David LeeCommented:
If you're running it from VBScript, then you have to take out all the type references.  In other words, remove all the "As Object", "As String", and "As Boolean".
0
 
Patrick MatthewsCommented:
*Please* tell us when this is VBScript, and not VB.  The syntax is different!



Function MoveTheFiles(Source, Dest)

    ' Source and Dest are UNC paths to source folder and destination folder, terminated
    ' with backslash character (e.g.     \\server\share\folder\subfolder\

    Dim fso
    Dim SourceFld
    Dil fil

    On Error GoTo ErrHandler

    Set fso = CreateObject("FileSystemObject")
    Set SourceFld = fso.GetFolder(Source)

    For Each fil In SourceFld.Files
        If fso.FileExists(Dest & fil.Name) Then fso.DeleteFile Dest & fil.Name, True
        fil.Move Dest & fil.Name
    Next

    MoveTheFiles = True

    GoTo Cleanup

ErrHandler:
    MoveTheFiles = False

Cleanup:
    Set fil = Nothing
    Set SourceFld = Nothing
    Set fso = Nothing

End Function
0
 
JackOfPHCommented:
ping
0
 
mcrmgAuthor Commented:
Sorry about that.....

I have the following error :

line 12
char 19
error syntac error....



Any ideas?  thx
0
 
Patrick MatthewsCommented:
I had a typo.

This line:

    Dil fil

should be:

    Dim fil



Patrick
0
 
mcrmgAuthor Commented:
sorry to bug you...I am still getting the same error.....
0
 
Patrick MatthewsCommented:
Ack.

Change:

    Set fso = CreateObject("FileSystemObject")

to:

    Set fso = CreateObject("Scripting.FileSystemObject")


Patrick
0
 
mcrmgAuthor Commented:
I am still getting the same error.......a quick question, in VBS, does it support error handler like in VB?  thx
0
 
Patrick MatthewsCommented:
BTW, it wouldn't hurt to stick in:

    On Error GoTo 0

right before End Function.

Patrick
0
 
mcrmgAuthor Commented:
Thanks for the help...
0
 
Patrick MatthewsCommented:
You're welcome :)
0
 
mcrmgAuthor Commented:
It has been working great.....is there a way to check if there is any file inside the source folder first?thx
0
 
Patrick MatthewsCommented:
Hm.  You could try this:

Function MoveTheFiles(Source, Dest)

    ' Source and Dest are UNC paths to source folder and destination folder, terminated
    ' with backslash character (e.g.     \\server\share\folder\subfolder\)

    Dim fso
    Dim SourceFld
    Dim fil

    On Error Resume Next

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set SourceFld = fso.GetFolder(Source)

    If SourceFld.Files.Count > 0 Then
        For Each fil In SourceFld.Files
            If fso.FileExists(Dest & fil.Name) Then fso.DeleteFile Dest & fil.Name, True
            fil.Move Dest & fil.Name
            If Err <> 0 Then
                MoveTheFiles = False
                Exit For
            Else
                MoveTheFiles = True
            End If
        Next
    Else
        MoveTheFiles = False
    End If

    Set fil = Nothing
    Set SourceFld = Nothing
    Set fso = Nothing

End Function
0
 
mcrmgAuthor Commented:
Great...thanks a lot...
0
 
mcrmgAuthor Commented:
uhh......I found out another problem....the script seems to copt files, is there a way to make it copy everything, folders, too?  thx
0
 
Patrick MatthewsCommented:
Function MoveTheFiles(Source, Dest)

    ' Source and Dest are UNC paths to source folder and destination folder, terminated
    ' with backslash character (e.g.     \\server\share\folder\subfolder\)

    Dim fso
    Dim SourceFld
    Dim fil
    Dim sf
   
    MoveTheFiles = False
   
    On Error Resume Next

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set SourceFld = fso.GetFolder(Source)

    For Each fil In SourceFld.Files
        If fso.FileExists(Dest & fil.Name) Then fso.DeleteFile Dest & fil.Name, True
        fil.Move Dest & fil.Name
        If Err <> 0 Then
            MoveTheFiles = False
            Exit For
        Else
            MoveTheFiles = True
        End If
    Next

    If MoveTheFiles Then
        For Each sf In SourceFld.SubFolders
            If fso.FolderExists(Dest & sf.Name) Then fso.DeleteFolder Dest & sf.Name, True
            sf.Move Dest & sf.Name
            If Err <> 0 Then
                MoveTheFiles = False
                Exit For
            End If
        Next
    End If
   
    Set sf = Nothing
    Set fil = Nothing
    Set SourceFld = Nothing
    Set fso = Nothing

End Function
0
 
mcrmgAuthor Commented:
The files part works great, but the folders part I have permission denied...strange...
0
 
mcrmgAuthor Commented:
Maybe there is a better way, but now I copy the folder first, then delete them...thx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.