Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting multiple SSRS reports across subfolders using command line?

I'm new to SSRS deployment, and I've been searching around but can't quite find the exact scenario that applies to my situation. I have several reports deployed across different subfolders in SSRS 2012. For example:

  • Sales/salesReport1
  • Sales/salesReport2
  • Finance/finReport1
  • Finance/finReport2
  • Misc/miscReport1
  • Misc/miscReport2

Due to the fact that sometimes SSRS doesn't like an overwritten report file, I would like to delete all the report files in the instance before deploying any new reports. I can successfully remove the folders and their contents by calling this script using rs.exe:

Public Sub Main()  

  rs.Credentials = System.Net.CredentialCache.DefaultCredentials  

  Dim bh As New BatchHeader()  

  bh.BatchID = rs.CreateBatch()  

  rs.BatchHeaderValue = bh 

   'Delete all reports from Main Folder and sub folders.Note:The Folders will be deleted also.  
   'If do not want to delete the folder, we could use rs.CreateFolder(“Folder name”, “/Main     Folder”, “nothing”) to create a new one with nothing.  

 rs.DeleteItem("/Sales")
 rs.DeleteItem("/Finance")
 rs.DeleteItem("/Misc") 

   'Delete all reports from a sub folder, and delete the sub folder  
   'rs.DeleteItem("/Main Folder/Sub Folder ")  

  rs.BatchHeaderValue = bh       

  ' Delete folders using batch header.  

  Try  

     rs.ExecuteBatch()  
     Console.WriteLine("Folders deleted successfully.")  

  Catch e As SoapException  
     Console.WriteLine(e.Detail.InnerXml.ToString())  

  Finally  
      rs.BatchHeaderValue = Nothing 

  End Try  

End Sub 'Main 

The issue lies in the fact that if any of those folders doesn't exist, the script will stop. I'm trying to determine the best way to handle this. Would the best way be to empty the folders and leave them there, or to find a way to ignore the errors and continue?

Depending on the answer, how would I invoke that in this script?

like image 338
edgesrazor Avatar asked Sep 30 '22 19:09

edgesrazor


1 Answers

You can check if the folder exists by running through the items in the parent folder and looking for any child folders with the expected name. Then, only if this exists, try deleting the folder.

This should solve your issue, i.e. you only delete when you know it exists.

Your code might look something like:

Public Sub Main()

    rs.Credentials = System.Net.CredentialCache.DefaultCredentials

    DeleteFolder("Sales")
    DeleteFolder("Finance")

End Sub

Public Sub DeleteFolder(ByVal folderName As String)

    Dim items() as CatalogItem  
    Dim item As CatalogItem
    Dim folderExists As Boolean = False

    items = rs.ListChildren("/", False)

    For Each item In items

        If item.TypeName = "Folder" And item.Name.Equals(folderName)

            folderExists = True

        End If

    Next

    If folderExists

        Try

            rs.DeleteItem("/" + folderName)
            Console.WriteLine("Deleted folder {0}", folderName)

        Catch e As Exception

            Console.WriteLine(e.Message)

        End Try     

    Else

        Console.WriteLine("Folder {0} does not exist", folderName)

    End If

End Sub

This checks the / folder only - you may have to tweak to match your setup.

like image 192
Ian Preston Avatar answered Oct 03 '22 07:10

Ian Preston