Answered by:
LINQ Lambda Expression

Question
-
User1867786015 posted
Hi,
I have scenario to get file from one folder and move the files to another folder.
I written the linq lambda query for getting all files from folder.
DirectoryInfo sourceFolder= new DirectoryInfo(outfolderPath);
var files = sourceFolder.getfiles().select(x => x.name).Toarray();
I have another method to move the file from another folder.
In this method i have input parameter as filename.
Based on input filename i am moving particular file to another folder.
I need to know how to do this scenario using LINQ lambda expression.
Please help me the way to do this.
Thanks,
Saravanan.A
Thursday, February 5, 2015 4:31 AM
Answers
-
User-830258159 posted
Use the below code
string sourceDirectory = "c:\\Prakher\\"; string targetDirectory = "c:\\Prakher\\Test\\"; System.IO.Directory.GetFiles(sourceDirectory).ToList().ForEach(file => System.IO.File.Move(file, targetDirectory + file.Split('\\').Last()));
you can even call your own method instead of File.Move inside the foreach loop.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, February 5, 2015 4:49 AM
All replies
-
User1577371250 posted
Hi,
1. get the files from a directory and Move. I think you don;t need LINQ for this.
string targetDirectory = @"D:\Test\"; if(Directory.Exists(targetDirectory)) { string [] fileEntries = Directory.GetFiles(targetDirectory); foreach(string fileName in fileEntries) { File.Move(Path.Combine(targetDirectory,fileName), @"D:\test1\") } }
https://msdn.microsoft.com/en-us/library/wz42302f%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/system.io.file.move%28v=vs.110%29.aspx
Thursday, February 5, 2015 4:42 AM -
User-830258159 posted
Use the below code
string sourceDirectory = "c:\\Prakher\\"; string targetDirectory = "c:\\Prakher\\Test\\"; System.IO.Directory.GetFiles(sourceDirectory).ToList().ForEach(file => System.IO.File.Move(file, targetDirectory + file.Split('\\').Last()));
you can even call your own method instead of File.Move inside the foreach loop.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, February 5, 2015 4:49 AM -
User1867786015 posted
Thanks,
Actually i want particular filename need to move one folder to another folder.
In Where condition to apply filename?.
How to apply this.
Thursday, February 5, 2015 5:22 AM -
User-830258159 posted
you can apply a filter of file name as below
System.IO.Directory.GetFiles(sourceDirectory).Where(f=> f.contains(yourfilename)).ToList().ForEach(file => System.IO.File.Move(file, targetDirectory + file.Split('\\').Last()));
Thursday, February 5, 2015 12:56 PM