teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

追記

2020/08/20 22:35

投稿

yureighost
yureighost

スコア2183

answer CHANGED
@@ -14,6 +14,16 @@
14
14
  -Recurseの前にディレクトリ名を入れていただければそこから、
15
15
  無指定ならpowershellの現在のディレクトリから、
16
16
  再帰的に配下のサブディレクトリ内のファイルのみをリネームします。
17
+
18
+ **再追記**
19
+ ikadzuchiさんご指摘ありがとうございます。
20
+ 正規表現に問題がありますね。
21
+ とりあえず例で挙げられてるのだけ処理できるように書いたので。
22
+ 具体的には一番右のピリオドより右のアンダースコアより右の文字を切り捨てる。
23
+ という正規表現に修正しました。
24
+ これならば
25
+ test.test_bk_test.txt_bk2_bk_bn → test.test_bk_test.txt
26
+ こういう置換ができます。
17
27
  ```powershell
18
- Get-ChildItem -Recurse * | ? { ! $_.PSIsContainer } | rename-item -NewName { $_.name -replace '([^_]+)(?:_bk)+','$1' }
28
+ Get-ChildItem renamefile -Recurse * | ? { ! $_.PSIsContainer } | rename-item -NewName { $_.name -replace '(.+.[^_]+)_.+','$1' }
19
29
  ```

2

誤字の修正

2020/08/20 22:35

投稿

yureighost
yureighost

スコア2183

answer CHANGED
@@ -14,6 +14,6 @@
14
14
  -Recurseの前にディレクトリ名を入れていただければそこから、
15
15
  無指定ならpowershellの現在のディレクトリから、
16
16
  再帰的に配下のサブディレクトリ内のファイルのみをリネームします。
17
- ```python
17
+ ```powershell
18
18
  Get-ChildItem -Recurse * | ? { ! $_.PSIsContainer } | rename-item -NewName { $_.name -replace '([^_]+)(?:_bk)+','$1' }
19
19
  ```

1

追記

2020/08/20 10:37

投稿

yureighost
yureighost

スコア2183

answer CHANGED
@@ -4,4 +4,16 @@
4
4
  ```powershell
5
5
  Get-ChildItem -Name | rename-item -NewName { $_ -replace '([^_]+)(?:_bk)+','$1' }
6
6
  ```
7
- これを実行していただければリネームできるはずです。
7
+ これを実行していただければリネームできるはずです。
8
+
9
+ **追記**
10
+ そのエラーはリネーム前と後が同じ名前を指定すると出るエラーです。
11
+ 置換処理を入れても名前が変わらないファイルがあったってことですね。
12
+
13
+ そしてディレクトリを再帰的に処理するコマンドはこれです。
14
+ -Recurseの前にディレクトリ名を入れていただければそこから、
15
+ 無指定ならpowershellの現在のディレクトリから、
16
+ 再帰的に配下のサブディレクトリ内のファイルのみをリネームします。
17
+ ```python
18
+ Get-ChildItem -Recurse * | ? { ! $_.PSIsContainer } | rename-item -NewName { $_.name -replace '([^_]+)(?:_bk)+','$1' }
19
+ ```