[英]Move files with specific date using Powershell Get-ChildItem
[英]How to get specific files using Get-ChildItem
我正在尝试获取文件名中具有今天日期的所有文件。 但是,当我运行此命令时,出现以下错误:
#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:路径中的非法字符。 在第2行:char:1 + Get-ChildItem -File -path $ source -Filter $ file *
任何帮助,将不胜感激。 我在过滤器中使用$file*
,因为文件扩展名可能不同。
您的问题是使用| Out-String
| 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
... | Out-String
追加一个换行符:
PS> (get-date -uformat "%Y-%m-%d" | Out-String).EndsWith("`n")
True
尝试这个:
#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 + ".*"
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
Get-ChildItem -File -path $source -Filter $file
目前,PowerShell正在尝试将名为$file*
的变量传递给Get-ChildItem
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.