How to get the latest backup files from different subfolders

  • Hi,

    I want to get latest backup files from different subdiretories and copy them to different folder. I am having a hard time in traaversing to different folder and getting latest backup filr and assign that to a variable.

    I have dir/dir1

    1. Gbackup1_20100331.bak

    2. Gbackup_20100401.bak

    Dir/ dir2

    1. GGbackup1_20100331.bak

    2. GGbackup_20100401.bak

    I wrote this in script task:

    Public Class ScriptMain

    Public Sub Main()

    'Dim RootDirectory As String

    Dim newestFile As String

    Dim fileDateSaved As Integer = 0 'initialize variable

    Dim fileDate As Integer

    Dim SubDi As System.IO.DirectoryInfo

    Dim rootDi As System.IO.DirectoryInfo

    'Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)

    'For Each subdirectory As String In subdirectoryEntries

    ' 'ProcessDirectory(subdirectory)

    'Next

    rootDi = New System.IO.DirectoryInfo("D:\dir/dir1\")

    For Each SubDirectory As DirectoryInfo In rootDi.GetDirectories

    For Each fi As FileInfo In SubDirectory.GetFiles("*.bak")

    fileDate = CInt(Right(Replace(fi.Name, Right(fi.Name, 8), ""), 8)) 'set to current file date in loop

    If fileDate > fileDateSaved Then

    fileDateSaved = fileDate

    newestFile = fi.Name 'set variable to fName loop variable

    'newestFile = "D:\dir\dir1\" + fi.Name 'set variable to fName loop variable

    Dts.Variables("newestFile").Value = fi.Name.ToString

    End If

    Next

    'newestFile = "D:\dir\dir1\" + fi.Name - this one gets one latest file, but I have hard code the sunfolder name. How do I get the second sub folder name?

    But this getting only one backupfile and assigning this to filesystemtask.

    Any suggestions will be appreciated.

    Thanks,

    VG

  • Does your code reside in Foreach Loop Container?

    Create Package Level Variables that capture the Newest File for each directory. THen compare the two variables and store the Folder & File Name in Package Level variables.

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply