2016年5月18日水曜日

Lsdfr.ps1.bat

@echo off
setlocal
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@rem setlocal DISABLEDELAYEDEXPANSION

@rem 説明:
@rem Directory file list.

@rem Usage:
@rem "Lsdfr.ps1.bat" dirpath csvpath
@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"=="BAT_Output_Args" (                        call :L_BAT_Output_Args %*
) 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
@rem echo BAT %0 %* ....................
call :L_call_PS1
set /a rv=!errorlevel!+9
goto :eof


@rem ....................
@rem Output Args.
@rem ....................
:L_BAT_Output_Args
set /a y=0
for %%z in (
  %*
) do (
  set /a y=!y!+1
  echo BAT %%!y!=%%~z.
)
set /a rv=!errorlevel!+1
goto :eof


@rem ....................
@rem Other args.
@rem ....................
:L_call_BAT_OtherArgs
call :L_call_PS1 %*
set /a rv=!errorlevel!+0
goto :eof


@rem ....................
@rem Echo ErrorLevel.
@rem ....................
:L_echoErrorLevel
@rem echo BAT L_echoErrorLevel ....................
echo BAT %0 R errorlevel=!errorlevel!
@rem echo.
goto :eof


@rem ....................
@rem Get PowerShellScriptLinesRange.
@rem ....................
:L_getPowerShellScriptLinesRange
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
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!"
)
@rem call :L_echoErrorLevel
goto :eof


# ####################
# PowerShell script.
# ####################


# ....................
# No args.
# ....................
function global:NoArgs {
  # Write-Host "PS1 NoArgs ...................."
  # Write-Host "PS1 Argc=$($Args.Length)."  
  Write-Host "Usage: Lsdfr.ps1.bat dirpath csvpath"
  return 9
}


# ....................
# Get-Lsdfr.
# ....................
# http://eco.senritu.net/make_filelist_with_powershell/
function global:Get-Lsdfr {
  Param ([string]$dirpath, [string]$csvpath)
  Set-Location $dirpath
  # "PSPath","PSParentPath","PSChildName","PSDrive","PSProvider","PSIsContainer",
  # "BaseName","Mode","Name","Parent","Exists","Root","FullName","Extension",
  # "CreationTime","CreationTimeUtc","LastAccessTime","LastAccessTimeUtc","LastWriteTime","LastWriteTimeUtc","Attributes"
  Get-ChildItem -Recurse | Select-Object Mode,CreationTime,CreationTimeUtc,LastAccessTime,FullName,Extension | Export-Csv -Encoding Default -force -notype $csvpath
  return 0
}


# ....................
# 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])."
#  }
  Write-Host "Usage: Lsdfr.ps1.bat dirpath csvpath"
  return 1
}


# ....................
# 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 = 0
if ($Args.Length -eq 0) {
  $rv = $rv + (NoArgs)
} else {
  if (0 -eq 1) {
    $rv = $rv + 1
  } elseif (($Args.Length -ge 2) -and ($Args[0].Equals("PS1_Output_PowerShellScriptLinesRange"))) {
    $rv = $rv + (Output_PowerShellScriptLinesRange ($Args[1]))
  } elseif ($Args.Length -ge 2) {
    $rv = $rv + (Get-Lsdfr $Args[0] $Args[1])
  } else {
    $rv = $rv + (OtherArgs ($Args))
  }
}
exit $rv