GetHistory.bat
の出力がというファイルにリダイレクトされていると仮定するとhistory.txt
、それを新しいバッチ ファイルにフィードできます。たとえば、ParamCSV.bat
次の結果が得られます。
C:\stackoverflow>ParamCSV.bat 1001 < history.txt
1001,Act1,Created,1-Jan-2013
1001,Act2,Submitted,2-Jan-2013
1001,Act3,Approved,2-Jan-2013
このための簡単なスクリプトをまとめるために、次の情報を参照しました。
私はこのバッチスクリプトを思いつきましたParamCSV.bat
:
@echo off
:: ParamCSV.bat
::
:: Usage: ParamCSV.bat <Parameter_ID> < history.txt
::
:: Thanks to:
:: https://stackoverflow.com/questions/6979747/read-stdin-stream-in-a-batch-file/6980605#6980605
:: https://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file
:: https://stackoverflow.com/questions/3001999/how-to-remove-trailing-and-leading-whitespace-for-user-provided-input-in-a-batch
:: Copy input parameter to 'id'
set id=%1
setlocal DisableDelayedExpansion
for /F "skip=2 tokens=*" %%a in ('findstr /n $') do (
set "line=%%a"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
set "activity=!line:~0,17!"
call:trim !activity! activity
set "status=!line:~17,11!"
call:trim !status! status
set "date=!line:~28,11!"
call:trim !date! date
echo(!id!,!activity!,!status!,!date!
endlocal
)
goto:EOF
::function: trim
::synopsis: Removes leading and trailing whitespace from a sting. Two
:: parameters are expected. The first is the text string that
:: is to be trimmed. The second is the name of a variable in
:: the caller's space that will receive the result of the
:: trim operation.
::
::usage: call:trim string_to_trim var_to_update
:: e.g. call:trim %myvar1% myvar2
::trim left whitespace
setlocal
set input=%~1
for /f "tokens=* delims= " %%a in ("%input%") do set input=%%a
::trim right whitespace (up to 100 spaces at the end)
for /l %%a in (1,1,100) do if "!input:~-1!"==" " set input=!input:~0,-1!
::return trimmed string in place
endlocal&set "%~2=%input%"
ここではいくつかの仮定がなされており、それらのいずれかが変更されたり無効になったりすると、スクリプトが壊れます。
- の出力に
GetHistory.bat
は、幅が 17、11、および 11 の固定幅の列があります。
- 2 つのヘッダー行がありますが、
for
ステートメントではスキップします。
- すべての出力行は同じ ID 用であるため、1 つの入力パラメーターのみが必要であり、それはすべての CSV 出力行の最初の要素です。