?
Solved

Moving folders

Posted on 2006-03-22
26
Medium Priority
?
196 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
0
Comment
Question by:mcrmg
  • 12
  • 11
  • 2
  • +1
26 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 16256463
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
 

Author Comment

by:mcrmg
ID: 16256908
We use pop3...thx
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16257362
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16257377
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
 

Author Comment

by:mcrmg
ID: 16260550
I am getting
Line:2
Char:30
Error: Expected ')'

help...thx
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16262725
Please paste back exactly what you have now.
0
 

Author Comment

by:mcrmg
ID: 16262794
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
 
LVL 76

Expert Comment

by:David Lee
ID: 16263164
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
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16263341
*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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 16264614
ping
0
 

Author Comment

by:mcrmg
ID: 16268752
Sorry about that.....

I have the following error :

line 12
char 19
error syntac error....



Any ideas?  thx
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16268784
I had a typo.

This line:

    Dil fil

should be:

    Dim fil



Patrick
0
 

Author Comment

by:mcrmg
ID: 16268905
sorry to bug you...I am still getting the same error.....
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16269010
Ack.

Change:

    Set fso = CreateObject("FileSystemObject")

to:

    Set fso = CreateObject("Scripting.FileSystemObject")


Patrick
0
 

Author Comment

by:mcrmg
ID: 16269133
I am still getting the same error.......a quick question, in VBS, does it support error handler like in VB?  thx
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 800 total points
ID: 16269311
> 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
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16269347
BTW, it wouldn't hurt to stick in:

    On Error GoTo 0

right before End Function.

Patrick
0
 

Author Comment

by:mcrmg
ID: 16269570
Thanks for the help...
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16269801
You're welcome :)
0
 

Author Comment

by:mcrmg
ID: 16269828
It has been working great.....is there a way to check if there is any file inside the source folder first?thx
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16269948
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
 

Author Comment

by:mcrmg
ID: 16270001
Great...thanks a lot...
0
 

Author Comment

by:mcrmg
ID: 16270320
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
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 16270551
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
 

Author Comment

by:mcrmg
ID: 16271331
The files part works great, but the folders part I have permission denied...strange...
0
 

Author Comment

by:mcrmg
ID: 16272002
Maybe there is a better way, but now I copy the folder first, then delete them...thx
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 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