如何解决通过Powershell查找给定文件的完整路径的最快方法?
我需要编写一个Powershell代码段,以便在整个分区上尽快找到给定文件名的完整路径。
为了更好地进行比较,我将以下全局变量用于代码样本:
$searchDir = "c:\"
$searchName = "hosts"
我从使用Get-ChildItem的一小段开始就有了第一个基线:
"get-ChildItem"
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$result = Get-ChildItem -LiteralPath $searchDir -Filter $searchName -File -Recurse -ea 0
write-host $timer.Elapsed.TotalSeconds "sec."
我的SSD上的运行时间为14,8581609秒。
接下来,我尝试运行传统的DIR命令以查看改进:
"dir"
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$result = &cmd /c dir "$searchDir$searchName" /b /s /a-d
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
此过程完成了13,4713342秒。 -不错,但是我们可以更快地得到它吗?
在第三次迭代中,我正在使用ROBOCOPY测试相同的任务。这里是代码示例:
"robocopy"
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$roboDir = [System.IO.Path]::GetDirectoryName($searchDir)
if (!$roboDir) {$roboDir = $searchDir.Substring(0,2)}
$info = [System.Diagnostics.ProcessStartInfo]::new()
$info.FileName = "$env:windir\system32\robocopy.exe"
$info.RedirectStandardOutput = $true
$info.Arguments = " /l ""$roboDir"" null ""$searchName"" /bytes /njh /njs /np /nc /ndl /xjd /mt /s"
$info.UseShellExecute = $false
$info.CreateNoWindow = $true
$info.WorkingDirectory = $searchDir
$process = [System.Diagnostics.Process]::new()
$process.StartInfo = $info
[void]$process.Start()
$process.WaitForExit()
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
或更短的版本(基于良好的评论):
"robocopy v2"
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$fileList = (&cmd /c pushd $searchDir `& robocopy /l "$searchDir" null "$searchName" /ns /njh /njs /np /nc /ndl /xjd /mt /s).trim() -ne ''
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
它比DIR快吗?是的,一点没错!现在,运行时间降至3,2685551秒。 进行此重大改进的主要原因是,在多个并行实例中,ROBOCOPY在多任务模式下以/ mt-swich运行。但是即使没有这个涡轮开关也比DIR快。
任务完成了吗?并非如此-因为我的任务是创建一个Powershell脚本,以尽可能快的速度搜索文件,但是调用ROBOCOPY有点作弊。
接下来,我想看看,使用[System.IO.Directory]将会有多快。首先尝试使用getFiles和getDirectory-calls。这是我的代码:
"GetFiles"
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$fileList = [System.Collections.Generic.List[string]]::new()
$dirList = [System.Collections.Generic.Queue[string]]::new()
$dirList.Enqueue($searchDir)
while ($dirList.Count -ne 0) {
$dir = $dirList.Dequeue()
try {
$files = [System.IO.Directory]::GetFiles($dir,$searchName)
if ($files) {$fileList.addRange($file)}
foreach($subdir in [System.IO.Directory]::GetDirectories($dir)) {
$dirList.Enqueue($subDir)
}
} catch {}
}
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
这次运行时间为19,3393872秒。迄今为止最慢的代码。我们可以做得更好吗?现在,这里有一个Enumeration-calls的代码片段供比较:
"EnumerateFiles"
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$fileList = [System.Collections.Generic.List[string]]::new()
$dirList = [System.Collections.Generic.Queue[string]]::new()
$dirList.Enqueue($searchDir)
while ($dirList.Count -ne 0) {
$dir = $dirList.Dequeue()
try {
foreach($file in [System.IO.Directory]::EnumerateFiles($dir,$searchName)) {
$fileList.add($file)
}
foreach ($subdir in [System.IO.Directory]::EnumerateDirectories($dir)) {
$dirList.Enqueue($subDir)
}
} catch {}
}
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
运行时间为19,2068545秒,速度仅稍快一点。
现在让我们看看是否可以通过Kernel32的直接WinAPI调用更快地获得它。 这里的代码。让我们看看,这次有多快:
"WinAPI"
add-type -Name FileSearch -Namespace Win32 -MemberDefinition @"
public struct WIN32_FIND_DATA {
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 14)]
public string cAlternateFileName;
}
[DllImport("kernel32.dll",SetLastError = true,CharSet = CharSet.Ansi)]
public static extern IntPtr FindFirstFile
(string lpFileName,out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll",CharSet = CharSet.Ansi)]
public static extern bool FindNextFile
(IntPtr hFindFile,CharSet = CharSet.Ansi)]
public static extern bool FindClose(IntPtr hFindFile);
"@
$rootDir = 'c:'
$searchFile = "hosts"
$fileList = [System.Collections.Generic.List[string]]::new()
$dirList = [System.Collections.Generic.Queue[string]]::new()
$dirList.Enqueue($rootDir)
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$fileData = new-object Win32.FileSearch+WIN32_FIND_DATA
while ($dirList.Count -ne 0) {
$dir = $dirList.Dequeue()
$handle = [Win32.FileSearch]::FindFirstFile("$dir\*",[ref]$fileData)
[void][Win32.FileSearch]::FindNextFile($handle,[ref]$fileData)
while ([Win32.FileSearch]::FindNextFile($handle,[ref]$fileData)) {
if ($fileData.dwFileAttributes -band 0x10) {
$fullName = [string]::Join('\',$dir,$fileData.cFileName)
$dirList.Enqueue($fullName)
} elseif ($fileData.cFileName -eq $searchFile) {
$fullName = [string]::Join('\',$fileData.cFileName)
$fileList.Add($fullName)
}
}
[void][Win32.FileSearch]::FindClose($handle)
}
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
对我来说,这种方法的结果是非常负面的惊喜。运行时间为17,499286秒。 这比System.IO调用快,但比简单的Get-ChildItem慢。
但是-仍然有希望接近ROBOCOPY带来的超快结果! 对于Get-ChildItem,我们无法进行以多任务模式执行的调用,但对于对于Kernel32调用,我们可以选择使其成为递归函数,并通过嵌入式C#代码对PARALLEL foreach循环中所有子文件夹的每次迭代进行调用。但是该怎么做?
有人知道如何将最后一个代码片段更改为使用parallel.foreach吗? 即使结果可能不如ROBOCOPY快,我也想在此发布这种方法,以获取有关该经典“文件搜索”主题的完整故事书。
请让我知道如何执行并行代码部分。
更新: 为了完整起见,我添加了在Powershell 7上运行的具有更智能的访问处理功能的GetFiles代码的代码和运行时:
"GetFiles PS7"
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$fileList = [system.IO.Directory]::GetFiles(
$searchDir,$searchFile,[IO.EnumerationOptions] @{AttributesToSkip = 'ReparsePoint'; RecurseSubdirectories = $true; IgnoreInaccessible = $true}
)
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
我的系统上的运行时间为9,150673秒。 -比DIR快,但比robocopy慢,在8个内核上执行多任务处理。
更新#2: 在试用了新的PS7功能之后,我想到了这个代码片段,它使用了我的第一个(但很丑?)并行代码方法:
"WinAPI PS7 parallel"
$searchDir = "c:\"
$searchFile = "hosts"
add-type -Name FileSearch -Namespace Win32 -MemberDefinition @"
public struct WIN32_FIND_DATA {
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr,CharSet = CharSet.Ansi)]
public static extern bool FindClose(IntPtr hFindFile);
"@
$rootDir = $searchDir -replace "\\$"
$maxRunSpaces = [int]$env:NUMBER_OF_PROCESSORS
$fileList = [System.Collections.Concurrent.BlockingCollection[string]]::new()
$dirList = [System.Collections.Concurrent.BlockingCollection[string]]::new()
$dirList.Add($rootDir)
$timer = [System.Diagnostics.Stopwatch]::StartNew()
(1..$maxRunSpaces) | ForEach-Object -ThrottleLimit $maxRunSpaces -Parallel {
$dirList = $using:dirList
$fileList = $using:fileList
$fileData = new-object Win32.FileSearch+WIN32_FIND_DATA
$dir = $null
if ($_ -eq 1) {$delay = 0} else {$delay = 50}
if ($dirList.TryTake([ref]$dir,$delay)) {
do {
$handle = [Win32.FileSearch]::FindFirstFile("$dir\*",[ref]$fileData)
[void][Win32.FileSearch]::FindNextFile($handle,[ref]$fileData)
while ([Win32.FileSearch]::FindNextFile($handle,[ref]$fileData)) {
if ($fileData.dwFileAttributes -band 0x10) {
$fullName = [string]::Join('\',$fileData.cFileName)
$dirList.Add($fullName)
} elseif ($fileData.cFileName -eq $using:searchFile) {
$fullName = [string]::Join('\',$fileData.cFileName)
$fileList.Add($fullName)
}
}
[void][Win32.FileSearch]::FindClose($handle)
} until (!$dirList.TryTake([ref]$dir))
}
}
$timer.Stop()
write-host $timer.Elapsed.TotalSeconds "sec."
现在,运行时非常接近robocopy-timing。实际上是4,0809719秒。
不错,但我仍在通过嵌入式C#代码寻找具有parallel.foreach-approach的解决方案,以使其也适用于Powershell v5。
更新#3: 现在这是我在并行运行空间中运行的Powershell 5的最终代码:
$searchDir = "c:\"
$searchFile = "hosts"
"WinAPI parallel"
add-type -Name FileSearch -Namespace Win32 -MemberDefinition @"
public struct WIN32_FIND_DATA {
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr,CharSet = CharSet.Ansi)]
public static extern bool FindClose(IntPtr hFindFile);
"@
$rootDir = $searchDir -replace "\\$"
$maxRunSpaces = [int]$env:NUMBER_OF_PROCESSORS
$fileList = [System.Collections.Concurrent.BlockingCollection[string]]::new()
$dirList = [System.Collections.Concurrent.BlockingCollection[string]]::new()
$dirList.Add($rootDir)
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$runSpaceList = [System.Collections.Generic.List[PSObject]]::new()
$pool = [RunSpaceFactory]::CreateRunspacePool(1,$maxRunSpaces)
$pool.Open()
foreach ($id in 1..$maxRunSpaces) {
$runSpace = [Powershell]::Create()
$runSpace.RunspacePool = $pool
[void]$runSpace.AddScript({
Param (
[string]$searchFile,[System.Collections.Concurrent.BlockingCollection[string]]$dirList,[System.Collections.Concurrent.BlockingCollection[string]]$fileList
)
$fileData = new-object Win32.FileSearch+WIN32_FIND_DATA
$dir = $null
if ($id -eq 1) {$delay = 0} else {$delay = 50}
if ($dirList.TryTake([ref]$dir,$delay)) {
do {
$handle = [Win32.FileSearch]::FindFirstFile("$dir\*",[ref]$fileData)
[void][Win32.FileSearch]::FindNextFile($handle,[ref]$fileData)
while ([Win32.FileSearch]::FindNextFile($handle,[ref]$fileData)) {
if ($fileData.dwFileAttributes -band 0x10) {
$fullName = [string]::Join('\',$fileData.cFileName)
$dirList.Add($fullName)
} elseif ($fileData.cFileName -like $searchFile) {
$fullName = [string]::Join('\',$fileData.cFileName)
$fileList.Add($fullName)
}
}
[void][Win32.FileSearch]::FindClose($handle)
} until (!$dirList.TryTake([ref]$dir))
}
})
[void]$runSpace.addArgument($searchFile)
[void]$runSpace.addArgument($dirList)
[void]$runSpace.addArgument($fileList)
$status = $runSpace.BeginInvoke()
$runSpaceList.Add([PSCustomObject]@{Name = $id; RunSpace = $runSpace; Status = $status})
}
while ($runSpaceList.Status.IsCompleted -notcontains $true) {sleep -Milliseconds 10}
$pool.Close()
$pool.Dispose()
$timer.Stop()
$fileList
write-host $timer.Elapsed.TotalSeconds "sec."
总运行时间为4,8586134秒。比PS7版本慢一些,但仍然比任何DIR或Get-ChildItem版本都快。 ;-)
最终解决方案: 最后,我能够回答自己的问题。这是最终代码:
"WinAPI parallel.foreach"
add-type -TypeDefinition @"
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
public class FileSearch {
public struct WIN32_FIND_DATA {
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr,CharSet = CharSet.Ansi)]
public static extern bool FindClose(IntPtr hFindFile);
static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
public static class Globals {
public static BlockingCollection<string> resultFileList {get;set;}
}
public static BlockingCollection<string> GetTreeFiles(string path,string searchFile) {
Globals.resultFileList = new BlockingCollection<string>();
List<string> dirList = new List<string>();
searchFile = @"^" + searchFile.Replace(@".",@"\.").Replace(@"*",@".*").Replace(@"?",@".") + @"$";
GetFiles(path,searchFile);
return Globals.resultFileList;
}
static void GetFiles(string path,string searchFile) {
path = path.EndsWith(@"\") ? path : path + @"\";
List<string> dirList = new List<string>();
WIN32_FIND_DATA fileData;
IntPtr handle = INVALID_HANDLE_VALUE;
handle = FindFirstFile(path + @"*",out fileData);
if (handle != INVALID_HANDLE_VALUE) {
FindNextFile(handle,out fileData);
while (FindNextFile(handle,out fileData)) {
if ((fileData.dwFileAttributes & 0x10) > 0) {
string fullPath = path + fileData.cFileName;
dirList.Add(fullPath);
} else {
if (Regex.IsMatch(fileData.cFileName,searchFile,RegexOptions.IgnoreCase)) {
string fullPath = path + fileData.cFileName;
Globals.resultFileList.TryAdd(fullPath);
}
}
}
FindClose(handle);
Parallel.ForEach(dirList,(dir) => {
GetFiles(dir,searchFile);
});
}
}
}
"@
[fileSearch]::GetTreeFiles($searchDir,'hosts')
现在,最终运行时间比robocopy快了3,2536388秒。 我还在解决方案中添加了该代码的优化版本。
解决方法
tl;博士:
此答案不是试图解决所要求的并行问题,但是:
- 单个递归
[IO.Directory]::GetFiles()
调用可能足够快,但是请注意,如果涉及不可访问的目录,这仅是PowerShell [Core] v6.2 +中的一个选项:
# PowerShell [Core] v6.2+
[IO.Directory]::GetFiles(
$searchDir,$searchFile,[IO.EnumerationOptions] @{ AttributesToSkip = 'ReparsePoint'; RecurseSubdirectories = $true; IgnoreInaccessible = $true }
)
- 实用地说(例如,不进行编码练习),调用
robocopy
是一种完全合法的方法-假设您只需要在 Windows 上运行-就像(请注意,con
是未使用的target-directory参数的伪参数):
(robocopy $searchDir con $searchFile /l /s /mt /njh /njs /ns /nc /ndl /np).Trim() -ne ''
前面几点:
-
但是调用ROBOCOPY有点作弊。
- 可以说,使用.NET API / WinAPI调用与调用RoboCopy之类的外部实用程序(例如
robocopy.exe /l ...
)一样多。毕竟,调用外部程序是任何Shell(包括PowerShell)的核心任务(并且System.Diagnostics.Process
及其PowerShell包装器Start-Process
都不需要)。 就是说,虽然在这种情况下不是问题,但是在调用外部程序时,您确实失去了传递和接收对象的能力,并且进程内操作通常更快。
- 可以说,使用.NET API / WinAPI调用与调用RoboCopy之类的外部实用程序(例如
-
为定时执行命令(测量性能),PowerShell为
System.Diagnostics.Stopwatch
:Measure-Command
cmdlet提供了高级包装。 -
这种性能度量会波动,因为PowerShell作为一种动态解析的语言,会使用大量缓存,这些缓存在首次填充时会产生开销,而您通常不知道发生的时间-请参见{{3} }以获取背景信息。
-
此外,遍历文件系统的长时间运行的命令会受到同时运行的其他进程的干扰,并且是否已经从先前的运行中缓存了文件系统信息,很大的差异。
-
以下比较在
Measure-Object
this GitHub issue周围使用了更高级别的包装,这使得比较多个命令的相对运行时性能变得容易。
加快PowerShell代码速度的关键是使实际的PowerShell代码最小化,并将尽可能多的工作分担给.NET方法调用/(编译的)外部程序。
以下是以下内容的对比:
-
Get-ChildItem
(为了对比,我们知道它太慢了) -
robocopy.exe
-
对
Time-Command
function的单个递归调用,尽管是单线程,但 可能足够快达到您的目的。- 注意:以下调用使用的功能仅在 .NET Core 2.1 + 中可用,因此仅在 PowerShell [Core] v6.2 +中可用强>。 该API的.NET Framework版本不允许忽略不可访问目录(由于缺少权限),如果遇到此类目录,枚举将失败。
$searchDir = 'C:\' #'# dummy comment to fix syntax highlighting
$searchFile = 'hosts'
# Define the commands to compare as an array of script blocks.
$cmds =
{
[IO.Directory]::GetFiles(
$searchDir,[IO.EnumerationOptions] @{ AttributesToSkip = 'ReparsePoint'; RecurseSubdirectories = $true; IgnoreInaccessible = $true }
)
},{
(Get-ChildItem -Literalpath $searchDir -File -Recurse -Filter $searchFile -ErrorAction Ignore -Force).FullName
},{
(robocopy $searchDir con $searchFile /l /s /mt /njh /njs /ns /nc /ndl /np).Trim() -ne ''
}
Write-Verbose -vb "Warming up the cache..."
# Run one of the commands up front to level the playing field
# with respect to cached filesystem information.
$null = & $cmds[-1]
# Run the commands and compare their timings.
Time-Command $cmds -Count 1 -OutputToHost -vb
在运行PowerShell Core 7.1.0-preview.7的2核Windows 10 VM上,我得到以下结果;数量取决于很多因素(不仅是文件数量)而异,但应该提供相对性能的一般含义(列Factor
)。
请注意,由于文件系统缓存是有意预热的,因此与没有缓存信息的运行相比,给定计算机的数量将过于乐观。
如您所见,在这种情况下,PowerShell [Core] [System.IO.Directory]::GetFiles()
调用实际上胜过多线程robocopy
调用。
VERBOSE: Warming up the cache...
VERBOSE: Starting 1 run(s) of:
[IO.Directory]::GetFiles(
$searchDir,[IO.EnumerationOptions] @{ AttributesToSkip = 'ReparsePoint'; RecurseSubdirectories = $true; IgnoreInaccessible = $true }
)
...
C:\Program Files\Git\etc\hosts
C:\Windows\WinSxS\amd64_microsoft-windows-w..ucture-other-minwin_31bf3856ad364e35_10.0.18362.1_none_079d0d71e24a6112\hosts
C:\Windows\System32\drivers\etc\hosts
C:\Users\jdoe\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\etc\hosts
VERBOSE: Starting 1 run(s) of:
(Get-ChildItem -Literalpath $searchDir -File -Recurse -Filter $searchFile -ErrorAction Ignore -Force).FullName
...
C:\Program Files\Git\etc\hosts
C:\Users\jdoe\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\etc\hosts
C:\Windows\System32\drivers\etc\hosts
C:\Windows\WinSxS\amd64_microsoft-windows-w..ucture-other-minwin_31bf3856ad364e35_10.0.18362.1_none_079d0d71e24a6112\hosts
VERBOSE: Starting 1 run(s) of:
(robocopy $searchDir con $searchFile /l /s /mt /njh /njs /ns /nc /ndl /np).Trim() -ne ''
...
C:\Program Files\Git\etc\hosts
C:\Windows\WinSxS\amd64_microsoft-windows-w..ucture-other-minwin_31bf3856ad364e35_10.0.18362.1_none_079d0d71e24a6112\hosts
C:\Windows\System32\drivers\etc\hosts
C:\Users\jdoe\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\etc\hosts
VERBOSE: Overall time elapsed: 00:01:48.7731236
Factor Secs (1-run avg.) Command
------ ----------------- -------
1.00 22.500 [IO.Directory]::GetFiles(…
1.14 25.602 (robocopy /l $searchDir NUL $searchFile /s /mt /njh /njs /ns /nc /np).Trim() -ne ''
2.69 60.623 (Get-ChildItem -Literalpath $searchDir -File -Recurse -Filter $searchFile -ErrorAction Ignore -Force).FullName
,
这是我创建的最终代码。现在的运行时间为2,8627695秒。 与所有子目录的Parallel.ForEach相比,将独占性限制为逻辑核的数量具有更好的性能。
您可以将每次匹配的完整FileInfo-Object返回到生成的BlockingCollection中,而不是仅返回文件名。
# powershell-sample to find all "hosts"-files on Partition "c:\"
cls
Remove-Variable * -ea 0
[System.GC]::Collect()
$ErrorActionPreference = "stop"
$searchDir = "c:\"
$searchFile = "hosts"
add-type -TypeDefinition @"
using System;
using System.IO;
using System.Linq;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
public class FileSearch {
public struct WIN32_FIND_DATA {
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst = 14)]
public string cAlternateFileName;
}
[DllImport("kernel32.dll",SetLastError = true,CharSet = CharSet.Ansi)]
static extern IntPtr FindFirstFile
(string lpFileName,out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll",CharSet = CharSet.Ansi)]
static extern bool FindNextFile
(IntPtr hFindFile,CharSet = CharSet.Ansi)]
static extern bool FindClose(IntPtr hFindFile);
static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
static BlockingCollection<string> dirList {get;set;}
static BlockingCollection<string> fileList {get;set;}
public static BlockingCollection<string> GetFiles(string searchDir,string searchFile) {
bool isPattern = false;
if (searchFile.Contains(@"?") | searchFile.Contains(@"*")) {
searchFile = @"^" + searchFile.Replace(@".",@"\.").Replace(@"*",@".*").Replace(@"?",@".") + @"$";
isPattern = true;
}
fileList = new BlockingCollection<string>();
dirList = new BlockingCollection<string>();
dirList.Add(searchDir);
int[] threads = Enumerable.Range(1,Environment.ProcessorCount).ToArray();
Parallel.ForEach(threads,(id) => {
string path;
IntPtr handle = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA fileData;
if (dirList.TryTake(out path,100)) {
do {
path = path.EndsWith(@"\") ? path : path + @"\";
handle = FindFirstFile(path + @"*",out fileData);
if (handle != INVALID_HANDLE_VALUE) {
FindNextFile(handle,out fileData);
while (FindNextFile(handle,out fileData)) {
if ((fileData.dwFileAttributes & 0x10) > 0) {
string fullPath = path + fileData.cFileName;
dirList.TryAdd(fullPath);
} else {
if (isPattern) {
if (Regex.IsMatch(fileData.cFileName,searchFile,RegexOptions.IgnoreCase)) {
string fullPath = path + fileData.cFileName;
fileList.TryAdd(fullPath);
}
} else {
if (fileData.cFileName == searchFile) {
string fullPath = path + fileData.cFileName;
fileList.TryAdd(fullPath);
}
}
}
}
FindClose(handle);
}
} while (dirList.TryTake(out path));
}
});
return fileList;
}
}
"@
$fileList = [fileSearch]::GetFiles($searchDir,$searchFile)
$fileList
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。