重命名项目并覆盖(如果文件名存在) [英] rename-item and override if filename exists

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

问题描述

我正在尝试使用Rename-Item cmdlet执行以下操作:

I am trying to use the Rename-Item cmdlet to do the following:

我有一个包含" *.txt "文件的文件夹.假设1.txt,2.txt等...

I have folder which contains "*.txt" files. Let's say 1.txt, 2.txt etc...

我想复制带有一些前缀的文件,将它们重命名为* .txt并覆盖所有现有文件(如果有的话).

I want to copy files that have some prefix, rename them to *.txt and override any existing files if there are any.

示例:

文件夹:c:\ TEST

folder: c:\TEST

文件:1.txt,2.txt,3.txt,4.txt,5.txt,index.1.txt,index.2.txt,index.3.txt,index.4.txt,索引.5.txt

files : 1.txt, 2.txt, 3.txt, 4.txt, 5.txt, index.1.txt, index.2.txt, index.3.txt, index.4.txt, index.5.txt

我想使用重命名项过滤任何 index.*.txt ,并将它们重命名为 *.txt ,如果存在相同文件,请替换它.

I want to use rename-item to filter any index.*.txt and rename them to *.txt, and if the same file exists, replace it.

谢谢你们

推荐答案

如@lytledw所述,Move-Item -Force非常适合重命名和覆盖文件.

As noted by @lytledw, Move-Item -Force works great for renaming and overwriting files.

要替换名称的index.部分,可以使用-replace正则表达式运算符:

To replace the index. part of the name, you can use the -replace regex operator:

Get-ChildItem index.*.txt |ForEach-Object {
    $NewName = $_.Name -replace "^(index\.)(.*)",'$2'
    $Destination = Join-Path -Path $_.Directory.FullName -ChildPath $NewName
    Move-Item -Path $_.FullName -Destination $Destination -Force
}

这篇关于重命名项目并覆盖(如果文件名存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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