Reputation: 108
So I have this list of filenames.avi. each one comes from a different directory, path. I have a txt file of a list of those.
When they are all in the same folder thats easy, when I have each in one folder I must change the directory path accordingly for each file.
Thank you
Upvotes: 0
Views: 146
Reputation: 8656
You can use Directory.GetFiles()
method and search sub-directories. I recommend to narrow down the search because you will get Exceptions when accessing restricted folders, but generally you could use a method like the following one which will return the path to the first occurrence of the filename or will return null
:
public string SearchFileSystem(string filename)
{
string [] files = null;
try
{
files = Directory.GetFiles("C:\\", filename, SearchOption.AllDirectories);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return files==null?null:files.FirstOrDefault();
}
Upvotes: 1
Reputation: 7215
var sr = new StreamReader("filelist.txt");
while(!sr.EOF)
{
string[] ListFiles = Directory.GetFiles("D:\movies\",
sr.ReadLine()/*your file name is the search pattern*/,
SearchOption.AllDirectories);
if(ListFIles.Count > 0)
{
//File Found
foreach(var f in ListFiles)
{
string fullPath = Path.GetFullPath(file);
string fileName = Path.GetFileName(fullPath);
}
}
}
Upvotes: 0
Reputation: 7005
This will give you the file name
fullPath = Path.GetFullPath(file);
fileName = Path.GetFileName(fullPath);
and this will give you the file path
shortPath = fullPath.Substring(0, inputText.LastIndexOf("\"));
Upvotes: 0
Reputation: 3940
You will want to use the System.IO.Path
class. The Path.GetDirectoryName
will return the directory name (without a trailing '\' character).
There are other useful methods in the Path
class, such as GetFileName
and GetExtension
.
Upvotes: 5