Discussion:
Examples of function in 4dos
(too old to reply)
Just..*
2014-12-13 17:00:49 UTC
Permalink
I have never used the self defined function feature introduced
in one of the later versions of 4dos. The help info is not clear to me as
to how one writes one. I know how to do it in the "nix" shell world.

Could someone do a simple example so I might see how it is constructed?

Let's say for purposes of example one to run a dos program or btm with
arguments.
JJ
2014-12-13 23:39:43 UTC
Permalink
Post by Just..*
I have never used the self defined function feature introduced
in one of the later versions of 4dos. The help info is not clear to me as
to how one writes one. I know how to do it in the "nix" shell world.
Could someone do a simple example so I might see how it is constructed?
Let's say for purposes of example one to run a dos program or btm with
arguments.
Custom function is just a batch of variable function(s). The parameters in
the for function is specified using %1, %2, and so on.

Here's an example function to calculate the propotionally scaled image
dimension (width and height) to fit into 1024x768 screen, then return the
result. Note that below commandline may be truncated into multiple lines.
There's only 3 spaces: between "FUNCTION" and "FITIMAGE", AND before and
after "GT".

FUNCTION FITIMAGE=`%@IF[%@EVAL[%2*1024/%1=.0] GT
768,%@EVAL[%1*768/%2=.0]x768,1024x%@EVAL[%2*1024/%1=.0]]`

The function takes 2 parameters. The original image width, then the height.
e.g.:

C:\4DOS>ECHO %@FITIMAGE[600,300]
1024x512

C:\4DOS>ECHO %@FITIMAGE[600,500]
922x768

C:\4DOS>ECHO %@FITIMAGE[1500,1000]
1024x683

For comparison, here's the batch file for that function.

@ECHO OFF
SETLOCAL
SET WIDTH=%1
SET HEIGHT=%2
SET NEWHEIGHT=%@EVAL[%HEIGHT*1024/%WIDTH=.0]
IFF %NEWHEIGHT GT 768 THEN
::HEIGHT IS TOO LARGE. USE MAX SCREEN HEIGHT AND SHRINK THE WIDTH
SET NEWWIDTH=%@EVAL[%WIDTH*768/%HEIGHT=.0]
SET NEWHEIGHT=768
ELSE
::HEIGHT FIT INTO MAX SCREEN HEIGHT
SET NEWWIDTH=1024
ENDIFF
ECHO %NEWWIDTH%x%NEWHEIGHT%
Michael Bednarek
2014-12-18 00:29:57 UTC
Permalink
Post by Just..*
I have never used the self defined function feature introduced
in one of the later versions of 4dos. The help info is not clear to me as
to how one writes one. I know how to do it in the "nix" shell world.
Could someone do a simple example so I might see how it is constructed?
Let's say for purposes of example one to run a dos program or btm with
arguments.
I assume you've read http://jpsoft.com/help/index.htm?function.htm.

Here are a few functions I use occasionally (they should show as single lines):

:: myMod[n,m] returns m where n%%m returns 0
FUNCTION myMOD=`%@EVAL[((%1-1)%%%2+1)]`

:: DaysOfMonth[validDate] returns number of days in the month
FUNCTION DaysOfMonth=`%@EVAL[42-%@DAY[%@MAKEDATE[%@EVAL[%@DATE[%@YEAR[%@MAKEDATE[%@DATE[%&],4]]-%@MONTH[%&]-1]+41],4]]]`

:: DaysOfMnth[yyyy,mm] returns number of days in the month
FUNCTION DaysOfMnth =`%@EVAL[42-%@DAY[%@MAKEDATE[%@EVAL[%@DATE[%1-%2-1]+41],4]]]`

:: EoMonthN[yyyy,mm] returns the date number of the last day of a month
FUNCTION EoMonthN=`%@DATE[%1-%2-%@DaysOfMnth[%1,%2]]`

:: EoMonthD[yyyy,mm] returns the date string of the last day of a month
FUNCTION EoMonthD=`%@MAKEDATE[%@EoMonthN[%1,%2],4]`

:: LastWDay[yyyy,mm,wd] returns the date for the last weekday [wd 1=Su, 7=Sa] in the month
FUNCTION LastWDay=`%@MAKEDATE[%@EVAL[%@EoMonthN[%1,%2]+%@IF[%3 GT %@DOWI[%@EoMonthD[%1,%2]],%@Eval[%3-%@DOWI[%@EoMonthD[%1,%2]]-7],%@Eval[%3-%@DOWI[%@EoMonthD[%1,%2]]]]],4]`

:: MMM[validDate] returns the 3-letter Monthname
FUNCTION MMM=`%@FIELD[%@MONTH[%&],,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec]`

:: PrevMonth[validDate] returns the argument's previous month
FUNCTION PrevMonth=`%@MONTH[%@MAKEDATE[%@EVAL[%@DATE[%&]-%@DAY[%&]]]]`

:: Win32 associations/filetypes (borrowed from "E. S. (Steve) Fabian")
FUNCTION assoc=`%@word["=",1,%@execstr[assoc %1]]`
FUNCTION ftype=`%@execstr[ftype %1]`
FUNCTION ext_type=`%@ftype[%@assoc[%1]]`

Hope this helps.
--
Michael Bednarek, Brisbane "ONWARD"
Loading...