vc2010下获取指定文件夹下指定后缀名的文件
如题,如何实现?在网上找了好多都编译出错。谢谢了
程序代码:
using namespace System;
using namespace System::IO;
Int32 main(array<String^> ^args)
{
Console::WriteLine("功能: 使用通配符检索文件");
Console::WriteLine("用法: FileList [/s] [pattern]");
Console::WriteLine("参数: /s 搜索进入子目录,默认不进入");
Console::WriteLine(" pattern 使用通配符的文件名,即包含?和*的字符串");
Console::WriteLine("备注: 1.参数顺序可以颠倒");
Console::WriteLine(" 2.如果没有输入命令行参数,程序会询问");
Console::WriteLine(" 3.若pattern为空则默认检索当前目录的*.*");
Console::WriteLine(" 4.pattern应包含合法的目录路径和文件名两部分");
Console::WriteLine();
String^ path = Directory::GetCurrentDirectory();
String^ search_pattern = "*.*";
SearchOption option = SearchOption::TopDirectoryOnly;
array<String^> ^_args = args;
if (args->Length == 0)
{
Console::Write("请输入检索指令:");
String^ buffer = Console::ReadLine();
if (!String::IsNullOrEmpty(buffer))
{
array<Char>^ separator = { L' ' };
_args = buffer->Split(separator);
}
}
for each (String^ s in _args)
{
if (s->ToUpper() == "/S")
{
option = SearchOption::AllDirectories;
}
else
{
path = s->Substring(0, s->LastIndexOf('\\') + 1);
search_pattern = s->Substring(s->LastIndexOf('\\') + 1);
if (String::IsNullOrEmpty(search_pattern))
{
search_pattern = "*.*";
}
}
}
try
{
array<String^> ^files = Directory::GetFiles(path, search_pattern, option);
for each (String^ file in files)
{
Console::WriteLine(file);
}
}
catch (Exception^ e)
{
Console::WriteLine(e->Message);
}
Console::Write("\n按<Enter>结束程序...");
Console::ReadLine();
return 0;
}
