36

Rename-Itemコマンドレットを使用して次のことを実行しようとしています。

「 *.txt」ファイルを含むフォルダーがあります。1.txt、2.txtなどとしましょう...

いくつかのプレフィックスを持つファイルをコピーし、それらの名前を *.txt に変更し、既存のファイルがあれば上書きしたいと考えています。

例:

フォルダ: c:\TEST

ファイル: 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

rename-item を使用していずれかをフィルタリングindex.*.txtし、それらの名前を に変更し*.txt、同じファイルが存在する場合はそれを置き換えます。

君たちありがとう

4

2 に答える 2

50

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

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
}
于 2015-08-31T15:15:12.747 に答える
11

rename-item -force を試しましたが、うまくいきませんでした。しかし、私は move-item -force を行うことができ、うまくいきました

于 2015-08-31T13:19:54.333 に答える