简体   繁体   English

PowerShell-根据文件名和扩展名过滤子项

[英]PowerShell - Filter childitem based off filename and extension

I am attempting to filter a result based off the filename with a variable for the previous day and the file extension. 我正在尝试根据前一天的变量和文件扩展名来过滤基于文件名的结果。

I have attempted to filter using different methods but the latest is the closes I have gotten. 我曾尝试使用不同的方法进行过滤,但最新的是我获得的收盘价。

$Date = Get-Date #-Format "yyyyMMdd"
$DateAdd = (Get-Date).AddDays(-1)
$DateStr = '{0:yyyyMMdd}' -f $DateAdd

$srcRoot = "U:\test\Processed"  
$exRoot = "U:\test\Extract" 


Get-ChildItem $srcRoot -Filter *.zip | Where-object -Property Name -Like *$DatStr  | Expand-Archive -DestinationPath $exRoot -Force

As of right now the code gets all the compressed files in the srcRoot directory. 到目前为止,该代码将所有压缩文件都保存在srcRoot目录中。 I am only trying to return the compressed file for the date variable. 我只想返回date变量的压缩文件。

Using recommendation from AdminOfThings 's comment above. 使用上面AdminOfThings的注释中的推荐。 I was able to filter the results as I was attempting. 我可以尝试过滤结果。

Get-ChildItem $srcRoot -Filter "*$DateStr*.zip" 

This returns the correct file after testing. 测试后,这将返回正确的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM