Solved

Vbscript - How to exclude a subdirectory from a log file backup

Posted on 2009-07-10
3
935 Views
Last Modified: 2012-05-07
I have the script below that backs up certain log files once a day. Currently it backs up the following directory in its entirety (c:\Dolphin\FSDev\FSPro\Live Site\). Within this directory there are seven subdirectories one of which i wish to exclude from the backup. Could someone possibly give me some pointers as to how to approach this as i am a vb novice and my google searches have drawn a blank.

Thanks in advance
<job id="main">		
	<script language="VBScript">
		Dim strFilePath
		Dim strdest
		Dim objFSO
		Dim objFileCopy
	    Dim dayToCompare
     '   dayToCompare = CDate("1")	    				    
	
	'MsgBox dayToCompare
		strdest = "\\matrix2\IBELogs\"			
        
        strComputer = "."
        
        'display computer name
        Set WshNetwork = WScript.CreateObject("WScript.Network")
         'WScript.Echo(WshNetwork.ComputerName)       
 
        'To do with windows service
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
        Set objFSO = CreateObject("Scripting.FileSystemObject")
       
        'Get the folder directory 
        Set objFolder = objFSO.GetFolder("C:\Dolphin\FSDev\FSPro\Live Site\DataSourceLogfiles")
 
        'Get the subfolder collection for the directory
        Set colSubfolders = objFolder.Subfolders
            
          Dim dateComparison 
          Dim dateToSaveFileAs
          
		  Dim folderNameArray 
		  Dim currentFolderName
		  Dim newFolderName
          Dim folderDate
          Dim splitValue
          Dim strYear
          Dim strMonth
          Dim strDay
          Dim strHours
          Dim strMinutes
          Dim strSeconds
          Dim strConcat       
          Dim lFileDateAndCurrentDateDiff
       
         Set filesys = CreateObject("Scripting.FileSystemObject") 
                 Set testfile= filesys.CreateTextFile("C:\ArchiveBatchFile\Logs\ArchiveEventLogFile.txt", True) 
testfile.Write "File transfer from server" & " "& WshNetwork.ComputerName & " "& "started at" &" " & Date 
       
       For Each objSubfolder in colSubfolders       
           dateComparison = Date          
           
          'Gets the interval between date created and current dates.
          lFileDateAndCurrentDateDiff = DateDiff("d",objSubfolder.DateCreated, dateComparison)
          
          if lFileDateAndCurrentDateDiff = 1 Then
              'Populate all the date variables requried 
               strYear = Year(objSubfolder.DateCreated)           
               strMonth = Pd(Month(objSubfolder.DateCreated),2)
               strDay = Pd(Day(objSubfolder.DateCreated),2)
               strHours = Pd(Hour(objSubfolder.DateCreated),2)
               strMinutes = Pd(Minute(objSubfolder.DateCreated),2)
               strSeconds = Pd(Second(objSubfolder.DateCreated),2)
                          
               ' character to build string
                 strConcat = "-"       
              'convert and assign Folder Date to a variable for later use.
              'folderDate = FormatDateTime(objSubfolder.DateCreated,2)
              
               folderDate = strYear & strConcat & strMonth & strConcat & strDay & " " &_
               strHours & strConcat & strMinutes & strConcat & strSeconds
              'find and replace / to meet file naming conventions
              folderDate = Replace(folderDate,"/","-")
              
              splitValue = "_"
              'assign the current folder name
              currentFolderName = objSubfolder.Name          
              
              'split the current folder 
              folderNameArray =  Split(currentFolderName, splitValue, -1, 1)
              
              'Insert the created date into the split as we want to ensure that the folders creation
              'date is retained
              newFolderName = folderNameArray(0) & splitValue & folderDate & splitValue & folderNameArray(2)
              
              'Copy to archive destination
               objSubfolder.Copy(strdest & newFolderName)          
  	       objSubfolder.Delete
               	
                testfile.Write  newFolderName & "successfully copied from computer" & " " & WshNetwork.ComputerName & vbCr & vbLf
              
              'WScript.Echo newFolderName & "successfully copied from computer" & " " & WshNetwork.ComputerName          
                               
           end if                     
        Next
 
	   testfile.Write "File transfer from server" & " "& WshNetwork.ComputerName & " "& "finished at at" &" " & Date & vbCr & vbLf
        testfile.Close  
        Function pd(n, totalDigits) 
                if totalDigits > len(n) then 
                    pd = String(totalDigits-len(n),"0") & n 
                else 
                    pd = n 
                end if 
         End Function 
       
	</script>
</job>

Open in new window

0
Comment
Question by:johnswaine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

by:Alan_White
ID: 24824292
You need an IF statement within the For Each loop.  There would be nicer ways of doing this if you had more folders to exlcude but this will work.  I can post a fully modified script if you need but if you put the following line in just after the For Each and then put anotehr End If down just before the Next, it should work.
 

If  objSubfolder.name <> "TheFolderNameYouWantToExclude" Then

Open in new window

0
 
LVL 10

Accepted Solution

by:
Alan_White earned 500 total points
ID: 24840407
Since I've not heard back, here is the full script with my modification included, all you need to do is change the name of the folder you want to exlcude.
<job id="main">		
	<script language="VBScript">
		Dim strFilePath
		Dim strdest
		Dim objFSO
		Dim objFileCopy
	    Dim dayToCompare
     '   dayToCompare = CDate("1")	    				    
	
	'MsgBox dayToCompare
		strdest = "\\matrix2\IBELogs\"			
        
        strComputer = "."
        
        'display computer name
        Set WshNetwork = WScript.CreateObject("WScript.Network")
         'WScript.Echo(WshNetwork.ComputerName)       
 
        'To do with windows service
        Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
        Set objFSO = CreateObject("Scripting.FileSystemObject")
       
        'Get the folder directory 
        Set objFolder = objFSO.GetFolder("C:\Dolphin\FSDev\FSPro\Live Site\DataSourceLogfiles")
 
        'Get the subfolder collection for the directory
        Set colSubfolders = objFolder.Subfolders
            
          Dim dateComparison 
          Dim dateToSaveFileAs
          
		  Dim folderNameArray 
		  Dim currentFolderName
		  Dim newFolderName
          Dim folderDate
          Dim splitValue
          Dim strYear
          Dim strMonth
          Dim strDay
          Dim strHours
          Dim strMinutes
          Dim strSeconds
          Dim strConcat       
          Dim lFileDateAndCurrentDateDiff
       
         Set filesys = CreateObject("Scripting.FileSystemObject") 
                 Set testfile= filesys.CreateTextFile("C:\ArchiveBatchFile\Logs\ArchiveEventLogFile.txt", True) 
testfile.Write "File transfer from server" & " "& WshNetwork.ComputerName & " "& "started at" &" " & Date 
       
       For Each objSubfolder in colSubfolders  
            If  objSubfolder.name <> "ExcludeFolderNameHere" Then
	           dateComparison = Date          
	           
	          'Gets the interval between date created and current dates.
	          lFileDateAndCurrentDateDiff = DateDiff("d",objSubfolder.DateCreated, dateComparison)
	          
	          if lFileDateAndCurrentDateDiff = 1 Then
	              'Populate all the date variables requried 
	               strYear = Year(objSubfolder.DateCreated)           
	               strMonth = Pd(Month(objSubfolder.DateCreated),2)
	               strDay = Pd(Day(objSubfolder.DateCreated),2)
	               strHours = Pd(Hour(objSubfolder.DateCreated),2)
	               strMinutes = Pd(Minute(objSubfolder.DateCreated),2)
	               strSeconds = Pd(Second(objSubfolder.DateCreated),2)
	                          
	               ' character to build string
	                 strConcat = "-"       
	              'convert and assign Folder Date to a variable for later use.
	              'folderDate = FormatDateTime(objSubfolder.DateCreated,2)
	              
	               folderDate = strYear & strConcat & strMonth & strConcat & strDay & " " &_
	               strHours & strConcat & strMinutes & strConcat & strSeconds
	              'find and replace / to meet file naming conventions
	              folderDate = Replace(folderDate,"/","-")
	              
	              splitValue = "_"
	              'assign the current folder name
	              currentFolderName = objSubfolder.Name          
	              
	              'split the current folder 
	              folderNameArray =  Split(currentFolderName, splitValue, -1, 1)
	              
	              'Insert the created date into the split as we want to ensure that the folders creation
	              'date is retained
	              newFolderName = folderNameArray(0) & splitValue & folderDate & splitValue & folderNameArray(2)
	              
	              'Copy to archive destination
	               objSubfolder.Copy(strdest & newFolderName)          
	  	       objSubfolder.Delete
	               	
	                testfile.Write  newFolderName & "successfully copied from computer" & " " & WshNetwork.ComputerName & vbCr & vbLf
	              
	              'WScript.Echo newFolderName & "successfully copied from computer" & " " & WshNetwork.ComputerName          
	                               
	           End If     
	        End If                     
	     Next
 
	   testfile.Write "File transfer from server" & " "& WshNetwork.ComputerName & " "& "finished at at" &" " & Date & vbCr & vbLf
        testfile.Close  
        Function pd(n, totalDigits) 
                if totalDigits > len(n) then 
                    pd = String(totalDigits-len(n),"0") & n 
                else 
                    pd = n 
                end if 
         End Function 
       
	</script>
</job>

Open in new window

0
 

Author Closing Comment

by:johnswaine
ID: 31602105
q
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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