PowerShell文件重命名 [英] PowerShell File rename

查看:107
本文介绍了PowerShell文件重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文件重命名过程中文件名已包含字符串,如何忽略文件重命名.

How to ignore file rename if the filename already contains string during file rename process.

我的例子:

Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Rename-Item -NewName { $_.Name -replace "\.csv", "TC.csv" }

我只想重命名文件名中没有字符串"TC"的文件-例如:

I would like to rename just files without string "TC" in filename - for example:

abc.csv 重命名
defTC.csv 不想重命名

推荐答案

作为一个更简单,更直接的解决方案,您可以过滤到ForEach循环中并使用IF语句.可能效率不高,但更易于维护和适应其他用途.

As a simpler and more straightforward solution you could just filter into a ForEach loop and use an IF statement. Probably not quite as efficient but easier to maintain and adapt for other uses.

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):

Alternatively pass it to a Where filter and then to the rename (note this particular syntax requires PS v3):

Get-ChildItem "D:\Shares\WinCAP Data\DAYPROT\OFS-222_2\*.csv" | Where Name -notlike "*TC*" | Rename-Item $_ ($_.Name -Replace "\.csv", "TC.csv")

这篇关于PowerShell文件重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆