UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Vbs To Backup Files And Folders, 2013    
 
   
ry94080
post Feb 26 2020, 01:35 PM
Post#1



Posts: 1,023
Joined: 27-July 05



Hi all,

I'm trying to create a vbscript that will copy ALL Files AND Folders from a certain directory over to another.

I have it working with Files, but not sure how to apply the same logic to Folder. See my script below:

Any ideas?

CODE
Sub PerformBackup()

sFolder = "C:\ProgramData\Tecan\"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each ofile In oFSO.GetFolder(sFolder).files
    sfilefullpath = ofile
    sFile = Mid(ofile, InStrRev(ofile, "\") + 1)
    

            MoveFile sFolder & sFile, "\\10.155.10.55\Vol_1_Bio_Fire\Tecan Biobank Backups\Program Data Tecan\" & sFile
            'KillFile sFolder & sFile

Next


End Sub


Thanks,
Go to the top of the page
 
theDBguy
post Feb 26 2020, 01:39 PM
Post#2


UA Moderator
Posts: 77,725
Joined: 19-June 07
From: SunnySandyEggo


Hi. You could try converting your sub into a recursive routine where it will call itself to process all the files when it hits a folder. Just a thought...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
ry94080
post Feb 26 2020, 02:09 PM
Post#3



Posts: 1,023
Joined: 27-July 05



Not quite sure how to do that.

Example?
Go to the top of the page
 
theDBguy
post Feb 26 2020, 02:32 PM
Post#4


UA Moderator
Posts: 77,725
Joined: 19-June 07
From: SunnySandyEggo


This demo uses recursion. Also, here's a simple function using recursion as well. Just to give you an idea...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
Larry Larsen
post Feb 26 2020, 02:41 PM
Post#5


UA Editor + Utterly Certified
Posts: 24,527
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi

Was playing around with this small code segment only the other day that may help..
CODE
Sub MoveFilesFolder2Folder(FPath As String, TPath As String)
    Dim FSO
    Dim sfol As String, dfol As String

    sfol = FPath    ' change to match the source folder path
    dfol = TPath    ' change to match the destination folder path

    Set FSO = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next

    If Not FSO.FolderExists(sfol) Then
        MsgBox sfol & " is not a valid folder/path.", vbInformation, "Invalid Source"
    ElseIf Not FSO.FolderExists(dfol) Then
        MsgBox dfol & " is not a valid folder/path.", vbInformation, "Invalid Destination"
    Else
         FSO.MoveFile (sfol & "\*.txt"), dfol    ' Change "\*.*" to "\*.xls" to move Excel Files only
    End If

    If Err.Number = 53 Then MsgBox "File not found"
    
End Sub


Use it like:
CODE
Call MoveFilesFolder2Folder("FromDir", "ToDir")


Note: Remember to backup your source folder before testing out the code..

HTH's
thumbup.gif

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
theDBguy
post Feb 26 2020, 03:05 PM
Post#6


UA Moderator
Posts: 77,725
Joined: 19-June 07
From: SunnySandyEggo


Hi. Larry's post made me read your original question again. You said you wanted to "copy" the files to another folder, but your code used "MoveFile" in it. I think Larry's code will also "move" the file from the original folder to the new destination folder. Is that what you really wanted to do? Just curious...

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
Larry Larsen
post Feb 26 2020, 03:17 PM
Post#7


UA Editor + Utterly Certified
Posts: 24,527
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi
Yes sorry I misread it as wanting to move rather than copy.. (just my thoughts were still in moving mode..).. laugh.gif

thumbup.gif

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
Larry Larsen
post Feb 26 2020, 03:35 PM
Post#8


UA Editor + Utterly Certified
Posts: 24,527
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi
Just had another look around and may have found the "Copy" version if it helps..
CODE
Copy ALL files (or of a specific file type) in one folder into another folder

Sub CopyFilesFolder2Folder()

    Dim fso
    Dim sfol As String, dfol As String

    sfol = "c:\MyFolder" ' change to match the source folder path
    dfol = "e:\MyFolder" ' change to match the destination folder path

    Set fso = CreateObject("Scripting.FileSystemObject")

    On Error Resume Next

    If Not fso.FolderExists(sfol) Then

        MsgBox sfol & " is not a valid folder/path.", vbInformation, "Invalid Source"

    ElseIf Not fso.FolderExists(dfol) Then

        MsgBox dfol & " is not a valid folder/path.", vbInformation, "Invalid Destination"

    Else

        fso.CopyFile (sfol & "\*.*"), dfol ' Change "\*.*" to "\*.xls" to move Excel Files only

    End If

    If Err.Number = 53 Then MsgBox "File not found"

End Sub

thumbup.gif

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
fogline
post Feb 27 2020, 07:34 PM
Post#9



Posts: 200
Joined: 5-August 15
From: Ringgold, GA. USA


Here is an app that I built that will Backup a Folder and everything in it
and you can save the Folder anywhere you want.
Hope it is helpful to anyone that can us it.
And it can always be improved so if you do please
re-post it for anybody else that may need it.

Attached File  Backup_Folder.zip ( 837.31K )Number of downloads: 4


PS:
I use this for a Backup in my App
so you really don't need the table in it
so you could take it out and just make the form fields unbound
and it should still work.
It also puts a Date stamp on the Folder when you save it
to the new location. But you can look in the code and take that out if need..



This post has been edited by fogline: Feb 27 2020, 08:19 PM

--------------------
Ray White - Fog Line Software LLC.
Email
Go to the top of the page
 
Larry Larsen
post Feb 28 2020, 06:32 AM
Post#10


UA Editor + Utterly Certified
Posts: 24,527
Joined: 26-August 02
From: Melton Mowbray,Leicestershire (U.K)


Hi

Excellent and simple process, thanks for sharing..
thumbup.gif uarulez2.gif

--------------------
"Time...We have exactly the same number of hours per day that were given to Helen Keller, Pasteur, Michaelangelo, Mother Teresa, Leonardo da Vinci, Thomas Jefferson, and Albert Einstein"
H. Jackson Brown
Go to the top of the page
 
fogline
post Feb 28 2020, 08:10 AM
Post#11



Posts: 200
Joined: 5-August 15
From: Ringgold, GA. USA


yw.gif

--------------------
Ray White - Fog Line Software LLC.
Email
Go to the top of the page
 
ry94080
post Feb 28 2020, 10:53 AM
Post#12



Posts: 1,023
Joined: 27-July 05



Thanks Everyone!


uarulez2.gif
Go to the top of the page
 
ry94080
post Mar 2 2020, 12:28 PM
Post#13



Posts: 1,023
Joined: 27-July 05



Hello,

I was able to edit the script and get the copy to work via task scheduler

However, the script appears to terminate early.

The folder is about 3 Gigs. When I copy it over manually, it works. However, when I use the vbs script, I can see the folders getting copied over. But, it stops early and doesn't copy everything.

Any ideas?
Go to the top of the page
 
ry94080
post Mar 2 2020, 02:40 PM
Post#14



Posts: 1,023
Joined: 27-July 05



The error I'm getting is:


The action can't be completed because the file is open...


Is there a way to ignore warnings like this in vbscript? Something like docmd.setwarnings false?
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    4th April 2020 - 06:33 AM