setlocal
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@rem setlocal DISABLEDELAYEDEXPANSION
@rem 説明:
@rem Template of PowerShell script in Batch file.
@rem Usage:
@rem "_deleteEmptyDirectory.ps1.bat" "PS1_DeleteEmptyDirectory" "ディレクトリ・パス"
@rem "_deleteEmptyDirectory.ps1.bat" "PS1_Output_PowerShellScriptLinesRange" " 1 " " 22 二弐 " " 333 333 333 "
@rem "_deleteEmptyDirectory.ps1.bat" "PS1_Output_PowerShellScriptLinesRange" "_deleteEmptyDirectory.ps1.bat" " 1 " " 22 二弐 " " 333 333 333 "
@rem "_deleteEmptyDirectory.ps1.bat"
@rem "_deleteEmptyDirectory.ps1.bat" "xxxxxxxx" " 1 " " 22 二弐 " " 333 333 333 "
@rem echo errorlevel=%errorlevel%
@rem ####################
@rem Batch.
@rem ####################
@rem ....................
@rem Batch main.
@rem ....................
set rv=11
if "%~1"=="" ( call :L_call_BAT_NoArgs
) else if "%~1"=="PS1_Output_PowerShellScriptLinesRange" ( call :L_call_PS1 %*
) else ( call :L_call_BAT_OtherArgs %*
)
@rem ....................
@rem Exit.
@rem ....................
@rem echo.
@rem pause
@rem echo 終了するには何かキーを押してください . . .
@rem pause>nul
exit /b !rv!
goto :eof
@rem ....................
@rem No args.
@rem ....................
:L_call_BAT_NoArgs
echo BAT %0 %* ....................
call :L_call_PS1
call :L_call_PS1
set /a rv=!errorlevel!+9
goto :eof
@rem ....................
@rem Other args.
@rem ....................
:L_call_BAT_OtherArgs
@rem echo BAT %0 %* ....................
call :L_call_PS1 %*
set /a rv=!errorlevel!+8
goto :eof
@rem ....................
@rem Echo ErrorLevel.
@rem ....................
:L_echoErrorLevel
@rem echo BAT L_echoErrorLevel ....................
@rem echo BAT %0 R errorlevel=!errorlevel!
@rem echo.
goto :eof
@rem ....................
@rem Get PowerShellScriptLinesRange.
@rem ....................
:L_getPowerShellScriptLinesRange
@rem echo BAT %0 %* ....................
for /f "usebackq tokens=1,2 delims=, " %%y in (
`call "%~f0" "PS1_Output_PowerShellScriptLinesRange" "%~f0"`
) do (
set /a ps1Sta1=%%y+0 2>nul
set /a ps1End1=%%z+0 2>nul
if "!ps1Sta1!,!ps1End1!"=="%%y,%%z" (
set "ps1Sta=%%y"
set "ps1End=%%z"
)
)
goto :eof
@rem ....................
@rem Call PowerShell script.
@rem ....................
:L_call_PS1
@rem http://qiita.com/cd01/items/82829ba0ec0f59e1b04d
@rem http://scripting.cocolog-nifty.com/blog/2007/07/powershellps1_bd41.html
@rem echo BAT %0 %* ....................
set "ps1args=%*"
set "ps1line=$ps1sb = (\"\");"
set "ps1line=!ps1line!$ps1obj = (Get-Content \"%~f0\");"
if "!ps1Sta!,!ps1End!"=="," (
if "%~1"=="PS1_Output_PowerShellScriptLinesRange" (
set "ps1line=!ps1line!$ps1Sta = ($ps1obj | Select-String (\"^goto \:eof$\") | Select-Object -Last 1).LineNumber + 1;"
set "ps1line=!ps1line!$ps1End = ($ps1obj | Select-String (\"^@\" + \"end$\") | Select-Object -First 1).LineNumber - 1;"
set "ps1line=!ps1line!if ($ps1End -eq -1) {$ps1End = $ps1obj.Length};"
) else (
call :L_getPowerShellScriptLinesRange
set "ps1line=!ps1line!$ps1Sta = !ps1Sta!;"
set "ps1line=!ps1line!$ps1End = !ps1End!;"
)
) else (
set "ps1line=!ps1line!$ps1Sta = !ps1Sta!;"
set "ps1line=!ps1line!$ps1End = !ps1End!;"
)
set "ps1line=!ps1line!$ps1sb += for ($cc = 0; $cc -lt $ps1Sta; ++$cc) {$ps1sb += \"`n\"} <# Dummy blank lines for line number in PowerShell message. #>;"
set "ps1line=!ps1line!$ps1sb += ($ps1obj | Select-Object -First $ps1End | Select-Object -Skip $ps1Sta) -Join \"`n\";"
set "ps1line=!ps1line!$ps1sb = [scriptblock]::create($ps1sb);"
set "ps1line=!ps1line!&$ps1sb"
if "!ps1args!"=="" (
powershell -NoProfile -ExecutionPolicy Unrestricted "!ps1line!"
) else (
set "ps1args=!ps1args:"='!"
rem '
powershell -NoProfile -ExecutionPolicy Unrestricted "!ps1line!" "!ps1args!"
)
call :L_echoErrorLevel
goto :eof
# ####################
# PowerShell script.
# ####################
# ....................
# No args.
# ....................
function global:NoArgs {
Write-Host "PS1 NoArgs ...................."
# Write-Host "PS1 Argc=$($Args.Length)."
Write-Host "PS1 There are no arguments."
return 9
}
# ....................
# Delete empty directory.
# ....................
function global:PS1_DeleteEmptyDirectory([string[]]$ps1args) {
# http://qiita.com/yjnk/items/a69920748e96272bfd10
# http://www.atmarkit.co.jp/ait/articles/0808/29/news131.html
# http://www55.atwiki.jp/sabakanse/pages/20.html
if(Test-Path $ps1args[0]){
$List = Get-ChildItem -Recurse $ps1args[0] |
Where-Object {$_.Attributes -eq "Directory"} |
Sort-Object FullName -Descending
foreach($a in $List) {
# Write-Host ("1`t" + $a.GetDirectories().Count + "`t" + $a.GetFiles().Count + "`t" + $a.FullName)
if (($a.GetFiles().Count -eq 0) -and ($a.GetDirectories().Count -eq 0)) {
# Write-Host ("2`t" + $a.GetDirectories().Count + "`t" + $a.GetFiles().Count + "`t" + $a.FullName)
Write-Host ($a.FullName)
Remove-Item $a.FullName
}
}
}else{
Write-Host 'ディレクトリは存在しません。'
}
return 1
}
# ....................
# Other args.
# ....................
function global:OtherArgs([string[]]$ps1args) {
Write-Host "PS1 OtherArgs ...................."
Write-Host "PS1 [$($ps1args.Length)]$($ps1args)."
for($idx = 0; $idx -lt $ps1args.Length; ++$idx) {
Write-Host "PS1 Args[$($idx)]=$($ps1args[$idx])."
}
return 8
}
# ....................
# Output PowerShellScriptLinesRange.
# ....................
function global:Output_PowerShellScriptLinesRange([string]$pathPowerShellScript) {
$ps1Sta = 0
$ps1End = 0
if (Test-Path $pathPowerShellScript) {
$ps1obj = Get-Content $pathPowerShellScript
$ps1Sta = ($ps1obj | Select-String ("^goto \:eof$") | Select-Object -Last 1).LineNumber + 1
$ps1End = ($ps1obj | Select-String ("^@" + "end$") | Select-Object -First 1).LineNumber - 1
if ($ps1End -eq -1) {$ps1End = $ps1obj.Length}
}
Write-Host "$ps1Sta,$ps1End"
return 7
}
# --------------------
# PowerShell script main.
# --------------------
$rv = 20
if ($Args.Length -eq 0) {
$rv = $rv + (NoArgs)
} else {
if (0 -eq 1) {
$rv = $rv + 1
} elseif (($Args.Length -eq 2) -and ($Args[0].Equals("PS1_DeleteEmptyDirectory"))) {
$ary = $Args[1 .. ($Args.length - 1)]
$rv = $rv + (PS1_DeleteEmptyDirectory ($ary))
} elseif (($Args.Length -ge 1) -and ($Args[0].Equals("PS1_Output_PowerShellScriptLinesRange"))) {
$rv = $rv + (Output_PowerShellScriptLinesRange ($Args[1]))
} else {
$rv = $rv + (OtherArgs ($Args))
}
}
exit $rv