How To Look at Multiple Folders Under Different Locations and Copy Files That Were Created

Hi I have project, where I need to look through multiple folders located under different locations and if files are there with date created within last month, then copy those files to a new folder.

Example:

Folder A - Located at \epic-nas.et1157.epichosted.com\xfr\nonprd\fromODB\Claims\PBClaims\UiPathTestingThree

Folder B - Located at \sbonas01\DRM_REQS\MedAssets\BusinessAnalysis\Automated Reports Epic\WEEKLYEPICFOLLOWUPWQREPORTS

I am using the For Each Activity and in the List of Items I am using Directory.GetFiles(“MyLocation”) and then using the If Condition to Look at New FileInfo(CurrentRemitFiles).LastWriteTime >= Today.Date.AddMonths(-1) - Look at last month

And then Copying those files and moving to new location.

Only issue is how can I reference Multiple directories as I can only reference For Each?

Put the directories into an array of a For Each. Then put your existing For Each inside that For Each. Make sure to name the “item name” variable differently for each For Each, so they don’t interfere with each other. So now you’re looping through an array of paths, and inside that loop you’re looping through the files in the folder.

{“c:\test\dir1”,“c:\test\dir2”,“c:\test\dir3”}

However, usually what I would do is put the folders into a config file as a comma delimited list, and then use Split to turn it into an array in the For Each. Or maybe you’re getting the list of folders from elsewhere in the process…either way you’re getting the list of folders just pass them as an array to the For Each.

By the way, you might want to look at using For Each File in Folder for your interior loop. Just a version of For Each that’s more specific to looping through files than the regular For Each.

So you’d have…

  • For Each (loop through array of folders, set its variable to currentFolder)
    – For Each File in Folder (pass currentFolder to it, and set its variable to currentFile)
    — Here you do your file date etc checks against currentFile and file move

Because you’re using For Each File in Folder, currentFile will be a FileInfo object that will give you direct access to various properties of the file (just type currentFile. and the menu will appear showing you all the properties).

Can you share what you mean by this, I am struggling here as I am not a coder. I tried looking online what you are referring to but having hard time programming this. If you can share how you would code it that would be much appreciated.

For Each through an array of folders. I have hard-coded the folders array but you can create arrays many different ways - just depends on your process.

Note that I changed the Item Name to CurrentFolder.

Then a For Each File in Folder inside the For Each:

Note that I passed CurrentFolder to the “In folder” property of For Each File in Folder, and used an If to determine whether to copy the file.

1 Like

Stupid question but “For Each CurrentFolder” is this same activity as in “For Each Folder in Folder”? I cannot seem to located the For Each CurrentFolder activity and my selection option is bit different than what your screenshot shows.

If same, when I type in this {“\epic-nas.et1157.epichosted.com\xfr\nonprd\fromODB\Claims\PBClaims\UiPathTesting",”\epic-nas.et1157.epichosted.com\xfr\nonprd\fromODB\Claims\PBClaims\UiPathTestingThree"} as my folder paths, I am getting error stating BC3011 Value of type ‘String()’ cannot be converted to ‘String.’ The selected value is incompatible with the property type.

Also, under For Each File in Folder I am getting error for the “In Folder” line, it is stating BC30311: Value of type ‘DirectoryInfo’ cannot be converted to ‘String’. The selected value is incompatible with the property type.

No. It’s a regular For Each and you name the variable CurrentFolder instead of currentText. If you have a For Each activity inside another For Each activity and they both have the same variable name, they’ll step on each other and it won’t work right.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.