如何忽略文件重命名如果在文件重命名過程中文件名已包含字符串。PowerShell文件重命名
我的例子:
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" }
我想在文件名重命名只是文件,而不字符串 「TC」 - 例如:
abc.csv
重命名
defTC.csv
不想重命名
如何忽略文件重命名如果在文件重命名過程中文件名已包含字符串。PowerShell文件重命名
我的例子:
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" }
我想在文件名重命名只是文件,而不字符串 「TC」 - 例如:
abc.csv
重命名
defTC.csv
不想重命名
您可以過濾不需要用正則表達式重命名的文件:
?{!($_.fullname -match "TC\.csv")}
所以,你的例子:
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" |
?{!($_.fullname -match "TC\.csv")} |
Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" }
納入日期過濾器進入這一如下:
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" |
?{!($_.fullname -match "TC\.csv") -and $_.CreationTime -ge (get-date).AddDays(-1).Date} |
Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" }
'-match「TC \ .csv」' –
@Mathias - 謝謝,修正。 – nimizen
如何準備條件在文件名當前和當天前一天覆制文件?我目前的代碼是:Copy-Item -Path「D:\ Shares \ WinCAP Data \ DAYPROT \ OFS-222_2 \ *。csv」-Destination \\ Oracle \ MP –
作爲一個更簡單,更直接的解決方案,你可以只過濾成一個foreach循環和使用一個IF聲明。可能效率不高,但更容易維護和適應其他用途。
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Foreach-Object {
if ($_.Name -notlike "*TC*") {
Rename-Item $_ ($_.Name -Replace "\.csv", "TC.csv")
}
}
或者把它傳遞到哪裏過濾器,然後重命名(請注意這個特殊的語法要求PS V3):
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Where Name -notlike "*TC*" | Rename-Item $_ ($_.Name -Replace "\.csv", "TC.csv")
'GET-ChildItem |位置對象{$ _基名-notlike 「* TC」}'? –