Copying folfer from one folder to another inside for each using code

Hi I have a folder A inside root folder.And folder A contains sub folders 1 2 3 now I need to move these 1 2 3 folders from A to root folder . Copy folder Activity is failing because of Name OF THE folders . How can I achieve this using codibg in uiapth. I am iterating the loop inside for each folder in folder for folder A

Hi

Use this XAML to move the a folder (or item within a For Each) and all file and subfolders to a new ‘parent’ folder location.
Copy_Folder_to_New_Location.xaml (39.8 KB)

Give it a try.

Cheers

Steve

Hi @shashank_dullu

Try the below code in Invoke Code activity:

' VB.NET code to move subfolders from Folder A to the root folder

Dim subfolders As String() = Directory.GetDirectories(folderA)

For Each subfolder As String In subfolders
    Dim subfolderName As String = Path.GetFileName(subfolder)
    Dim newLocation As String = Path.Combine(rootFolder, subfolderName)

    Try
        Directory.Move(subfolder, newLocation)
        Console.WriteLine("Moved subfolder " & subfolderName & " to " & newLocation)
    Catch ex As Exception
        Console.WriteLine("Failed to move subfolder " & subfolderName)
    End Try
Next

Console.WriteLine("Process completed.")

Invoked arguments:


folderA is the folder path of A folder
rootFolder is in which path you want to move the subfolders.

Hope it helps!!

THis is moving the files not copying the files to new loctaion

Hi @shashank_dullu

Try this:

Dim subfolders As String() = Directory.GetDirectories(folderA)

For Each subfolder As String In subfolders
    Dim subfolderName As String = Path.GetFileName(subfolder)
    Dim newLocation As String = Path.Combine(rootFolder, subfolderName)

    Try
        ' Create a copy of the subfolder in the new location
        Directory.CreateDirectory(newLocation)

        ' Copy all files and subdirectories
        For Each filePath As String In Directory.GetFiles(subfolder, "*", SearchOption.AllDirectories)
            Dim relativePath As String = filePath.Substring(subfolder.Length + 1)
            Dim destinationPath As String = Path.Combine(newLocation, relativePath)
            File.Copy(filePath, destinationPath)
        Next

        Console.WriteLine("Copied subfolder " & subfolderName & " to " & newLocation)
    Catch ex As Exception
        Console.WriteLine("Failed to copy subfolder " & subfolderName)
    End Try
Next

Console.WriteLine("Process completed.")

The arguments are the same.

Hope it helps!!
Cheers!!

Can we connect for this Via zoom if possible. Dynamically its not working

Hi @shashank_dullu,

can you please elaborate this “Hi I have a folder A inside root folder.And folder A contains sub folders 1 2 3 now I need to move these 1 2 3 folders from A to root folder . Copy folder Activity is failing because of Name OF THE folders . How can I achieve this using codibg in uiapth. I am iterating the loop inside for each folder in folder for folder A”. Will try to provide solution for this.

Link:-https://www.youtube.com/channel/UCtx5wWVbY-NuCKclc6v7NOA

Regards,
Techystack.

this is only working only in case of one data not for dynamically data