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

Can an exclusions be added to COPY in VBS

I have this script to copy all my FTP folders to an FTP-SERVER-Old folder. The script works fine except I want to add several exclusions to it. Under C:\FTP-SERVER\Sports I have 4 directories I dont want to have copied. Lets just call them DIR-1, DIR-2, Dir-3 and DIR-4. Can an exclusions be added?


dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists("C:\FTP-SERVER\Sports") Then
   filesys.CopyFolder "C:\FTP-SERVER\Sports", "C:\FTP-SERVER-Old\"
End If

0
chobus
Asked:
chobus
  • 3
1 Solution
 
chandru_solCommented:
0
 
bkellyboulderitCommented:
I know not VB, but in xcopy you create an exclusion file and then reference that in the command. The file has the extensions or dirs you want to skip.
Maybe there's something similar in VB. Sorry to interject.... Or, maybe you can use VB to call the windows script.....
0
 
chandru_solCommented:
I will also suggest Robocopy from Microsoft if you want to use it

Robcopy link
http://www.ss64.com/nt/robocopy.html

one more link for vbs script
http://www.tek-tips.com/viewthread.cfm?qid=1402849&page=7
0
 
chandru_solCommented:
Hi,

Try this script which excludes the folders and files......

Option Explicit

'Create needed variables
Dim objNetwork, objFSO, RegExObj, variablesObj
Dim SOURCE, TARGET, FLDR_EXcLUDE, FILE_EXCLUDE
Dim replicate, synchronize, silent, hiddenFiles, hiddenFldrs
Dim sourceObj, subSourceObj, sourceFileObj, sourceFile, newSource
Dim targetObj, subTargetObj, targetFileObj, targetFile, newTarget

Set objNetwork = wscript.CreateObject("wscript.network") 'objNetwork.Username
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set variablesObj = WScript.Arguments


'=======================================================
'=============== USER DEFINED VARIABLES ================
'=======================================================
SOURCE = "c:\scripts"
TARGET = "c:\logs"

'Excluded folders
'- list folder names, comma separated
FLDR_EXcLUDE = "test"

'Excluded file types
'- list file extensions (without periods), comma separated
FILE_EXCLUDE = "exe,txt"

replicate   = true    'Perform replication: true/false
synchronize = true    'Perform synchonization: true/false
silent      = false   'Show dialogs: true false
hiddenFldrs = true    'Process hidden folders: true/false
hiddenFiles = true    'Process hidden files: true/false
'       NOTE: If a folder is missing in the target, the entire
'             folder is copied, even the hidden files
'=======================================================


'Use variables passed from command line if sent
If WScript.Arguments.Count > 0 Then
  replicate = variablesObj(0)
End If
If WScript.Arguments.Count > 1 Then
  synchronize = variablesObj(1)
End If
If WScript.Arguments.Count > 2 Then
  silent = variablesObj(2)
End If


'Set up regular expression object
Set RegExObj = New RegExp
RegExObj.IgnoreCase = True
FILE_EXCLUDE = "," & FILE_EXCLUDE & ","
FLDR_EXCLUDE = "," & FLDR_EXCLUDE & ","

'Verify if source folder exists
If Not (objFSO.FolderExists(SOURCE)) Then
  'Do not replicate, Perform error handling
  If Not ( silent ) Then
    MsgBox "Source Missing"
  End If
'Proceed if replicating is on
ElseIf ( replicate ) Then
    replicateFolders SOURCE, TARGET
End If

'Verify if target folder exists
If Not (objFSO.FolderExists(TARGET)) Then
  'Do not synchronize, Perform error handling
  If Not ( silent ) Then
    MsgBox "Target Missing"
  End If
'Proceed if synching is on
ElseIf ( synchronize ) Then
    syncFolders SOURCE, TARGET
End If

'Cleanup variables
Set objFSO        = nothing
Set objNetwork    = nothing
Set variablesObj  = nothing
Set RegExObj      = nothing
Set sourceObj     = nothing
Set targetObj     = nothing
Set targetFileObj = nothing
Set sourceFileObj = nothing

If Not ( silent ) Then
  MsgBox "Backup Complete!", 0, "Done"
End If

'=======================================================
Sub replicateFolders (currentSource, currentTarget)
'=======================================================
'This procedure will call the procedure replicateFiles
'to process the files in the current directory. Then it
'will check the subfolders in the current directory. If
'a sub directory does not exist in the target it will be
'copied there. If it does exist the procedure will
'perform replicateSource on that directory as well
'=======================================================

  'Check if target folder exists
  If Not (objFSO.FolderExists(currentTarget)) Then

    'Create the target folder and copy the contents of currentSource there
    objFSO.CreateFolder(currentTarget)
    objFSO.CopyFolder currentSource, currentTarget

  Else

    'Create folder object of the currentSource
    Set sourceObj = objFSO.GetFolder(currentSource)

    'Perform file replication for the currentSource
    replicateFiles sourceObj, currentTarget

    'Do recursive processing of sub folders
    For Each subSourceObj in sourceObj.SubFolders
      If Not excluded(subSourceObj.Name,0,FLDR_EXCLUDE) Then
      If (hiddenFldrs) Or (Not subSourceObj.Attributes And 2) Then
        newTarget = TARGET & RIGHT(subSourceObj.Path,(Len(subSourceObj.Path)-Len(SOURCE)))
        replicateFolders subSourceObj.path, newTarget
      End If
      End If
    Next

  End If

'=======================================================
End Sub 'replicateFolders
'=======================================================


'=======================================================
Sub replicateFiles (sourceObj, targetPath)
'=======================================================
'
'=======================================================-

  ' Recurse through all files in the source object
  For Each sourceFileObj in sourceObj.Files

    If Not excluded(sourceFileObj.Name,1,FILE_EXCLUDE) Then
    If (hiddenFiles) Or (Not sourceFileObj.Attributes And 2) Then
 
     targetFile = targetPath & "\" & sourceFileObj.Name

      'Check if file exists in target
      If objFSO.FileExists(targetFile) Then

        'Create file object of target file
        Set targetFileObj = objFSO.GetFile(targetFile)

        'Check if file in source is newer
        If sourceFileObj.DateLastModified > targetFileObj.DateLastModified Then
          'Overwrite existing file in target with newer source file
          sourceFileObj.Copy targetFile, true
        End If

      Else
        'Copy file from source to target
        sourceFileObj.Copy targetFile
      End If

    End If
    End If
  Next

'=======================================================
End Sub 'replicateFiles
'=======================================================


'=======================================================
Sub syncFolders (currentSource, currentTarget)
'=======================================================
'
'=======================================================

  'Check if source folder exists
  If Not (objFSO.FolderExists(currentSource)) Then

    'Delete the current target folder
    objFSO.DeleteFolder currentTarget

  Else

    'Create folder object of the currentTarget
    Set targetObj = objFSO.GetFolder(currentTarget)

    'Perform file synching for the currentTarget
    syncFiles currentSource, targetObj

    'Do recursive processing of sub folders
    For Each subTargetObj in targetObj.SubFolders
      If Not excluded(subTargetObj.Name,0,FLDR_EXCLUDE) Then
      If (hiddenFldrs) Or (Not subTargetObj.Attributes And 2) Then
        newSource = SOURCE & RIGHT(subTargetObj.Path,(Len(subTargetObj.Path)-Len(TARGET)))
        syncFolders newSource, subTargetObj.path
      End If
      End If
    Next

  End If

'=======================================================
End Sub 'syncFolders
'=======================================================


'=======================================================
Sub syncFiles (sourcePath, targetObj)
'=======================================================
'
'=======================================================-

  ' Recurse through all files in the target object
  For Each targetFileObj in targetObj.Files

    'Do not process excluded file types
    If Not excluded(targetFileObj.Name,1,FILE_EXCLUDE) Then

      sourceFile = sourcePath & "\" & targetFileObj.Name

      'Delete target file if does not exist in source
      If Not (objFSO.FileExists(sourceFile)) Then
        targetFileObj.Delete true
      End If

    End If

  Next

'=======================================================
End Sub 'syncFiles
'=======================================================


'=======================================================
Function excluded (itemName,isFile,excludeList)
'=======================================================
' Test if itemName is in excluded list
'=======================================================-
  'If file, get the file extension
  If isFile Then
    itemName = Right(itemName,Len(itemName)-InStrRev(itemName,"."))
  End If

  RegExObj.Pattern = "," & itemName & ","
  excluded = RegExObj.Test(excludeList)
'=======================================================
End Function 'excluded
'=======================================================
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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