编程论坛
注册
登录
编程论坛
→
C++教室
vc2010下获取指定文件夹下指定后缀名的文件
howema
发布于 2012-11-28 17:35, 3553 次点击
如题,如何实现?在网上找了好多都编译出错。
谢谢了
3 回复
#2
mmmmmmmmmmmm
2012-11-28 17:41
没用2010
#3
TonyDeng
2012-11-29 03:33
程序代码:
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
;
}
#4
rjsp
2012-11-29 08:55
你这个问题和vc2010没有任何关系,你想问的其实是:windows下如何获取指定文件夹下指定后缀名的文件列表
无非就是调用 FindFirstFile/FindNextFile/FindClose 这三个windows api
1