tags:

views:

102

answers:

6

Hi,

I need to move all files from source folder to destination folder. How can I easily extract file name from file path name?

string newPath = "C:\\NewPath";

string[] filePaths = Directory.GetFiles(_configSection.ImportFilePath);
foreach (string filePath in filePaths)
{
  // extract file name and add new path 
  File.Delete(filePath);
}
+4  A: 

Try the following:

string newPathForFile = Path.Combine(newPath, Path.GetFileName(filePath));
Pieter
Thanks, I love this site)) 1 min to get the answer.
Captain Comic
You're welcome. Got nothing better to do anyway (you know: work).
Pieter
lot of people to see your problem :) , Collective Intelligence
saurabh
+10  A: 
Path.GetFileName(filePath)
saurabh
+5  A: 

use DirectoryInfo and Fileinfo instead of File and Directory, they present more advanced features.

DirectoryInfo di = 
    new DirectoryInfo("Path");
FileInfo[] files = 
    di.GetFiles("*.*", SearchOption.AllDirectories);

foreach (FileInfo f in files)
    f.MoveTo("newPath");
vaitrafra
+3  A: 

You may want to try the FileInfo.MoveTo method (code example at the following link):

http://msdn.microsoft.com/en-us/library/system.io.fileinfo.moveto.aspx

dhirschl
Yeap, that looks pretty too.
Captain Comic
+3  A: 

You can do it like this:

string newPath = "C:\\NewPath"; 
string[] filePaths = Directory.GetFiles(_configSection.ImportFilePath);  
foreach (string filePath in filePaths)  
{  
   string newFilePath = Path.Combine(newPath, Path.GetFileName(filePath);
   File.Move(filePath, newFilePath);
}
klausbyskov
A: 

maybe its work:

string[] val = newPath.Split('\\');
string FileName = val[val.Length-1];
us50