Forum Discussion

AJ's avatar
AJ
New Contributor III
3 years ago

Deleting multiple files from public folder

Hi 

I am trying to delete files saved in public folder via business rule.

Below BusinessRule.Extender works fine to delete the files from share folder.

Dim configSettings As AppServerConfigSettings = AppServerConfig.GetSettings(si)

'*** First Delete files from username folder created under Data Management->Export folder****
'------------------------------------------------------------------------------------------------------------

Dim xfolderPath As String = FileShareFolderHelper.GetDataManagementExportUsernameFolderForApp(si,True, configsettings.fileShareRootFolder,si.AppToken.AppName)
'**xfolderPath stores the path for DataManagementExportUsernameFolderForApp**

Dim filesToDelete As String = "TRF_Rules_*.xml" '; // Only delete all files which starts with names TRF_Rules*
Dim fileList = System.IO.Directory.GetFiles(xfolderPath, filesToDelete)
For Each file As String In fileList

'System.Diagnostics.Debug.WriteLine(file + "will be deleted");
System.IO.File.Delete(file)
Next

 

However if I follow similar rules to delete the files from public folder - it does not work .

------------------------------------------------------------------------------------------------------------
'Public folder files to delete

Dim newfolder As String = "TRF_Rules" '<-- This is the destination folder

Dim parentfolder As String = "Documents/Public" '<-- App Folder path where new folder will be added, may be updated

'Get full folder path to test

Dim PublicfolderPath As XFFolderEx = BRApi.FileSystem.GetFolder(si, FileSystemLocation.ApplicationDatabase, parentfolder & "/" & newfolder)

Dim filesToDelete2 As String = "TRF_Rules_*.xml" '<-- update this string with the name of the exported file

Dim targetDir As String = BRApi.FileSystem.GetFolder(si, FileSystemLocation.ApplicationDatabase, parentfolder & "/" & newfolder).XFFolder.FullName

Dim fileToDelete3 As String = filesToDelete2

Dim fileBytes1 As Byte() = File.ReadAllBytes(targetDir & "\" & fileToDelete3)

'brapi.ErrorLog.LogMessage(si, "FilestoDelete2 = " & FilestoDelete2) ' <-- comment/uncomment for testing/logging

Using dbConnApp1 As DBConnInfo = BRApi.Database.CreateApplicationDbConnInfo(si)

'Save file in User's Temp folder in application DB

'(This will be cleaned up automatically be OneStream platform When user's session expires)

Dim dbFileInfo1 As New XFFileInfo(FileSystemLocation.ApplicationDatabase, fileToDelete3, targetDir, XFFileType.Unknown)

dbFileInfo1.ContentFileContainsData = True

dbFileInfo1.ContentFileExtension = dbFileInfo1.Extension

Dim dbFile1 As New XFFile(dbFileInfo1, String.Empty, fileBytes1)

'BRApi.FileSystem.DeleteFile(si, fileBytes1)
'BRApi.FileSystem.DeleteFile(si, PublicfolderPath, filesToDelete2)

End Using

'*****Option -2 - Does not work*****
'Dim fileList2 = System.IO.Directory.GetFiles(targetDir, filesToDelete2)
'For Each file As String In fileList2

'System.Diagnostics.Debug.WriteLine(file + "will be deleted");
'System.IO.File.Delete(file)
'Next
'*****Option -2 - Does not work*****

Both the lines highlighted is not working currently and getting errors

 

 

Is there any way to delete the files in public folder via business rules ?

 

 

  • Hi, 

     

    Below is a code example outlining how you may be able to delete files that exist in a given folder stored on the application database file structure.

     

    'get the folder you wish to delete files from in the application database
    Dim folder As XFFolderEx = BRApi.FileSystem.GetFolder(si, FileSystemLocation.ApplicationDatabase, "Documents/Public/SomeFolderName")
    'retrieve the files from the folder stored on the application database
    Dim fileList As List(Of XFFileInfoEx) = BRApi.FileSystem.GetFilesInFolder(si, FileSystemLocation.ApplicationDatabase, folder.XFFolder.FullName, XFFileType.All, Nothing)
    'loop through the files and delete them from the folder
    For Each varFile As XFFileInfoEx In fileList
    	brapi.FileSystem.DeleteFile(si, FileSystemLocation.ApplicationDatabase, varFile.XFFileInfo.FullName)
    Next

     

    Regards,

     

    Nick Kroppe

  • Hi, 

     

    Below is a code example outlining how you may be able to delete files that exist in a given folder stored on the application database file structure.

     

    'get the folder you wish to delete files from in the application database
    Dim folder As XFFolderEx = BRApi.FileSystem.GetFolder(si, FileSystemLocation.ApplicationDatabase, "Documents/Public/SomeFolderName")
    'retrieve the files from the folder stored on the application database
    Dim fileList As List(Of XFFileInfoEx) = BRApi.FileSystem.GetFilesInFolder(si, FileSystemLocation.ApplicationDatabase, folder.XFFolder.FullName, XFFileType.All, Nothing)
    'loop through the files and delete them from the folder
    For Each varFile As XFFileInfoEx In fileList
    	brapi.FileSystem.DeleteFile(si, FileSystemLocation.ApplicationDatabase, varFile.XFFileInfo.FullName)
    Next

     

    Regards,

     

    Nick Kroppe

  • AJ's avatar
    AJ
    New Contributor III

    Thank you Nick.

    It works now.

    Cheers

    AJ

    • Nikpowar97's avatar
      Nikpowar97
      Contributor

      This should also work.

      For Each deletecsvFile In Directory.GetFiles(<yourpath>,"*.*",SearchOption.TopDirectoryOnly)

               File.Delete(deletecsvFile)
      Next