PowerShell小僧:スペースを含むフォルダ名 [コンピューター]
ファイル名のみならず、フォルダ名に含まれるスペースもアンダーバーに変更するようにスクリプトを書き換えてみました。
フォルダ名を変更した場合、ファイル・フォルダの一覧を取得し直す様にしています。【nkf_UTF8wBOM2.ps1】
# nkfでファイルの文字コードをUTF-8のBOMありに変更して上書きする。
# フォルダ内のファイル全て変換する
# 起動方法
# C:\aaa\PowerShell\nkf_UTF8wBOM2.ps1 -InFolder1 C:\aaa\bbb
# パラメータ
Param(
[Parameter(Mandatory=$true)]
[String]$InFolder1 # 対象フォルダ
)
# スクリプトがある場所
$ScriptPath1 = Split-Path $MyInvocation.MyCommand.Path -Parent
Write-Host "`$ScriptPath1="$ScriptPath1
# 関数ファイル
$myFunctionFile1 = $ScriptPath1+"\myFunction1_UTF8.ps1"
## 関数読み込み ##
. $myFunctionFile1
$commandPath = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
$stdErrLogTmp = ".\stderr.log"
$stdOutLogTmp = ".\stdout.log"
$myResult1 = $true
if (-not(Test-Path($InFolder1))) {
Write-Host $InFolder1"がありません。"
$myResult1 = $false
}
if ($myResult1 -eq $true) {
$myFlag1 = $true
While($myFlag1 -eq $true){
# $InFolder1内のファイル・フォルダのリストを取得する。
# -Recurseを付けるとサブフォルダも検索する。
$itemList = Get-ChildItem $InFolder1 -Recurse;
foreach($item in $itemList){
if($item.PSIsContainer){
# フォルダの場合の処理
Write-Host($item.Name + 'はフォルダです。');
$NewItem1 = myFRnSpToUBar1 -TargetFile1 $item # フォルダ名の空白をアンダーバーに変換
Write-Host("新しいフォルダ名="+$NewItem1);
if($NewItem1 -ne $item){
# フォルダ名の変更があったため、foreachループを抜けて、ファイル・フォルダのリストを再取得する。
$myFlag1 = $true # whileループは継続
break # foreachを抜ける
}else{
$myFlag1 = $false # foreachループが終了したら、whileループも終了
}
Write-Host "`$myFlag1="$myFlag1
Read-HostDefault "OK?" > $null # 入力待ち
}else{
# ファイルの場合の処理
Write-Host ($item.Name + 'はファイルです。');
$NewItem1 = myFRnSpToUBar1 -TargetFile1 $item # ファイル名の空白をアンダーバーに変換
Write-Host "`$NewItem1=$NewItem1"
Read-HostDefault "OK?" > $null # 入力待ち
$ScriptFile1 = $ScriptPath1 + "\nkf_UTF8wBOM1.ps1 -TargetFile1 $NewItem1 -ScriptPath1 $ScriptPath1"
Write-Host "`$ScriptFile1=$ScriptFile1"
$Argument1 = "-Command $ScriptFile1"
Start-Process -FilePath $commandPath `
-ArgumentList $Argument1 `
-NoNewWindow `
-RedirectStandardOutput $stdOutLogTmp -RedirectStandardError $stdErrLogTmp -Wait
Write-Host "終了"
Get-Content $stdErrLogTmp, $stdOutLogTmp
foreach($strLogPath in @($stdErrLogTmp, $stdOutLogTmp)){
if (Test-Path $strLogPath) {
Remove-Item $strLogPath;
}
}
$myFlag1 = $false # foreachループが終了したら、whileループも終了
}
}
Write-Host "`$myFlag1="$myFlag1
Read-HostDefault "OK?" > $null # 入力待ち
}
}
# 終了
Return $myResult1
【myFunction1_UTF8.ps1(一部)】
# 関数:ファイル名の空白をアンダーバーに変換
function myFRnSpToUBar1{
param (
[Parameter(Mandatory=$true)]
[String]$TargetFile1 # 対象ファイル
)
# ドットソース演算子
# .{}の中の標準出力は戻り値に含めない
.{
if($item.PSIsContainer){
# フォルダの場合
$NewFileName1 = (Split-Path ($TargetFile1) -Leaf) -replace '\s','_'
}else {
# ファイルの場合
$NewFileName1 = (Get-ChildItem -Path ($TargetFile1)).Name -replace '\s','_'
# $NewFileName1 = (Get-ChildItem -Path $TargetFile1).Name -replace '\s','_'
}
$ParentPath1 = Split-Path $TargetFile1 -Parent
$NewFilePath1 = $ParentPath1 + "\" + $NewFileName1
Write-Host "`$NewFilePath1=$NewFilePath1"
Read-HostDefault "OK?" > $null # 入力待ち
# ファイル名置換(空白→アンダーバー)
if($NewFilePath1 -ne $TargetFile1){
get-item $TargetFile1|Rename-Item -NewName { $_.Name -replace '\s','_' }
}
}>$null
Return $NewFilePath1
}
フォルダ名を変更した場合、ファイル・フォルダの一覧を取得し直す様にしています。【nkf_UTF8wBOM2.ps1】
# nkfでファイルの文字コードをUTF-8のBOMありに変更して上書きする。
# フォルダ内のファイル全て変換する
# 起動方法
# C:\aaa\PowerShell\nkf_UTF8wBOM2.ps1 -InFolder1 C:\aaa\bbb
# パラメータ
Param(
[Parameter(Mandatory=$true)]
[String]$InFolder1 # 対象フォルダ
)
# スクリプトがある場所
$ScriptPath1 = Split-Path $MyInvocation.MyCommand.Path -Parent
Write-Host "`$ScriptPath1="$ScriptPath1
# 関数ファイル
$myFunctionFile1 = $ScriptPath1+"\myFunction1_UTF8.ps1"
## 関数読み込み ##
. $myFunctionFile1
$commandPath = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
$stdErrLogTmp = ".\stderr.log"
$stdOutLogTmp = ".\stdout.log"
$myResult1 = $true
if (-not(Test-Path($InFolder1))) {
Write-Host $InFolder1"がありません。"
$myResult1 = $false
}
if ($myResult1 -eq $true) {
$myFlag1 = $true
While($myFlag1 -eq $true){
# $InFolder1内のファイル・フォルダのリストを取得する。
# -Recurseを付けるとサブフォルダも検索する。
$itemList = Get-ChildItem $InFolder1 -Recurse;
foreach($item in $itemList){
if($item.PSIsContainer){
# フォルダの場合の処理
Write-Host($item.Name + 'はフォルダです。');
$NewItem1 = myFRnSpToUBar1 -TargetFile1 $item # フォルダ名の空白をアンダーバーに変換
Write-Host("新しいフォルダ名="+$NewItem1);
if($NewItem1 -ne $item){
# フォルダ名の変更があったため、foreachループを抜けて、ファイル・フォルダのリストを再取得する。
$myFlag1 = $true # whileループは継続
break # foreachを抜ける
}else{
$myFlag1 = $false # foreachループが終了したら、whileループも終了
}
Write-Host "`$myFlag1="$myFlag1
Read-HostDefault "OK?" > $null # 入力待ち
}else{
# ファイルの場合の処理
Write-Host ($item.Name + 'はファイルです。');
$NewItem1 = myFRnSpToUBar1 -TargetFile1 $item # ファイル名の空白をアンダーバーに変換
Write-Host "`$NewItem1=$NewItem1"
Read-HostDefault "OK?" > $null # 入力待ち
$ScriptFile1 = $ScriptPath1 + "\nkf_UTF8wBOM1.ps1 -TargetFile1 $NewItem1 -ScriptPath1 $ScriptPath1"
Write-Host "`$ScriptFile1=$ScriptFile1"
$Argument1 = "-Command $ScriptFile1"
Start-Process -FilePath $commandPath `
-ArgumentList $Argument1 `
-NoNewWindow `
-RedirectStandardOutput $stdOutLogTmp -RedirectStandardError $stdErrLogTmp -Wait
Write-Host "終了"
Get-Content $stdErrLogTmp, $stdOutLogTmp
foreach($strLogPath in @($stdErrLogTmp, $stdOutLogTmp)){
if (Test-Path $strLogPath) {
Remove-Item $strLogPath;
}
}
$myFlag1 = $false # foreachループが終了したら、whileループも終了
}
}
Write-Host "`$myFlag1="$myFlag1
Read-HostDefault "OK?" > $null # 入力待ち
}
}
# 終了
Return $myResult1
【myFunction1_UTF8.ps1(一部)】
# 関数:ファイル名の空白をアンダーバーに変換
function myFRnSpToUBar1{
param (
[Parameter(Mandatory=$true)]
[String]$TargetFile1 # 対象ファイル
)
# ドットソース演算子
# .{}の中の標準出力は戻り値に含めない
.{
if($item.PSIsContainer){
# フォルダの場合
$NewFileName1 = (Split-Path ($TargetFile1) -Leaf) -replace '\s','_'
}else {
# ファイルの場合
$NewFileName1 = (Get-ChildItem -Path ($TargetFile1)).Name -replace '\s','_'
# $NewFileName1 = (Get-ChildItem -Path $TargetFile1).Name -replace '\s','_'
}
$ParentPath1 = Split-Path $TargetFile1 -Parent
$NewFilePath1 = $ParentPath1 + "\" + $NewFileName1
Write-Host "`$NewFilePath1=$NewFilePath1"
Read-HostDefault "OK?" > $null # 入力待ち
# ファイル名置換(空白→アンダーバー)
if($NewFilePath1 -ne $TargetFile1){
get-item $TargetFile1|Rename-Item -NewName { $_.Name -replace '\s','_' }
}
}>$null
Return $NewFilePath1
}
ヤバイぜ! ありがとうございます(^_0)ノ
by cheese999 (2021-11-06 04:37)