13 Replies - 20165 Views - Last Post: 18 February 2010 - 07:14 AM Rate Topic: -----

#1 elenatinuviel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-March 08

How to get a specific filename from any file path?

Posted 18 February 2010 - 12:11 AM

I'm currently writing a program to read a specific file, and then write the info stored in the file to the database. I've got the writing to the database figured out; what's causing me problems is getting the specific file.

I'm using C# .Net, and writing code for a console application. I know how to use the GetFileName and GetFullPath options in C#, but my problem is trying to get the file from the desired directory/subdirectory. I need a way to search through all the directories and subdirectories and find the file I need to use. For example, my file name is "Telephone Logging.csv" and it's located in "C:\Dir\subdir1\subdir2\Telephone Logging.csv" but whenI run the test program, the GetPullPath option returns the all the directories/subdirectories leading to the project's bin folder, where the file is obviously *NOT* stored.

Below is a short (very short) version of the code i'm using, more of a test program to check if the file is being read in properly.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Text;
using System.IO;


namespace Telephone_Log
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath;

            filePath = @"c:..\Telephone Logging.csv";

            Console.WriteLine("Directory name: " + Path.GetDirectoryName(testPath));
            Console.WriteLine();

            if (Path.HasExtension(filePath)) 
            {
                Console.WriteLine("File extension: " + Path.GetExtension(filePath));
                Console.WriteLine();
            }

            Console.WriteLine("Filename: " + Path.GetFileName(filePath));
            Console.WriteLine();

            Console.WriteLine("Full path: " + Path.GetFullPath(filePath));
            Console.WriteLine();

                        
            FileInfo fInfo = new FileInfo(Path.GetFullPath(testPath));

            string dir = fInfo.DirectoryName.ToString();
            string fileN = fInfo.Name.ToString();

            
            Console.WriteLine("DIRECTORY IS: " + dir);
            Console.WriteLine();
            Console.WriteLine("FILE NAME IS: " + fileN);
            Console.WriteLine();
            
            Console.WriteLine("Press enter");
            Console.Read();
        }
    }
}




When I use this code, I don't get the proper output, but I am not sure what I'm doing wrong or what I'm missing. :( Can anyone please help?



Add:

The output I get is:

Directory Name: c:..

File Extension: .csv

File Name: Telephone Logging.csv

Full Path: C:\Documents and Settings\Khan\My Documents\Visual Studio 2008\Projects\Telephone_Log\bin\Telephone Logging.csv

DIRECTORY IS: C:\Documents and Settings\Khan\My Documents\Visual Studio 2008\Projects\Telephone_Log\bin

FILE NAME IS: Telphone Logging.csv

This post has been edited by elenatinuviel: 18 February 2010 - 12:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to get a specific filename from any file path?

#2 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 01:39 AM

string SearchPattern = "Telphone Logging.csv";
var temporaryFileSearch = new DirectoryInfo(@"C:\");
var fileList = temporaryFileSearch.GetFiles(SearchPattern, SearchOption.AllDirectories);



That should find the file for you. If you change the SearchPattern string to some other file name then you will search for a different file. Right now you are telling it to search the C drive (every directory) for a file called "Telphone Logging.csv" Enjoy
Was This Post Helpful? 0
  • +
  • -

#3 elenatinuviel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-March 08

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 04:16 AM

View PostAdkins, on 18 February 2010 - 09:39 AM, said:

string SearchPattern = "Telphone Logging.csv";
var temporaryFileSearch = new DirectoryInfo(@"C:\");
var fileList = temporaryFileSearch.GetFiles(SearchPattern, SearchOption.AllDirectories);



That should find the file for you. If you change the SearchPattern string to some other file name then you will search for a different file. Right now you are telling it to search the C drive (every directory) for a file called "Telphone Logging.csv" Enjoy



Hi Adkins
Thank you very much for your help!



[Edit]

Ok, stupid me, I figured it out. Never mind. Just had to add ToString() :stupid:



But now I have another problem: when I'm running the console application, I get an exception (Access to the path 'C:\System Volume Information' is denied.). how do I check if I have enough acces, or get around this?? :unsure:

This post has been edited by elenatinuviel: 18 February 2010 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#4 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 04:38 AM

Don't worry about asking questions (no matter how stupid you think they are) that is how I learned too!

The file search will return an array of FileInfo items. Each item in there has a property called FullName. This gives you the path and the file name together in a nice neat package stored as a string. That is exactly what you need!
Was This Post Helpful? 0
  • +
  • -

#5 elenatinuviel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-March 08

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 05:05 AM

Thanks a lot Adkins! :lol:

But umm... Could you please tell me how I can access the info in the file array.. and assign it to my testPath variable?

I tried
testpath = fileList.ToString();
as I thought that was the answer, but it wasn't... so now I'm confused all over again. And I am still getting the exception (Access to the path 'C:\System Volume Information' is denied) :crazy:
Was This Post Helpful? 0
  • +
  • -

#6 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 05:13 AM

The reason you are getting the error is because it is searching in folders it shouldn't be searching in. If you could narrow down the search path a little that would get rid of the problem.

If you know there is only one file with that name, the way I do it (even though I know this isn't the best or most proper way) is simply
testpath = fileList[0].FullName;


If someone can correct me on a better way to handle this that would be great! Till then however that will get you working.
Was This Post Helpful? 1
  • +
  • -

#7 elenatinuviel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-March 08

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 05:52 AM

Thanks a lot!!! :lol:

I changed the directory to be a little more specific, so it solved the whole Access denied problem, but now even by typing the code you gave
testPath = fileList[0].FullName;
I am yet another exception - Index was outside the bounds of the array.. I don't understand why this is happening? Such a small thing is giving me so many problems! :cry2:
Was This Post Helpful? 0
  • +
  • -

#8 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 06:34 AM

That means that it is not finding the file. I would recommend changing your search pattern to *Logging.csv If that doesn't work post the code for your search (including pattern and everything and I will see what I can come up with.
Was This Post Helpful? 0
  • +
  • -

#9 elenatinuviel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-March 08

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 06:46 AM

View PostAdkins, on 18 February 2010 - 02:34 PM, said:

That means that it is not finding the file. I would recommend changing your search pattern to *Logging.csv If that doesn't work post the code for your search (including pattern and everything and I will see what I can come up with.



I tried it with *Logging.csv and it worked perfectly! Thank you! :lol:
I am guessing that it was the white space in the file name that was causing the problem. Is there a way to get around this because I cant always hope that the user saves the file without a white space?
Was This Post Helpful? 0
  • +
  • -

#10 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 06:56 AM

To be honest I didn't even know white space was a problem :P And I am not sure off the top of my head about a solution either, but I can look around and ponder it and let you know what I come up with.
Was This Post Helpful? 0
  • +
  • -

#11 elenatinuviel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-March 08

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 07:01 AM

Hehe, I know what you mean! Same here!

But thanks a lot for all the help, otherwise. Much appreciated :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#12 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 07:01 AM

you could try searching for "Telephone?Logging.csv" but I don't know if that will give the result you want
Was This Post Helpful? 0
  • +
  • -

#13 elenatinuviel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 09-March 08

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 07:09 AM

View PostAdkins, on 18 February 2010 - 03:01 PM, said:

you could try searching for "Telephone?Logging.csv" but I don't know if that will give the result you want



I tried it and it worked beautifully :hehe: ... YaY :w00t: Thanks a million!
Was This Post Helpful? 0
  • +
  • -

#14 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: How to get a specific filename from any file path?

Posted 18 February 2010 - 07:14 AM

no worries :bananaman:

EDIT:

for clarification purposes the '?' just means it is a wild card but only for one character. There for that covers your white space but doesn't find files with more words between telephone and logging.

This post has been edited by Adkins: 18 February 2010 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1