gpt4 book ai didi

powershell - 如何使用PowerShell一次加载和处理文件

转载 作者:行者123 更新时间:2023-12-03 00:30:55 24 4
gpt4 key购买 nike

我得到了以下脚本,用于加载约十万个.doc文件并在它们上运行一个程序。根据输出,文件被分组到文件夹中。我在很少文件的本地目录上测试了脚本,它可以按预期工作。

但是,当从大型文件库加载时,脚本会打印“Loading Files ....”并停留在该位置。似乎脚本正在等待,直到它从语料库加载了所有文件。如果是这种情况,是否可以同时加载和处理一个文件?

如果您也可以对效率方面发表意见,那将是很棒的。

$path = "\\Storage\100kCorpus"
$filter = "*.doc"
$count = 0
Write-Host "Loading files....";
$files = @(get-childitem -recurse -path $path -filter $filter)
Write-Host "files loaded";
foreach ($file in $files) {
$count ++
Write-Host "$file.FullName";
$out = & "D:\Test\doc\Verify.exe" /i:$file.FullName
$failed_file_location="D:\Test\doc\2875555\$out";
if (($out -ne "passed") -and !(Test-Path -path $failed_file_location )){
[IO.Directory]::CreateDirectory($failed_file_location)
Copy-Item $file $failed_file_location
}
}

Write-Host "There are $count files with the pattern $filer in folder $path"

最佳答案

如果您通过管道传输get-childitem的输出,而不是将其保存到数组,即它将按照您想要的方式工作。

get-childitem -recurse -path $path -filter $filter | % {
$file = $_
$count ++
# etc ...
}

请注意, $file = $_只是这样,您不必过多修改脚本。

在效率方面,我没有什么要说的,除了以这种方式,您还避免将所有文件对象存储到数组( $files)中,因此此版本至少避免了不必要的操作。

关于powershell - 如何使用PowerShell一次加载和处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10515606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com