Wednesday, 23 May 2012

ASP.net Copy Files and Folder from source to destination with creating backup folder.


The manual deployment of files is applicable in most of the enhancement or maintenance project. This method is applicable only for certain applications that are developed before 2002 for classic asp and other programming languages with no class libraries. Still it is a part of activity performed by the deployment team to transfer the modified file from staging folder to the production folder. Normally the developers copy the files from the development environment or UAT environment and put it in a staging environment. And then after approval process the deployment team takes the code from staging and deployment in production.  The whole process is so cumbersome and difficult for the developer to copy the files from the development or UAT to staging. So the below code is so self explanatory for creating the files and folders in the staging environment with a deployment instruction in the staging location.

The Code accepts various parameters like source location, destination location, backup folder location, ticket name.
Create deployment instruction and rollback instruction along with the files and folders to be copied in the staging environment with the same structure.

This code can used as a part in a web application for creating deployment instruction.

Public Shared Sub CreateFileFolder(ByVal srcFullPath As String, ByVal srcPath As String, ByVal dstPath AsString, ByVal fileName As String, ByVal ticketNo As String, ByVal lastFldr As Boolean, ByVal bkpFolder AsString)
        Dim dstDir, backupDir As DirectoryInfo
        Dim tmpPath As String
        ‘Create instr if it is the last folder 
        If lastFldr = True Then
            If File.Exists(DeployInstrPath & "Instr.txt") Then File.Delete(DeployInstrPath & "Instr.txt")
            WriteFile(DeployInstr, DeployInstrPath & "Instr.txt")
            If File.Exists(DeployInstrPath & "RollbackInstr.txt") Then File.Delete(DeployInstrPath &"RollbackInstr.txt")
            WriteFile(RollbackInstr, DeployInstrPath & "RollbackInstr.txt")
            Exit Sub
        End If

        Try
            If (srcPath.Contains("\")) Then
                tmpPath = srcPath.Substring(0, srcPath.IndexOf("\"))
                srcPath = srcPath.Substring(srcPath.IndexOf("\") + 1)
                lastFldr = False
            Else
                tmpPath = srcPath
                lastFldr = True
            End If
            If ticketNo.Length <> 0 Then
                dstPath += "\" + tmpPath + "\" + ticketNo
                backupDir = New DirectoryInfo(dstPath + "\Backup")
              ‘Create backupFolder 
                backupDir.Create()
                bkpFolder = dstPath + "\Backup"
            Else
                dstPath += "\" + tmpPath
                bkpFolder += "\" + tmpPath
              ‘Create folder inside the structure
                backupDir = New DirectoryInfo(bkpFolder)
                If (Not backupDir.Exists) Then backupDir.Create()
            End If
             ‘Create Main Folder
            dstDir = New DirectoryInfo(dstPath)
            If (Not dstDir.Exists) Then dstDir.Create()
            If lastFldr = True Then
                CopyFiles(srcFullPath, dstPath + "\" + fileName)
                WriteDeployInstruction(fileName, srcPath, bkpFolder & "\", dstPath & "\")
                WriteRollbackInstruction(fileName, srcPath, bkpFolder & "\")
            End If
            CreateFileFolder(srcFullPath, srcPath, dstPath, fileName, String.Empty, lastFldr, bkpFolder)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

No comments:

Post a Comment