C# How to grep the last word from a directory path?
Asked Answered
J

6

0

I have a program that "greps" out various directory paths from a log text file and prints various results according to the word.

Examples of Directory paths:

  • C:/Documents and Settings/All Users/Desktop/AccessData FTK Imager.lnk

  • C:/Documents and Settings/All Users/Start Menu/Programs/AccessData

  • C:/Documents and Settings/Administrator/Desktop/AccessData FTK Imager.exe:Zone.Identifier

Therefore how can I grep out the file or folder name after the last "/"? This is to help the program to identify between files and folder. Please do take note of the multiple "." and white spaces found within a directory paths. etc "Imager.exe:Zone.Identifier". Therefore it is difficult to use if(!name.contains()".")

Etc. How to get the "AccessData FTK Imager.lnk" or "AccessData" or "AccessData FTK Imager.exe:Zone.Identifier" from the path STRING?!

May someone please advise on the methods or codes to solve this problem? Thanks!

The codes:

if (!token[7].Contains("."))
                {    

                    Console.WriteLine("The path is a folder?");

                    Console.WriteLine(token[7]);

                    Console.WriteLine(actions);

                    MacActions(actions);

                    x = 1;
                } 
Jehius answered 17/12, 2010 at 14:19 Comment(0)
D
1

Use the Path class when working with file paths, and use the File and Directory class when working with actual files and folders.

string str1=@"C:/Documents and Settings/All Users/Desktop/AccessData FTK Imager.lnk";
string str2=@"C:/Documents and Settings/All Users/Start Menu/Programs/AccessData";
string str3=@"C:/Documents and Settings/Administrator/Desktop/AccessData FTK Imager.exe:Zone.Identifier";

Console.WriteLine(Path.GetFileName(str1));
Console.WriteLine(Path.GetFileName(str2));
Console.WriteLine(Path.GetFileName(str3));

outputs:

AccessData FTK Imager.lnk
AccessData
Zone.Identifier <-- it chokes here because of the :

This class operates on strings, as I do not have those particular files and/or folders on my system. Also it's impossible to determine whether AccessData is meant to be a folder or a file without an extension.

I could use some common sense and declare everything with an extension to be a file (Path.GetFileExtension can be used here) and everything else to be a folder.
Or I could just check it the string in question is indeed a file or a folder on my machine using (File.Exists and Directory.Exists respectively).

if (File.Exists(str2))
    Console.WriteLine("It's a file");
else if (Directory.Exists(str2))
    Console.WriteLine("It's a folder");
else
    Console.WriteLine("It's not a real file or folder");
Dubitation answered 17/12, 2010 at 14:20 Comment(4)
Please look at the question again.... There is no need to get the path name as it is already supplied to the program...Jehius
Yes but how can we differentiate if the path is a file or folder?Jehius
@JavaNoob, you can't. See my answerIsla
@JavaNoob, it the files are local, you can check if they exist. It they are remote files, there is no sure way of knowing.Dubitation
V
1

Use Path.GetFileName.

The characters after the last directory character in path. If the last character of path is a directory or volume separator character, this method returns String.Empty.

Vaughnvaught answered 17/12, 2010 at 14:20 Comment(5)
@Jehius - The Path class is an abstraction of file paths. It lets you load any path string to extract information from it.Vaughnvaught
Yes but how can we differentiate if the path is a file or folder?Jehius
@Jehius - There is no way to tell. Can you tell me if this string ends in a file or a directory? c:\mydir\docs\stuffVaughnvaught
The strings provided by the text file just outputs exactly what you see in the above with various endings in files and directories.Jehius
@Jehius - Exactly my point. Without a physical file or directory, you can't look up the data. They are completely indistinguishable from each other.Vaughnvaught
D
1

Use the Path class when working with file paths, and use the File and Directory class when working with actual files and folders.

string str1=@"C:/Documents and Settings/All Users/Desktop/AccessData FTK Imager.lnk";
string str2=@"C:/Documents and Settings/All Users/Start Menu/Programs/AccessData";
string str3=@"C:/Documents and Settings/Administrator/Desktop/AccessData FTK Imager.exe:Zone.Identifier";

Console.WriteLine(Path.GetFileName(str1));
Console.WriteLine(Path.GetFileName(str2));
Console.WriteLine(Path.GetFileName(str3));

outputs:

AccessData FTK Imager.lnk
AccessData
Zone.Identifier <-- it chokes here because of the :

This class operates on strings, as I do not have those particular files and/or folders on my system. Also it's impossible to determine whether AccessData is meant to be a folder or a file without an extension.

I could use some common sense and declare everything with an extension to be a file (Path.GetFileExtension can be used here) and everything else to be a folder.
Or I could just check it the string in question is indeed a file or a folder on my machine using (File.Exists and Directory.Exists respectively).

if (File.Exists(str2))
    Console.WriteLine("It's a file");
else if (Directory.Exists(str2))
    Console.WriteLine("It's a folder");
else
    Console.WriteLine("It's not a real file or folder");
Dubitation answered 17/12, 2010 at 14:20 Comment(4)
Please look at the question again.... There is no need to get the path name as it is already supplied to the program...Jehius
Yes but how can we differentiate if the path is a file or folder?Jehius
@JavaNoob, you can't. See my answerIsla
@JavaNoob, it the files are local, you can check if they exist. It they are remote files, there is no sure way of knowing.Dubitation
I
1

This is to help the program to identify between files and folder

There is no way to determine is a path represents a file or folder, unless you access the actual file system. A directory name like 'Foo.exe' would be perfectly valid, and a file with no extension ('Foobar') would be valid too.

Isla answered 17/12, 2010 at 14:23 Comment(0)
V
1

how about tokenized it with "/" like what you're doing ... and then you'll know that the last token is the file, and whatever before it is the path.

Vicious answered 17/12, 2010 at 14:24 Comment(0)
F
0

You can simply split the whole string by /

e.g.:

string a="C:/Documents and Settings/All Users/Desktop/AccessData FTK Imager.lnk"; string[] words=a.split('/'); int len=words.length; so now words[len] returns the data after last slash(/).. I hope you understand...

Forgiveness answered 17/12, 2010 at 14:26 Comment(1)
Thats tokenizing as mentioned by aggi.Jehius
A
0

I guess you only have a string that represents the name of the file, if that is the case you can't really be sure. It's totally ok to have a folder namen something like Folder.doc. So if you don't have access to the actual file system it is hard to check. You can get close though using regular expression like:

(.*\\)(.+)(\..*)

Try it on: http://www.regexplanet.com/simple/index.html If you get any output in group number 3 it's likely that it is a file and not a folder. If you don't get some output try this direct after:

(.*\\)(.+)(\..*)?

That will give you the folder in group 2.

Albigenses answered 17/12, 2010 at 14:26 Comment(1)
Any clues on how to create that regular expressions?Jehius

© 2022 - 2024 — McMap. All rights reserved.