Digital Sorceress

NATHAN'S PROGRAMMING LESSONS

This page is just to give me an easy place to post some programming examples for Nathan.

LESSON 1 - hello.bat

Example of prompting the user for some input (name and age in years) and really roughly calculate how many months old the person is.

New Concepts:

  • Variables
  • Prompting for user input
  • Simple arethmetic
  • Echoing output
@echo off
SET /P MYNAME=Type your name here: 
set /p MYAGE=Type your age (in years):
set /a MONTHS=MYAGE*12

echo hello, my name is %MYNAME% and I am %MONTHS% months old
pause

LESSON 2 - hello2.bat

Building on the previous example, we delve into tokenizing, and while the math is still simple, we have to solve the problem of how to get the right number of months based on birth month/year versus current month/year.

New Concepts:

  • Tokenizing strings
  • Calling build in functions
  • Problem (real-world) solving with algorithms
@echo off
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=2,3 DELIMS=/" %%A IN ('DATE/T') DO SET yyyy=%%B

echo current month: %mm%
echo current year: %yyyy%

SET /P MYNAME=Type your name here: 
set /p MYYEAR=Type the year you were born (yyyy):
set /p MYMONTH=Type the month (number) you were born (mm):

echo name: %MYNAME%
echo born: %MYMONTH% / %MYYEAR%

set /a FULLYEARS=(yyyy-1) - (MYYEAR+1)
echo FULLYEARS: %FULLYEARS%

set /a PREMONTHS = 12 - MYMONTH
echo PREMONTHS: %PREMONTHS%

set /a PLUSMONTHS = (PREMONTHS + mm)
echo PLUSMONTHS: %PLUSMONTHS%

set /a AGEINMONTHS = (FULLYEARS*12) + PLUSMONTHS

echo -------------------------------
echo ok, %MYNAME% that makes you %AGEINMONTHS% months old!
pause

LESSON 2a - tokens.bat

Now, I thought I'd give you a little sandbox to play with the parsing

@echo off

FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B

echo raw date: 
DATE/T

echo mm: %mm%

pause

 

HOME | PRODUCTS | TOYS | WHOIS | CONTACT US

©1998-2008 The Digital Sorceress

 
 
SITE NAVIGATION
· HOME

· PRODUCTS

· TOYS
  · WordFinder
  · EncoDecode

· PHOTOGRAPHY

· WHOIS

· CONTACT US