由于这个问题没有产生有用的答案和一些评论,请参阅我的其他问题,它得到了答案:How exactly "Everything Search" can give me immediately searchable list of 2bln files on my 4TB HDD in less than 10 seconds?
我知道读取目录的唯一方法是递归地下降到每个目录,但如果我想快速找到整个磁盘上的任何位置,这太慢了。
有一个Windows程序“Everything Search”http://www.voidtools.com/这比我想象的递归下降速度要快(它在 10 秒内读取 4TB HDD 上近 20 亿个文件的文件名)。
我知道我可以提前构建索引,但是可以通过一次操作将磁盘的整个目录树读入内存并在那里解析来完成吗?
编辑
由于我的问题被证明令人困惑,这就是我想做的:
// For Directory::GetFiles and Directory::GetDirectories
// For File::Exists, Directory::Exists
using namespace System;
using namespace System::IO;
using namespace System::Collections;
// Insert logic for processing found files here.
void ProcessFile( String^ path )
{
Console::WriteLine( "Processed file '{0}'.", path );
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
void ProcessDirectory( String^ targetDirectory )
{
// Process the list of files found in the directory.
array<String^>^fileEntries = Directory::GetFiles( targetDirectory );
IEnumerator^ files = fileEntries->GetEnumerator();
while ( files->MoveNext() )
{
String^ fileName = safe_cast<String^>(files->Current);
ProcessFile( fileName );
}
// Recurse into subdirectories of this directory.
array<String^>^subdirectoryEntries = Directory::GetDirectories( targetDirectory );
IEnumerator^ dirs = subdirectoryEntries->GetEnumerator();
while ( dirs->MoveNext() )
{
String^ subdirectory = safe_cast<String^>(dirs->Current);
ProcessDirectory( subdirectory );
}
}
int main( int argc, char *argv[] )
{
for ( int i = 1; i < argc; i++ )
{
String^ path = gcnew String(argv[ i ]);
if ( File::Exists( path ) )
{
// This path is a file
ProcessFile( path );
}
else
if ( Directory::Exists( path ) )
{
// This path is a directory
ProcessDirectory( path );
}
else
{
Console::WriteLine( "{0} is not a valid file or directory.", path );
}
}
}
我想获得相同的信息,但不想多次调用Directory::GetDirectories
。我正在寻找的解决方案无论如何看起来都不像这段代码。此代码只是说明我想要读取磁盘的哪些信息(目录中所有文件的名称),而不是我想要的方式(我不想要递归和与目录一样多的系统调用)。
编辑2(对于认为这个问题太宽泛的人):
我问如何在 Windows 或 Linux 操作系统上执行此操作。我会接受任何语言的答案,因为我最感兴趣的是需要进行哪些系统调用(以及如何解析这些调用的结果)才能将整个驱动器的 NTFS 目录树放入 RAM,而驱动器上每个目录的系统调用次数少于一次。
我也会接受指向我的 Windows 或 Linux 库的答案,该库正是执行此操作的。
最佳答案
如果你有足够的内存,那么就继续吧。但我建议您分部分加载磁盘而不是整个磁盘,这也会提高您的时间效率。
关于c++ - 我可以一次将整个 NTFS 目录树读入 RAM 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32551089/