Copying a file to multiple subfolders in multiple folders

Hello,

I am trying to use uipath to place a file in multiple sub-folders within multiple folder while excluding certain folders. I have managed to get UIpath to a folder that contains 5 folders. Well call them Mainfolder 1, Mainfolder 2, Mainfolder 3, Mainfolder 4, and Mainfolder 5. I want to exclude Mainfolder 1 and 5. Within each of the other Mainfolders are various amounts of subfolders. I need to place this file within each of these subfolders. Any ideas on how to do this?

You have to use copy file activity.

Get the list of all sub directories

var directories = Directory.GetDirectories("your_directory_path");

and use the copy file activity.

https://docs.uipath.com/activities/docs/copy-file

Iterate thru the list and copy the file.

Regards,
Karthik Byggari

3 Likes

To add to @KarthikByggari solution.

To get all subfolders within your parent folder (including subfolder of sub folders) - you can use:

directory.GetDirectories(“your_directory_path”,"*",System.IO.SearchOption.AllDirectories)

In your For Each loop through the directories -
Add an if at the start to see whether folder path is should be ignored or not but doing string comparison.

Hi, welcome to uipath community forum
use for each loop
For each of Directory.getdirectories(“Mainfolderpath”)
Change the for each loop argument to string
Now use of condition
If
Item.contains(“Mainfolder1”) or Item.contains(“Mainfolder5”)

Then

Use continue activity

Else

Use another for each loop
For.each of Directory.getdirectories(item)
Change the for each loop argument property to string

Now inside this for each loop
Use copy file activity
At source property give the target file.path
And at destination property give value as ‘item’

It placed the files within the folders but not in the sub-folders.

Do as i suggested :slight_smile:

directory.GetDirectories(“your_directory_path”,"*",System.IO.SearchOption.AllDirectories)

I tried that but it still placed the files in the Main folders and not the subfolders.

Those solutions helped but I am now running into two issues. First, it is also placing a file in the Main folders not just the sub-folders. Second, it is only placing a file in sub-folders if it has a sub-folder in it.

Hi , that could be because of item is same for 2 loops as those are nested loops
What you can do here is
Pls modify as stated below for my solution in my previous comment
At the 2nd for each loop
Instead of keeping it as item , change it to folder
Just rename from item to folder
For each folder Directory.getdirectories(item)
Now inside body of loop
Use move file with destination property value as ‘folder’

I tried that as well and it caused the same problem.

Just have a for each loop using directory.GetDirectories(“yourFolderPath”,"*",System.IO.SearchOption.AllDirectories)

  • with TypeArgument as Object
    If NOT item.Tostring.contains(“XXX”) ORELSE NOT item.ToString.contains(“YYY”)
    Then if you use Copy File inside body
  • From can be your file you want to copy
  • To can be item.tostring +"\FileName"

This will add as the below:

Main Folder

  • Folder A - File
    • SubFolder A - File
    • SubFolder B - File
      • SubSubFolder A - File
  • Folder B - File
    • SubFolder A - File
    • SubFolder B - File