Powershell 递归地迭代文件
我有一个powershell脚本,它迭代来自某个网络目录的文件。在这个目录中,大约有190个子目录,每个子目录包含几个文件 我用它来执行工作:Powershell 递归地迭代文件,powershell,loops,filesystems,Powershell,Loops,Filesystems,我有一个powershell脚本,它迭代来自某个网络目录的文件。在这个目录中,大约有190个子目录,每个子目录包含几个文件 我用它来执行工作: get-childitem -Path $pathFilesOrig -Recurse -Force -Include *.pdf, *.csv | ForEach { write-host "INFO:File name: $_" } 但是,它不会在某些目录中循环,而其中有有效的文件,访问权限也很好(我可以通过windows资
get-childitem -Path $pathFilesOrig -Recurse -Force -Include *.pdf, *.csv | ForEach {
write-host "INFO:File name: $_"
}
但是,它不会在某些目录中循环,而其中有有效的文件,访问权限也很好(我可以通过windows资源管理器手动访问这些文件)
我尝试了以下方法来检查是否至少可以迭代每个目录:
get-childitem -Path $pathFilesOrig -Recurse -Force | ?{ $_.PSIsContainer } | ForEach {
write-host "INFO:Dir name: $_"
}
get-childitem -Path $pathFilesOrig -Recurse -Force | ?{ $_.PSIsContainer } | ForEach {
write-host "INFO:Dir name: $_"
$tmpDirName = $pathFilesOrig + $_.Name
get-childitem -Path $tmpDirName | ForEach {
Write-Host "INFO:File name: $_"
当我这样做时,脚本会显示每个目录
现在我要列出每个目录中的文件:
get-childitem -Path $pathFilesOrig -Recurse -Force | ?{ $_.PSIsContainer } | ForEach {
write-host "INFO:Dir name: $_"
}
get-childitem -Path $pathFilesOrig -Recurse -Force | ?{ $_.PSIsContainer } | ForEach {
write-host "INFO:Dir name: $_"
$tmpDirName = $pathFilesOrig + $_.Name
get-childitem -Path $tmpDirName | ForEach {
Write-Host "INFO:File name: $_"
在这种情况下,同样地,一些目录丢失了
甚至没有第二行write host“INFO:Dir name:$\ux”
的结果,该行以前工作正常
我不明白,我肯定错过了一些东西
感谢您的帮助。我怀疑您的问题与提供者收集所有对象后过滤项目的方式有关,而不是在枚举时(即首先获取对象的时间)处理项目的方式。通过仅使用
-File
参数枚举文件,可以进一步提高性能
#需要-版本3
获取ChildItem-Path$pathFilesOrig-Filter*.pdf、*.csv-File-Force-Recurse
如果此命令无法解决您的问题,我怀疑您确实存在权限问题,因为
-Force
将获得隐藏目录,但不会取代access。谢谢您的回复。不幸的是,我有一个较旧版本的powershell,无法在此服务器上更改它。在我的版本J中,我只能过滤一件事,这就是为什么我使用Include。现在,我将通过将powershell封装在另一个脚本(bash)中来绕过我的问题,该脚本将完成清单。@LaurentC。v5.1在2008R2上受支持(我假设您在上)。我强烈建议从安全的角度出发。