Copy files to different certain folders

I have numbers of txt files but want to copy the certain txt files to certain folders (also newly created same folder name to file name).
eg. A.txt to A folder, B.txt to B folder

Hello @koolrc812

You can use Directory.getfiles(”folder path”) to get all the files. Then loop through that list using for each activity.
Inside that use create folder activity and after use copy file activity and provide newly created folder path as destination.

Thanks

Hi @Rahul_Unnikrishnan
I have got For each then create folder.after that create copy file activity? but im stuck here what do I put in (copy file activity) To: ?
I did put the newly created folder path but it says already exists?

Create Folder, folder name: "C:\Users\Testing\Copy"+Path.GetFileNameWithoutExtension(txtfile.ToString)

Copy file activity To: "C:\Users\Testing\Copy"+Path.GetFileName(txtfile)

In copy file source will be “basepath”+filename+”.txt” and destination will be “basepath”+filename+”/”

Here filename is dynamic (a, b, c getting from for loop)

Sorry I’m a beginner. for the filename, do I need to declare it?
May I have a screenshot for reference?
I tried so many ways, either had error says already exists, Operator ‘+’ is not defined for string, either cant find the path.
not sure which part I missed. Thanks

Hi @koolrc812,

Hope below Workflow help you

Input
image

Output
image

Workflow

Code

currentItem.Replace(Path.GetFileName(currentItem).ToString,“”)+""+Path.GetFileNameWithoutExtension(currentItem).ToString

Xaml
Q.xaml (8.9 KB)

Thanks,
Rajkumar

Thanks heaps it worked with Move File activity. but when I tried to do with Copy File activity, it shows the target file is a directory, not a file

Hi @koolrc812,

Just replace Move File Activity to Copy File like below and from and To Value same
image

Output
image

Thanks,
Rajkumar

Yes I did, but now shows Copy File: Could not find a part of the path 'C:\Users\Testing\Copy'.

Hi @koolrc812,

if possible kindly share your workflow screenshot

you need to do some changes in this flow like this

use below mentioned expression in Copy File, Folder Create and IF condition after “Directory.Exists(”

txtfile.Replace(Path.GetFileName(txtfile).ToString,“”)+""+Path.GetFileNameWithoutExtension(txtfile).ToString


it shows as the above, didnt auto save it in the certain folders and the copied files type is File instead of txt

Hi @koolrc812,

Kindly follow below workflow

Q1
Directory.GetFiles(stPath).ToList()

Q2
Directory.Exists(txtfile.Replace(Path.GetFileName(txtfile).ToString,“”)+""+Path.GetFileNameWithoutExtension(txtfile).ToString)

Q3
txtfile.Replace(Path.GetFileName(txtfile).ToString,“”)+""+Path.GetFileNameWithoutExtension(txtfile).ToString

Q4
txtfile.Replace(Path.GetFileName(txtfile).ToString,“”)+""+Path.GetFileNameWithoutExtension(txtfile).ToString

Thanks,
Rajkumar

Thanks heaps. You guys are awesome!!!

1 Like

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