![](/img/trans.png)
[英]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.