我尝试过滤目录中的对象,如果它们包含在我的过滤器中。我遇到了一个问题,我不想过滤的文件在其名称中也包含我的文件。
我想要过滤的文件看起来像:0000.pdf
,0001.tsv
,0065.png
语法是一样的,我想过滤的每一项,都以"00“开头,包含一个圆点,圆点后面是树形字符,如"png","tif”或“0065.png
”
我不想过滤的文件如下:batman_N_#_0000.xls
、spiderman_N_#_0022.xls
、bane_N_#_0020.png
我的代码看起来像这样:
if($foldercontentz -ilike "00*.*"){
Add-Content -path $destinationFolderSave\0___missingFiles.txt -value $folderDirection
}
有没有更好的方法来过滤它,或者我可以用另一种方法来过滤那些文件,这是可能的吗?或者我必须更改文件。我认为它会过滤文件,因为我的对象每次都以"00“开头,而我不想过滤的文件不会。
编辑:
$pdfFiles = "Desktop\Import\superheros\"
$sidepaths = Get-ChildItem -path $pdfFiles -Directory
$filesfolder = New-Object System.Collections.Generic.List[System.Object]
for ($i = 0; $i -le $sidepaths.count; $i++) {
$filesfolder.add($sidepaths[$i])
}
for ($i = 0; $i -le $filesfolder.count; $i++) {
$folder = $pdfFiles + $filesfolder[$i]
#write-host $folder
$folder | foreach {
$contentfolder = Get-ChildItem $_
$location = $directories[$i]
if ($contentfolder -contains "00*.*" ) {
#Write-Output $contentfolder "haaadadasd" $filesfolder[$i]
Add-Content -path $pdfFiles\0___missingFiles.txt -value $contentfolder
}
# ...
}
# ...
}
发布于 2021-07-19 15:26:27
你在找这样的东西吗?
$rootFolder = 'X:\Desktop\Import\superheros' # path to the superheros folder
$outFile = 'X:\Desktop\Import\0___missingFiles.txt' # full path and filename for the output file
# look for Files with a name beginning with '00' and write their full path and name to the output file.
(Get-ChildItem -Path $rootFolder -File -Filter '00*.*' -Recurse).FullName | Add-Content -Path $outFile
https://stackoverflow.com/questions/68442603
复制