Since it's a first msg after all this time, at least I would like to say that every time I walk through Dostips that makes me very enthusiast, I always discover something new, it's endless!, thanks to the aliens here who speak esoteric&dirty batch fluently
So, I would ask for a little help: I would like to have a small subroutine for simple calculation using hybrid cmd/cscript, embedded in a self calling batch so that I could use the sub inside when needed, and without tmp script file.
I saw numerous examples here or on SO for ex., yet didn't reach to make what I want.
After all I came with an hybrid cmd/powershell, the problem being that powershell is too long to load and perform the calculation, I find, yet powerfull but for simple or fast scripts I guess it's too much.
A code being worth a thousand words (while not so sure for msdos batch..), here is an example:
Code: Select all
@echo off
::main
setlocal EnableDelayedExpansion
setlocal EnableExtensions
::operation to perform
set "op=1+1"
call :hybrid-calc !op! res
echo.!op!=!res!
::pws offers a rather complete math library while it's a bit too much for my needs
set "m=[Math]::" & rem just an alias to lighten the writing
set "op=!m!cos(!m!log(!m!pi+20))"
call :hybrid-calc "!op!" res
echo.cos(ln(pi+20))=!res!
exit/b
:hybrid-calc op res
set "op=%~1"
for /f %%A in ('powershell -NoLogo -NoProfile -ExecutionPolicy Bypass "Write-Host ( !op! )" ') do set "r=%%~A"
(endlocal & set "%2=!r!" & exit/b)
I guess it should use something like
Code: Select all
@cscript //E:JScript //nologo "%~f0" "!op!"
Thank you