如何读取目录中包含的所有文件 (WINDOWS)
How to Read all files contained in a directory (WINDOWS)
我需要传输目录中包含的所有文件。
我应该如何继续读取特定目录中的所有文件,然后通过套接字传输?
编辑:我对转移没有问题,只是不知道我应该怎么做才能下载完整的目录。
这是一个简单的版本,可以帮助您入门,因为如果我理解正确,您唯一的麻烦是检索文件列表而不是传输它们。通过一些递归,您还可以下降到子目录并一次性获取完整的列表(修改此示例很容易)。
// Returns files in the specified directory path.
vector<wstring> list_files(wstring path)
{
vector<wstring> subdirs, matches;
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile((path + L"\*.*").c_str(), &ffd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
wstring filename = ffd.cFileName;
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
matches.push_back(path + L"\" + filename);
} while (FindNextFile(hFind, &ffd) != 0);
}
FindClose(hFind);
return matches;
}
例:
vector<wstring> files = list_files("C:\pr0n");
// 'files' now contains 1,000 file entries.
// Directories are not included.
// Now send them over individually.
请注意,如果您有兴趣,还有更好的跨平台替代方案,例如 boost FS。
这是来自 MDSN 的示例代码,但我像您一样对其进行了一点修改只想从当前目录中获取所有文件。我假设您正在使用 MFC。 如果没有,请参阅此列表。
void Recurse(LPCTSTR pstr)
{
CFileFind finder;
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
// skip . and .. files; otherwise, we'd
// recur infinitely!
if (finder.IsDots())
continue;
if (!finder.IsDirectory())
{
CString str = finder.GetFilePath();
AfxMessageBox( str );
}
}
finder.Close();
}
要使用它,请致电:
Recurse(_T("C:\intel"));
相关文章:
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 从存储为 Windows 资源 (c++) 的 png 中获取 png 文件数据
- vcruntime.h 从 Windows 包含文件夹中丢失?
- 在Linux上使用Clang / OLLVM交叉编译helloworld Windows可执行文件时的问题
- 如何在Windows内核中获取文件大小
- Qt自动生成的.rc文件无法打开包含文件'windows.h'
- MinGW GCC通配符编译所有文件(Windows)
- QTCreator G .EXE:致命错误:无输入文件(Windows)
- 切割和粘贴后监视文件(Windows shell扩展?)
- 如何读取目录中包含的所有文件 (WINDOWS)
- WebCam预览和捕获到文件(Windows)
- 从 c++ 中的当前目录复制文件(Windows API)
- 我可以将一个可执行文件与另一个可执行文件(Windows 服务程序)组合在一起,并将该程序作为逻辑服务运行吗?
- C++锁定文件Windows阻止模式
- 正在将明文AES 128密钥导出到缓冲区/文件Windows Crypto API c++
- 有效删除文件Windows c++