我正在尝试获取文件名中包含今天日期的所有文件。但是,当我运行此命令时,出现以下错误:
#Share location
$source = "U:\Data\*"
#Sharepoint file location
$prefix = "file_name_"
#Date Info
$date = get-date -uformat "%Y-%m-%d" | Out-String
$file = $prefix + $date
Get-ChildItem -File -path $source -Filter $file*
Get-ChildItem : Illegal characters in path. At line:2 char:1 + Get-ChildItem -File -path $source -Filter $file*
如有任何帮助,我们将不胜感激。我在过滤器中使用 $file*
因为文件扩展名可能不同。
最佳答案
您的问题是使用| Out-String
这不仅在您的情况下是不必要的,而且附加尾随换行符,这是问题的原因。
仅使用:
$date = get-date -uformat "%Y-%m-%d" # Do NOT use ... | Out-String
get-date -uformat "%Y-%m-%d"
直接返回一个[string]
。
可选的故障排除提示:
要验证 get-date -uformat "%Y-%m-%d"
是否输出 [string]
实例:
PS> (get-date -uformat "%Y-%m-%d").GetType().FullName
System.String
验证... | Out-String
附加换行符:
PS> (get-date -uformat "%Y-%m-%d" | Out-String).EndsWith("`n")
True
关于powershell - 如何使用 Get-ChildItem 获取特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48890112/