使用命令行跨子文件夹删除多个SSRS报告?

时间:2014-09-23 20:27:39

标签: vb.net reporting-services sql-server-2012 rs.exe

我是SSRS部署的新手,我一直在搜索,但无法找到适合我情况的确切方案。我在SSRS 2012中的不同子文件夹中部署了多个报告。例如:

  • 销售/ salesReport1
  • 销售/ salesReport2
  • 金融/ finReport1
  • 金融/ finReport2
  • 杂项/ miscReport1
  • 杂项/ miscReport2

由于SSRS有时候不喜欢覆盖的报告文件,我想在部署任何新报告之前删除实例中的所有报告文件。我可以通过使用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 

问题在于,如果这些文件夹中的任何文件夹不存在,脚本将停止。我试图确定处理这个问题的最佳方法。最好的方法是清空文件夹并将它们留在那里,或找到忽略错误的方法并继续?

根据答案,我将如何在此脚本中调用它?

2 个答案:

答案 0 :(得分:3)

您可以通过运行父文件夹中的项目并查找具有预期名称的任何子文件夹来检查文件夹是否存在。然后,仅当存在时,尝试删除该文件夹。

这可以解决您的问题,即您只有在知道它存在时才会删除。

您的代码可能类似于:

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

仅检查/文件夹 - 您可能需要调整以匹配您的设置。

答案 1 :(得分:0)

我完成了接受支持的答案:

  • 删除不在根目录中的文件夹
  • 仅删除文件夹的内容
  • 支持SSRS 2017
  • 也可以用作rss文件,也可以在rs.exe工具中使用

所以代码应该是:

Dim TargetRSFolder As String = vTARGETRSFOLDER 
Dim TargetRSRoot As String = vTARGETROOT
Dim DeleteContentOnly As Boolean = vDeleteContentOnly 

Public Sub Main()

    rs.Credentials = System.Net.CredentialCache.DefaultCredentials

    DeleteFolder(TargetRSFolder)

End Sub

Public Sub DeleteFolder(ByVal folderName As String)

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

    IF (path = "")
         path = "/"
    END IF


    items = rs.ListChildren(path, False)

    For Each item In items

        If item.Type.ToString() = "Folder" And item.Name.ToLower() = folderName.ToLower()

            folderExists = True

        End If

    Next

    If folderExists

        Try

            IF (DeleteContentOnly = True)   
                    items = rs.ListChildren(TargetRSRoot + "/" + folderName, False)
                     For Each item In items
                          rs.DeleteItem(item.Path)   
                     Next
                     Console.WriteLine("Deleted folder's contetnts : {0}", folderName)
            ELSE
                   rs.DeleteItem(TargetRSRoot + "/" + folderName)
                   Console.WriteLine("Deleted folder :{0}", folderName)
            END IF

        Catch e As Exception

            Console.WriteLine(e.Message)

        End Try     

    Else

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

    End If

End Sub

假设我们在DeleteServerReportFolder.rss文件中具有上述代码,并且在"/bb/cc"文件夹中。

  1. 删除}
  2. 删除cc文件夹的内容:
  

rs.exe -i DeleteServerReportFolder.rss -s http://server2/ReportServer2017 -v vTARGETRSFOLDER = cc -v vTARGETROOT = / bb -v vDeleteContentOnly = true

  1. 要完全删除bb文件夹
  

rs.exe -i DeleteServerReportFolder.rss -s http://server2/ReportServer2017 -v vTARGETRSFOLDER = bb -v vTARGETROOT = -v vDeleteContentOnly = false