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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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!
mcrmgAuthor Commented:
We use pop3...thx
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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
mcrmgAuthor Commented:
I am getting
Line:2
Char:30
Error: Expected ')'

help...thx
Patrick MatthewsCommented:
Please paste back exactly what you have now.
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
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".
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
JackOfPHCommented:
ping
mcrmgAuthor Commented:
Sorry about that.....

I have the following error :

line 12
char 19
error syntac error....



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

This line:

    Dil fil

should be:

    Dim fil



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

Change:

    Set fso = CreateObject("FileSystemObject")

to:

    Set fso = CreateObject("Scripting.FileSystemObject")


Patrick
mcrmgAuthor Commented:
I am still getting the same error.......a quick question, in VBS, does it support error handler like in VB?  thx
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
BTW, it wouldn't hurt to stick in:

    On Error GoTo 0

right before End Function.

Patrick
mcrmgAuthor Commented:
Thanks for the help...
Patrick MatthewsCommented:
You're welcome :)
mcrmgAuthor Commented:
It has been working great.....is there a way to check if there is any file inside the source folder first?thx
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
mcrmgAuthor Commented:
Great...thanks a lot...
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
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
mcrmgAuthor Commented:
The files part works great, but the folders part I have permission denied...strange...
mcrmgAuthor Commented:
Maybe there is a better way, but now I copy the folder first, then delete them...thx
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.