We help IT Professionals succeed at work.

Moving folders

mcrmg
mcrmg asked
on
Medium Priority
207 Views
Last Modified: 2010-05-01
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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2010

Commented:
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!

Author

Commented:
We use pop3...thx
CERTIFIED EXPERT
Top Expert 2010

Commented:
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
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

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

help...thx
CERTIFIED EXPERT
Top Expert 2010

Commented:
Please paste back exactly what you have now.

Author

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
CERTIFIED EXPERT
Top Expert 2010

Commented:
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".
CERTIFIED EXPERT
Top Expert 2010

Commented:
*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

Commented:
ping

Author

Commented:
Sorry about that.....

I have the following error :

line 12
char 19
error syntac error....



Any ideas?  thx
CERTIFIED EXPERT
Top Expert 2010

Commented:
I had a typo.

This line:

    Dil fil

should be:

    Dim fil



Patrick

Author

Commented:
sorry to bug you...I am still getting the same error.....
CERTIFIED EXPERT
Top Expert 2010

Commented:
Ack.

Change:

    Set fso = CreateObject("FileSystemObject")

to:

    Set fso = CreateObject("Scripting.FileSystemObject")


Patrick

Author

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2010

Commented:
BTW, it wouldn't hurt to stick in:

    On Error GoTo 0

right before End Function.

Patrick

Author

Commented:
Thanks for the help...
CERTIFIED EXPERT
Top Expert 2010

Commented:
You're welcome :)

Author

Commented:
It has been working great.....is there a way to check if there is any file inside the source folder first?thx
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

Commented:
Great...thanks a lot...

Author

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
CERTIFIED EXPERT
Top Expert 2010

Commented:
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

Author

Commented:
The files part works great, but the folders part I have permission denied...strange...

Author

Commented:
Maybe there is a better way, but now I copy the folder first, then delete them...thx
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.