我是SSRS部署的新手,我一直在搜索,但无法找到适合我情况的确切方案。我在SSRS 2012中的不同子文件夹中部署了多个报告。例如:
由于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
问题在于,如果这些文件夹中的任何文件夹不存在,脚本将停止。我试图确定处理这个问题的最佳方法。最好的方法是清空文件夹并将它们留在那里,或找到忽略错误的方法并继续?
根据答案,我将如何在此脚本中调用它?
答案 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)
我完成了接受支持的答案:
所以代码应该是:
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"
文件夹中。
cc
文件夹的内容:
rs.exe -i DeleteServerReportFolder.rss -s http://server2/ReportServer2017 -v vTARGETRSFOLDER = cc -v vTARGETROOT = / bb -v vDeleteContentOnly = true
bb
文件夹:rs.exe -i DeleteServerReportFolder.rss -s http://server2/ReportServer2017 -v vTARGETRSFOLDER = bb -v vTARGETROOT = -v vDeleteContentOnly = false