ディレクトリには、次のような名前のさまざまなファイルがあります。
first_file_123456.jpg 5 * second_file_246531 (2).jpg
私がやろうとしているのは、これらのファイルを取得して次のように名前を変更できる PowerShell スクリプトを手に入れることです。
123456.jpg 246531 (2).jpg
最後のアンダースコアとそれに続くすべてのテキストを削除してファイルの名前を変更し、エンタープライズ リソース プランニング システムの項目番号と一致させたいと考えています。このシステムはかなり古い (2004 テクノロジ) ため、その側からの自動化は終了しています。
私がこれまでに配線しようとしたが、正しく動作していないように見えるのは次のとおりです。
Get-ChildItem -Recurse -filter *_* | `
Foreach-Object {
$oldName = $_.Name
$pos = $oldName.LastIndexOf("_")
$newName = $oldName.Substring($pos + 1)
if (Test-Path $newName) {
# This is where I get lost - if it runs into a duplicate file name
# how can I make the name unique
}
#write-host $_.fullname
write-host $oldName renamed To: $newName | Out-File renamelog.txt
#rename-item $_.FullName -NewName $newName
}
実際に何かを実行するコマンドをコメントアウトして、出力を確認しました。