2011-06-08 190 views
0

我正在寻找C#或C++/C中可从文件中提取文件路径的工具/代码,例如,如何从文本文件中提取文件路径

FILE.TXT:

Lorem Impusum 
C:\Windows\System32\test.exe 
C:\Users\Limited\Downloads.txt 
testing 123 

所以它会输出FILE.TXT如下:

C:\Windows\System32\test.exe 
C:\Users\Limited\Downloads.txt 

回答

2

这应返回你以后,假设你已经加载的文件的内容为List<string>string[]

 var result = potentialPaths.Where(Path.IsPathRooted).ToList(); 

此外,这是C#。

+0

请注意,这可能会抛出一个'ArgumentException'当其他行在文件/'潜在路径'包含完全任意的文本,其中包含来自'Path.GetInvalidPathChars()'的字符。然而,可以说,OP给出的例子有点粗糙,所以无论如何它可能是OK的。 – 2011-06-10 05:34:48

0

看一看C#System.IO.Path

Google是你的朋友。

+0

这就有点简单了:/ – James 2011-06-08 17:36:33

+0

GetDirectoryName是列出的第六种方法。还有其他许多有用的方法:http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx – Jay 2011-06-13 18:26:17

0

有一个例子如何做到这一点在C++中的位置:http://www.gbresearch.com/axe/Reference.pdf

下面是摘录:

// windows path can start with a server name or letter 
auto start_server = "\\\\" & +path_chars - '\\'; 
auto start_drive = r_alpha() & ':'; 
auto simple_path = (start_server | start_drive) & *('\\' & +path_chars); 
auto quoted_path = '"' & (start_server | start_drive) & 
*('\\' & +(space | path_chars)) & '"'; 
// path can be either simple or quoted 
auto path = simple_path | quoted_path; 
// rule to extract all paths 
std::vector<std::wstring> paths; 
size_t length = 0; 
auto extract_paths = *(*(r_any() - (path >> e_push_back(paths) >> e_length(length))) 
& r_advance(length)); 

您可以为您的目的自定义规则。规则也适用于unicode和二进制文件,不需要更改。