Lina AL
Lina AL

Reputation: 35

How to modify code in Blue Prism to Move folders with all they have inside

I have got an issue where it would be super convenient to move whole folders instead of files.

I myself have 0 code writing experience with Blue Prism but went through the code to see if I can modify it to suit my needs.

When I run this code, I get this error message:

Second path fragment must not be a drive or UNC name.

Would anyone be able to have a look and advice on the mistakes I have made? Please bear in mind that it's a novice's work. Thank you in advance.

Inputs: Folder Path, Destination

Try

Dim sSourceFolder As String = Folder_Path

Dim sDestinationFolder As String
If Directory.Exists(Destination) Then
    sDestinationFolder = Destination
    If Not sDestinationFolder.EndsWith("\") Then
        sDestinationFolder &= "\"
    End If
    sDestinationFolder = ""
Else
    sDestinationFolder = ""
    sDestinationFolder = Destination
End If

Dim objDirectoryInfo As DirectoryInfo = New DirectoryInfo(sSourceFolder)
Dim aFolders As DirectoryInfo() = objDirectoryInfo.GetDirectories(sSourceFolder)

For Each oFolder As DirectoryInfo In aFolders
    If sDestinationFolder = "" Then
        oFolder.MoveTo(sDestinationFolder)
    Else
        oFolder.MoveTo(sDestinationFolder)
    End If
Next

Success = True
Message = ""

Catch e As Exception
    Success = False
    Message = e.Message
End Try

Upvotes: 0

Views: 1792

Answers (1)

Dallan
Dallan

Reputation: 516

My solution creates a new folder in the destination with the same name as the source folder, copies its contents, and deletes the source folder (essentailly the same thing as moving it). The inputs remain Folder_Path and Destination:

Success = True
Message = "" 

Try
    If Not Folder_Path.EndsWith("\") Then
        Folder_Path &= "\"
    End If

    Dim newDirectory As String = System.IO.Path.Combine(Destination, Path.GetFileName(Path.GetDirectoryName(Folder_Path)))
    If Not (Directory.Exists(newDirectory)) Then
        Directory.CreateDirectory(newDirectory)
    End If
    Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(Folder_Path, newDirectory)
    System.IO.Directory.Delete(Folder_Path, True)

Catch e As Exception
    Success = False
    Message = e.Message
End Try

Make sure that you include the System.IO namespace in the Code Options of your object. The Code Options can be found by double-clinking the description box on the Initialize Page/Action and choosing the appropriate tab.

Upvotes: 2

Related Questions