Skip to content

Instantly share code, notes, and snippets.

@kunst1080
Last active February 2, 2017 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kunst1080/5176912 to your computer and use it in GitHub Desktop.
Save kunst1080/5176912 to your computer and use it in GitHub Desktop.
BATファイルでヒアドキュメントっぽいことを実現するBAT。 内部ではJScriptを使用している。
@if (1==1) /*
@if "%~2"=="" goto :USAGE
@if "%~1"=="/?" goto :USAGE
@rem ---------------------------
:MAIN
@CScript //nologo //E:JScript "%~f0" "%~1" "%~2"
@exit /b
@rem ---------------------------
:USAGE
@echo USAGE:Call NearDoc.bat "%~0" ヒアドキュメントの区切り文字
@exit /b 9
@rem ---------------------------
@rem */
@end
(function outHereDoc(file, tag){
var sr = (WScript.CreateObject("Scripting.FileSystemObject")).OpenTextFile(file, 1, false);
var outFlg = false;
while(!sr.AtEndOfStream){
var s = sr.ReadLine();
if(s==("<" + tag)) outFlg = false;
//if(outFlg) WScript.Echo(s);
if(outFlg) WScript.StdOut.WriteLine(s);
if(s==(tag + ">")) outFlg = true;
}
sr.Close();
delete sr;
})(WScript.Arguments(0), WScript.Arguments(1));
@echo off
cd /d "%~dp0"
rem コンソールの文字コードをUTF-8に変更。
rem GitHubに上げると文字コードがUTF-8に変わってしまうので。。。
rem ちゃんとShift-JISで検証する場合、このコマンドは不要。
chcp 65001
rem ---------------------------
rem simple test
rem ---------------------------
echo テスト①
Call NearDoc.bat "%~0" HERE1
echo.
echo テスト②
Call NearDoc.bat "%~0" HERE2 | sort
echo.
echo テスト終了
pause
GOTO :EOF
HERE1>
1 2 3
4 5 6
7 8 9
<HERE1
HERE2>
anjr
56
osdsdpo
3
4
5
7
1
<HERE2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment