2011-06-08 85 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和二進制文件,不需要更改。