gpt4 book ai didi

powershell - 如何使用Get-ChildItem获取特定文件

转载 作者:行者123 更新时间:2023-12-03 01:05:55 25 4
gpt4 key购买 nike

我正在尝试获取文件名中具有今天日期的所有文件。但是,当我运行此命令时,出现以下错误:

#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/

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