Created
September 28, 2013 04:37
-
-
Save TearTheSky/6738397 to your computer and use it in GitHub Desktop.
リネームの機会が何回かあったため、PowerShellでリネームツール作成に挑戦してみました。 フォルダ内のファイル名を一括で変換してくれるすごいやつになる予定です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#====================================================================== | |
# author : TearTheSky | |
# フォルダ内のファイル名を一括で変換してくれるすごいやつ(予定) | |
#====================================================================== | |
#---------------------------------------------------------------------- | |
# - - - フォルダ選択ダイアログの表示 - - - | |
#---------------------------------------------------------------------- | |
#フォルダ選択ダイアログの表示 | |
$shell = New-Object -com Shell.Application | |
$folderPath = $shell.BrowseForFolder(0,"対象フォルダーを選択してください",0,"D:") | |
if($folderPath -eq $null){exit} | |
$pathName = $folderPath.Self.Path | |
#---------------------------------------------------------------------- | |
#---------------------------------------------------------------------- | |
# - - - 変更前文字列を入力させる - - - | |
#---------------------------------------------------------------------- | |
# Microsoft.VisualBasicアセンブリを有効化 | |
[void][System.Reflection.Assembly]::Load("Microsoft.VisualBasic, Version=8.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a") | |
# 入力ボックスを表示(変換前文字列を入力) | |
$beforeKeyword = [Microsoft.VisualBasic.Interaction]::InputBox("変換対象の文字列を入力", "ファイル名一括変換") | |
# 入力値の文字列長が0以上である(空でない)場合に、結果を表示 | |
if($beforeKeyword.Length -lt 0){ | |
write-host "変換対象の文字列が入力されていません。処理を終了します。" | |
return | |
} | |
# 入力ボックスを表示(変換後文字列を入力) | |
$afterKeyword = [Microsoft.VisualBasic.Interaction]::InputBox("変換後の文字列を入力してください。", "ファイル名一括変換") | |
# 入力値の文字列長が0以上である(空でない)場合に、結果を表示 | |
if($afterKeyword.Length -lt 0){ | |
write-host "変換後の文字列が入力されていません。処理を終了します。" | |
return | |
} | |
#---------------------------------------------------------------------- | |
$a = new-object -comobject wscript.shell | |
$intAnswer = $a.popup("連番を付与しますか?(3桁)", 0, "ファイル名一括変換", 4) | |
If ($intAnswer -eq 6) { | |
#yesの場合 | |
$renban = 1 | |
#ファイル名変更対象のファイルが存在するフォルダへ移動 | |
Set-Location $pathName | |
#フォルダ内のファイル名を取得 | |
Get-ChildItem $pathName | foreach-object { | |
#ユーザが入力した変更前文字列を変更後文字列へ置換 | |
Rename-Item $_ -NewName ($_ -replace $beforeKeyword, $afterKeyword) | |
} | |
#フォルダ内のファイル名を再取得 | |
Get-ChildItem $pathName | foreach-object { | |
#ファイル名の末尾に連番を付与したファイル名へ変更 | |
Rename-Item -Path $_.FullName -NewName ($_.basename + "_" + $renban.ToString("000") + $_.Extension) | |
$renban++ | |
} | |
} else { | |
#noの場合 | |
#ファイル名変更対象のファイルが存在するフォルダへ移動 | |
Set-Location $pathName | |
#ユーザが入力した変更前文字列を変更後文字列へ置換 | |
Get-ChildItem $pathName | foreach-object { Rename-Item $_ -NewName ($_ -replace $beforeKeyword, $afterKeyword) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment