社内SEの話

日々起きたことの記録用

Batch rename by replacing file names

↓プログラミングで副業を考えたらこちら↓

There are times when you want to batch rename files with a specific rule in a large number of files such as logs.

Renaming one by one is troublesome, so use Powershell to finish it in an instant.

Composing the commands is not difficult, so it's useful to remember.

Program

Get-ChildItem -File "Search condition to replace" | Rename-Item -NewName { $_.Name -replace 'Search condition to replace','Afterreplacement' }  

sample

"-"

Get-ChildItem -File -path "*-*" | Rename-Item -NewName { $_.Name -replace '-','' }

Description

1) Get-ChildItem:

Only the file is extracted by attaching File Get-File. If you want to remove the folder, remove -File.

By the way, the ls command is an alias for Get-ChildItem, so ls will give the same result.

-path Sets the search condition to extract.

learn.microsoft.com

2)Rename-Item -NewName : Replace the specified characters

Rename-Item

learn.microsoft.com

Normally Rename-Item File before change File after change , since it is a pipeline, it works without specifying the pre-change file.

Detailed replacement method

learn.microsoft.com

learn.microsoft.com