Windows PowerShell文件重命名
如果文件名在文件重命名过程中已经包含字符串,如何忽略文件重命名 我的例子是:Windows PowerShell文件重命名,windows,powershell,rename,file-rename,powershell-4.0,Windows,Powershell,Rename,File Rename,Powershell 4.0,如果文件名在文件重命名过程中已经包含字符串,如何忽略文件重命名 我的例子是: Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" } 我只想重命名文件名中没有字符串“TC”的文件-例如: Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.cs
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" }
我只想重命名文件名中没有字符串“TC”的文件-例如:
Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" |
?{!($_.fullname -match "TC\.csv")} |
Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" }
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" }
作为一个更简单、更直接的解决方案,您可以只过滤到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")
}
}
或者将其传递到Where筛选器,然后再传递到重命名(注意,此特定语法需要PS v3):
Get ChildItem | Where Object{$\ Basename-不象“*TC”}
?@Mathias-谢谢,修复了。如何准备条件以仅复制文件名中当前日期和当前日期前一天的文件?我当前的代码是:复制项-路径“D:\Shares\WinCAP Data\DAYPROT\OFS-222_2*.csv”-目标\\Oracle\MPIt也可能是当前文件创建日和前一个创建日的条件。例如,我的文件看起来像:abc27.11.2014TC.csv。我在当前问题的答案中加入了一个日期过滤器,逻辑与文件副本相同。你可以问另一个问题,特别是如何对文件副本执行此操作,但我怀疑它将作为副本关闭。