• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

I need a script to copy a source folder&subfolders to a target and append date to a files that change from initial copy.

I need a VB script that will take a folder named c:\home and all it's subfolders and copy that to d:\home and create the subfolders.

However, the next time and any time thereafter the VB script is run, it only needs to copy files that are new or have changed (looks at the archive bit) and if the file has changed it is not to overwrite the target but it should append the current date to the file name in the target folder.  The customer wants a to retain copies in the target folders of each time the file has changed based on date.  Can this be done and will someone help me :)  I unfortunately can visually understand VB scripting but I underfortunately cannot program.  


0
debbiez
Asked:
debbiez
  • 5
  • 4
1 Solution
 
aikimarkCommented:
don't really need VBScript for this one:
XCOPY src tgt /e /s /a
0
 
debbiezAuthor Commented:
XCOPY isn't going to append the date to changed files before it copies them over.
0
 
debbiezAuthor Commented:
Maybe a Visual Would Help:

C:\Home> test.txt (initial files)
D:\Home> test.txt (initial copy)

Then a change is made to test.txt
C:\Home> test.txt (now would have the archive bit turned on)
Then a script is run at night to copy the changed filed over to target which has BOTH the original version and now the changed:

D:\Home> test.txt ; test042206.txt
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aikimarkCommented:
How about locating the backups in a date-named subdirectory?
Example:
MD D:\HOME\Bkup060422
XCOPY C:\Home\*.* D:\Home\Bkup060422\ /e /s /a

=====================
* This way, the filenames don't change.
* You wouldn't have to use the /e flag if you didn't want empty directories.
* Keeping the file in a target folder doesn't seem like a good idea.  If you don't like the above idea, consider the following:
  + Use Bkup060422_test.txt naming sceme
  + create Bkup060422 subdirectories in the places where you need to make the backups, as opposed to using it as the top-most (target) directory name.

=====================
You can also use a utility like WinZip to make the copy of just the changed files and reset their archived flags.
http://www.winzip.com/prodpagecl.htm
0
 
debbiezAuthor Commented:
Can't do it that way, NTFS security needs to be different on the target folders and I'd have to set the security individually each time.  Not gonna happen.  Customer wants it the way I described originally.
0
 
debbiezAuthor Commented:
Backup Exec which will run after the script, will reset the archive bit.
0
 
aikimarkCommented:
the simplest solution will use FileSystemObjects and recursive procedure calls.  It can be done as you'd like.  I'll see what I can whip up.
0
 
aikimarkCommented:
if you use XCOPY /T /E
you can easily create the target directory structure and greatly simplify the code.

Public Sub ProcessFiles(ByVal parmFrom, ByVal parmTo)
    Dim strFldr 'As String
    Dim strFile 'As String
    Dim lngPosn 'As Long
    Dim lngAttr 'As Long     'VbFileAttribute
    Dim FSO 'As Object
    Dim FLDR_Thing 'As Folder
    Dim FILE_Thing 'As File
    Set FSO = CreateObject("Scripting.FileSystemObject")
    For Each FLDR_Thing In FSO.GetFolder(parmFrom).SubFolders
        ProcessFiles parmFrom & "\" & FLDR_Thing.Name, parmTo & "\" & FLDR_Thing.Name
    Next

    For Each FILE_Thing In FSO.GetFolder(parmFrom).Files
        If (FILE_Thing.Attributes And vbArchive) <> 0 Then
            For lngPosn = Len(strFile) To 1 Step -1
                If Mid(strFile, lngPosn, 1) = "." Then
                    Exit For
                End If
            Next
           
            If Len(FILE_Thing.Type) = 0 Then 'no file type
                FSO.CopyFile parmFrom & "\" & FILE_Thing.Name, parmTo & "\" & FILE_Thing.Name & Format(Date, "yymmdd")
            Else
                If FSO.FolderExists(parmTo) Then
                Else
                    CreatePath parmTo   'not needed if directory tree already exists
                End If
                FSO.CopyFile parmFrom & "\" & FILE_Thing.Name, parmTo & "\" & FSO.GetBaseName(FILE_Thing.Name) & Format(Date, "yymmdd") & "." & FSO.GetExtensionName(FILE_Thing.Name)
            End If
           
            FILE_Thing.Attributes = FILE_Thing.Attributes Xor vbArchive
        End If
    Next
   
    Set FSO = Nothing
End Sub

==================
Public Sub CreatePath(ByVal parmPath As String)
    Dim FSO 'As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")

    If FSO.FolderExists(parmPath) Then Exit Sub
    If FSO.FolderExists(FSO.GetParentFolderName(parmPath)) Then
        FSO.CreateFolder parmPath
    Else
        CreatePath FSO.GetParentFolderName(parmPath)
        FSO.CreateFolder parmPath
    End If
   
    Set FSO = Nothing
End Sub
0
 
debbiezAuthor Commented:
Thanks a bunch and I'll try this out!  Hope it works.  BTW, I already have the initial copy created with subfolders and everything on the target drive.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now