Tuesday, 7 July 2009

JS2BAT converter

This script allows you to embed Javascript codes into the batch-script immediately. So you can stop to worry about creating of an additional batch-wrapper or lost of some specific parameters. Perl distribution has the same functional script pl2bat but for wrapping of perl-scripts with batch-scripts. I am not author of this idea, i just implemented this feature as the standalone tool. The main feature is to write the prolog code that is valid both in a batch and javascript. When launching the batch it runs as batch and calls the javascript interpreter with the same file as javascript file. But the second one skips this prolog as valid javascript comments.
@echo off

if /i "%~1" == "/?" goto help
if /i "%~1" == "/H" goto help

if "%~1" == "" (
    copy /b con "%TEMP%\$%TIME::=%" > nul
    if errorlevel 1 goto :EOF

    call :print_file "%TEMP%\$%TIME::=%"

    goto :EOF

if exist "%~f1" (
    call :print_file "%~f1" > "%~dpn1.bat"

    goto :EOF

echo Can't open "%~1": No such file or directory

goto :EOF

echo @set @x=0 /*
echo @cscript /nologo /e:javascript "%%0" %%*
echo @goto:eof */

type "%~1"
goto :EOF

echo Usage:
echo     JS2BAT [/?^|/H]
echo     JS2BAT [file]
goto :EOF

No comments:

Post a Comment