# Moving files from a one directory to multiple directories based on excel sheet

I am trying to move some files from one directory to multiple directories. Filenames are in an excel sheet. Hence I need to pick file names from excel one by one, check them in directory and then copy them to a particular location/locations. Bot gets the destination file path/paths from excel.

What have you gotten so far? Usually the procedure is to use Read Range to read the Excel sheet to a datatable, use For Each Row to loop through the datatable and then use the Move File activity to move it.

Hi @tilarapiyush welcome to forum

Check this workflow
FileMoving.zip (101.2 KB)

mark it as solution if you got it

Regards
Nived N
Happy Automation

I am stuck at Move File Activity. Since bot needs to loop every row in Input file and then search the filename in directory. If found, move the file to the destination directory path of which is given in Input file corresponding to the filename.

Not really. Filename and destination path are in row(0) and row(1) of excel sheet. Bot needs to pick filename from row(0) and search the file in a source directory. If found, then move the file to the path mentioned in row(1) of excel sheet.

Check my worlflow I had did it in same way as u had mentioned

But make a little change there

Instead of copy file , use move file activitiy and ur isssue will.be resolved

@tilarapiyush Test with this:
In the For Each Row loop, add an If activity:

If File.Exists(Path.Combine(sourcePath, row(0).ToString))


In the Then section add a Move File activity:

Move File
.From Path = Path.Combine(sourcePath, row(0).ToString)
.To Destination = Path.Combine(row(1).ToString, row(0).ToString)


Also check the Overwrite option if you want to overwrite the file if it already exists in the destination folder.

Bot is running the workflow without error but not copying files in destination locations. Here are screenshots of Source File containing filenames and Path for clarity. Also the folder structure containing files.

Can u share the screenshot of move activitiy u had used ?

Sequence7.xaml (9.8 KB)

I am using this workflow. But files are not getting copied.

Hi @tilarapiyush i checked your workflow

I think the core reason ur file is not getting copied is the incorrect destination name you had provided in move file activity

In the destination part you had provided only the folder path, you had to provide the file path completely
Lets say there are two folder ABC and CDF , Folder ABC contains file text. You want to move file text from folder ABC to CDF, so u use move file activity with folllowing property values

From : ABC\text
To : CDF\text.

So in destination you had to specify the file name too

Hope it helps you

Regards

Nived N

Happy Automation

There are multiple files (not just one) which need to be copied. Mentioning just one file will only copy that file and won’t copy rest of the files to their designated folders.

I am saying through the previous is just an example

If I am correct item.ToString is the file path of file which is to be moved . So in destination section u had use Destination Path+\Path.GetFileName(item.ToString)

Like what I meant is in the destination part u had to mention complete filepath not the folder path only

You haven’t included the loop for directory which contains the files. Below is the entire process with Directory and Excel File (which contains file names and their destination after being copied) screenshots.
Scenario:

Filename and destination path are in row(0) and row(1) of excel sheet. Bot needs to pick filename from row(0) and search the file in a source directory. If found, then move the file to the path mentioned in row(1) of excel sheet.

That is what the If statement should do. It check if the file is in the source directory. Did you test to modify your workflow as I suggested?

If File.Exists(Path.Combine(sourcePath, row(0).ToString))

Yes I did modify it as you suggested, but it didn’t work.

Hey that worked. My bad I was not writing the source path correctly. Could you please explain what these lines of code did exactly ? This would be helpful for me in future.

Great to hear! The Path.Combine() is a safer way to create a file path.

Path.Combine(path, file) is the same as path + "\" + file (mostly). The trouble with combining the string yourself with plus is if the path already contains a backslash. Then you would end up with two slashes.

Eg. If path="C:\" and file="test.txt"
Path.Combine() will give you: "C:\test.txt"
While the plus method will give you: "C:\\test.txt"

Explanation of the code:

Let’s say that sourcePath=C:\Source
Then in the first iteration of the loop you will get

Path.Combine("C:\Source", "ABZ.txt")


which returns "C:\Source\ABZ.txt". File.Exists will then check if the file actually exists.

In the Move File, you have the same path combination as above in the From text field.

In the To text field, you will get:

Path.Combine("D:\Destination1", "ABZ.txt")


which will return "D:\Destination1\ABZ.txt" and this is where the Move File activity will move the file.

2 Likes

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