解决文件搜索问题 [英] Solving a file search problem
问题描述
嗨
我有一个似乎无法用最简单的方法解决的问题……也许是因为星期五.请问有人可以帮我解决这个问题吗?
我正在所有目录和子目录中搜索* .doc文件,如下所示,并将它们保存到列表中.
我要搜索的文档文件是这样的:
单元测试文件夹:Blah.c
1590-VMM-C-2004_ 010 SCIC修订版 00 .doc
1590-VMM-C-2004_010 SCIC修订版01.doc
1590-VMM-C-2004_010 SCIC修订版02.doc
1590-VMM-C-2004_010 SUTC修订版00.doc
1590-VMM-C-2004_010 SUTC修订版01.doc
1590-VMM-C-2004_010 SUTC修订版02.doc
单元测试文件夹:Foo.c
1590-VMM-C-2004_ 096 SCIC修订版 00 .doc
1590-VMM-C-2004_096 SCIC修订版01.doc
1590-VMM-C-2004_096 SUTC修订版00.doc
1590-VMM-C-2004_096 SUTC修订版01.doc
在我的每个单元测试文件夹中,都有许多这些* .doc文件,其修订取决于单元测试的更新次数.上面加粗的010和096数字是一个唯一的数字,该数字被分配给来自同一单元测试的文档.
问题:我想在各种单元测试文件夹中搜索各种scic和sutc文档的所有最新修订,并将它们保存在列表中.我怎么做而不必编写一堆嵌套的if语句?
列表输出应为以下示例:
列表[0] ="1590-VMM-C-2004_010 SCIC修订版02.doc"
列表[1] ="1590-VMM-C-2004_010 SUTC Rev 02.doc"
列表[2] ="1590-VMM-C-2004_096 SCIC修订版01.doc"
列表[3] ="1590-VMM-C-2004_096 SUTC Rev 01.doc"
功能示例:
Hi
I''ve gota problem which I can''t seem to get solved in the simplest possible way... Maybe its because its Friday. Is there someone who can help me solve it please?
I''m searching all directories and sub directories for *.doc files, as shown below and saving them to a List.
The docfiles which I''m searching for is as such:
Unit testing folder for: Blah.c
1590-VMM-C-2004_010 SCIC Rev 00.doc
1590-VMM-C-2004_010 SCIC Rev 01.doc
1590-VMM-C-2004_010 SCIC Rev 02.doc
1590-VMM-C-2004_010 SUTC Rev 00.doc
1590-VMM-C-2004_010 SUTC Rev 01.doc
1590-VMM-C-2004_010 SUTC Rev 02.doc
Unit testing folder for: Foo.c
1590-VMM-C-2004_096 SCIC Rev 00.doc
1590-VMM-C-2004_096 SCIC Rev 01.doc
1590-VMM-C-2004_096 SUTC Rev 00.doc
1590-VMM-C-2004_096 SUTC Rev 01.doc
In each of my unit testing folders there are a bunch of these *.doc files, which revision depends on howmany times the unit tests was updated. The 010 and 096 number that was bolded above is a unique number that gets assigned to docs from the same unit test.
The Question: I want to search for all the latest revisions of the various scic and sutc docs in the various unit testing folders and save them in a list. How can I do this without having to write a whole bunch of nested if-statements?
Example of what list output should look like:
List[0] = "1590-VMM-C-2004_010 SCIC Rev 02.doc"
List[1] = "1590-VMM-C-2004_010 SUTC Rev 02.doc"
List[2] = "1590-VMM-C-2004_096 SCIC Rev 01.doc"
List[3] = "1590-VMM-C-2004_096 SUTC Rev 01.doc"
FUNCTION EXAMPLE:
/*--[ Function ]-----------------------------------------------------------*/
/*
Function Name : SearchLatestDocPaths
Description : This function searches for all the latest rev. *.doc file
paths in the respective unit testing folders.
Access Modifier : public
Param : string sDir
Retval : -none-
*/
/*-------------------------------------------------------------------------*/
public void SearchLatestDocPaths(string sDir)
{
list_utLatestDocFilePath.Clear();
try
{
// Search sub directories and save *.doc files
foreach (string d1 in Directory.GetDirectories(sDir))
{
foreach (string d2 in Directory.GetDirectories(d1))
{
foreach (string f in Directory.GetFiles(d2, "*.doc"))
{
if (f.Contains(".doc") == true)
{
list_utLatestDocFilePath.Add(f);
}
}
}
WriteToBufferSwSrcFilePaths(d1, ".doc", false);
}
// Search current directories and save *.doc files
foreach (string f in Directory.GetFiles(sDir, "*.doc"))
{
list_utLatestDocFilePath.Add(f);
}
// Print all documents to see correctness
foreach (string f in list_utDocFilePath)
{
MessageBox.Show(f);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
;-)
;-)
推荐答案
您可以使用以下搜索模式:
"* SCIC * .doc"和"* SUTC * .doc",并仅选择两个最高修订版本号.我还更改了您的搜索方法,因此它可以递归运行,这也意味着代码更少.
You could just use this search pattern:
"*SCIC*.doc" and "*SUTC*.doc" and select only the two highest revision numbers. I also changed your search method so it operates recursively which also means less code.
public void SearchLatestDocPaths(string sDir, li)
{
// list_utLatestDocFilePath.Clear(); <- would make the recursion useless
try
{
// Search sub directories and save *.doc files
foreach (string d1 in Directory.GetDirectories(sDir))
{
SearchLatestDocPaths(d1);
WriteToBufferSwSrcFilePaths(d1, ".doc", false); // <- adding file path?
}
// Search current directories and save *.doc files
foreach (string f in Directory.GetFiles(sDir, "*.doc"))
{
list_utLatestDocFilePath.Add(f);
// Print all documents to see correctness
MessageBox.Show(f);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
http://msdn.microsoft.com/en-us/library/8he88b63.aspx [ ^ ]
祝你好运!
http://msdn.microsoft.com/en-us/library/8he88b63.aspx[^]
Good luck!
这可以解决问题:
This does the trick thx:
/*--[ Function ]-----------------------------------------------------------*/
/*
Function Name : SearchDocPaths
Description : This function searches for all the docs
in the various unit testing subfolders
and saves their paths to a list.
Access Modifier : public
Param : string sDir
Retval : -none-
*/
/*-------------------------------------------------------------------------*/
public void SearchDocPaths(string sDir)
{
try
{
// Search sub directories and save *.doc files
foreach (string d1 in Directory.GetDirectories(sDir))
{
// recursion
SearchDocPaths(d1);
// Search current directories and save *.doc files
foreach (string f in Directory.GetFiles(d1, "*.doc"))
{
list_utDocFilePath.Add(f);
}
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
/*--[ Function ]-----------------------------------------------------------*/
/*
Function Name : ExtractLatestDocPaths
Description : This function searches for all the latest rev. *.doc file
paths in the respective unit testing folders and saved it
to a list.
Access Modifier : public
Param : string sDir
Retval : -none-
*/
/*-------------------------------------------------------------------------*/
public void ExtractLatestDocPaths(string sDir)
{
int idx = 0;
string latest_rev_string = "";
// Fill list buffer containing all document, paths
SearchDocPaths(sDir);
// Extract only the latest revisions
for (idx = 0; idx < list_utDocFilePath.Count; idx++)
{
latest_rev_string = list_utDocFilePath[idx];
if (((idx + 1) == list_utDocFilePath.Count) ||
(list_utDocFilePath[idx].Substring(
list_utDocFilePath[idx].Length - 35, 24)) !=
(list_utDocFilePath[idx + 1].Substring(
list_utDocFilePath[idx + 1].Length - 35, 24)))
{
list_utLatestDocFilePath.Add(latest_rev_string);
}
else
{
// do nothing
}
}
}
这篇关于解决文件搜索问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!