1

Have done quite a bit of searching for a guide (of any substance) for the above to no avail. Can anyone refer me to one?

In the present tense however, I am trying to understand the below code example, which returns a two digit representation of the month, that corresponds to the 3 character month name set in v:

    SET v=May

    SET map=Jan-01;Feb-02;Mar-03;Apr-04;May-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12
    CALL SET v=%%map:*%v%-=%%
    SET v=%v:;=&rem.%

    ECHO.%v%
Joey
  • 344,408
  • 85
  • 689
  • 683
user66001
  • 774
  • 1
  • 13
  • 36

3 Answers3

2

SET v=May Set the variable

SET map=Jan-01;Feb-02;Mar-03;Apr-04;May-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12 Set the substitution string

CALL SET v=%%map:*%v%-=%% - Set %v to the map, but replace everything up to %v%- with nothing (: replace * everything up to and including May- with nothing (no substitution code after =) - v is now 05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12

SET v=%v:;=&rem.% replace ; with &rem sets up a line that sets v to 05 and the & rem comments out all the other parts of the command. The command executed is SET v=05 & rem.Jun-06 & rem.Jul-07 & rem.Aug-08 & rem.Sep-09 & rem.Oct-10 & rem.Nov-11 & rem.Dec-12

ECHO.%v% gives the answer

This site gives a lot of info, but running the batch file, and putting echo %v% will also help

SeanC
  • 15,695
  • 5
  • 45
  • 66
  • Second @Jeb's comment. Thanks! As for the set /a reference - This example actually came from this site :) but was hoping for more of a reference (much like your answer provided for this example), rather than learn-by-example method. Let me know if you am aware of any others. Thanks again. – user66001 Jun 26 '12 at 21:33
  • 2
    [High voted batch tags on stackoverflow.com](http://stackoverflow.com/questions/tagged/batch-file+or+batch?sort=votes) shows many good tricks and syntax basics. And also read the FAQ – jeb Jun 26 '12 at 21:39
  • @jeb - Good point - Didn't think of this most-obvious avenue! – user66001 Jun 26 '12 at 21:46
  • +1, I like to refer to the technique as search and replace with code injection. – dbenham Jun 27 '12 at 01:35
1

Excuse me. I don't like this type of code. In my personal opinion, it is much clear to manage these values as an array this way:

rem Prepare the array of two-digits values for each 3-letters month names:
for %%a in ("Jan=01" "Feb=02" "Mar=03" "Apr=04" "May=05" "Jun=06" "Jul=07" "Aug=08" "Sep=09" "Oct=10" "Nov=11" "Dec=12") do (
   set month%%~a
)
rem Previous code is equivalent to these 12 lines:
rem set monthJan=01
rem set monthFeb=02
. . . .
rem set monthDec=12

rem This way, the result is immediately accessible:
SET v=May
CALL SET v=%%month%v%%%

rem Or in the clearer way using Delayed Expansion:
SET v=!month%v%!

monthXXX is an array of values for 12 different 3-letters month names.

SET v=May set the value of the index to an element array.

SET v=!month%v%! is first expanded to SET v=!monthMay! (normal expansion), and then to SET v=05 (Delayed Expansion).

I had explained the reasons I have to prefer this notation with every detail in these posts:

Arrays, linked lists and other data structures in cmd.exe (batch) script

DIR output into BAT array?

I apologize if someone thinks this answer is off-topic...

Antonio

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Thanks for your answer @Aacini. Agree that this seems to be cleaner and easier to follow (once you "expand" all the syntax to what it is doing behind the scenes) – user66001 Jun 27 '12 at 17:19
0

For those who wants the other way around, and wanted an error if the entered month number is wrong, put this in a file named script.cmd:

@echo off
set mNum=%1

set mMap=01-Jan;02-Feb;03-Mar;04-Apr;05-May;06-Jun;07-Jul;08-Aug;09-Sep;10-Oct;11-Nov;12-Dec
call set mName=%%mMap:*%mNum%-=%%
set mName=%mName:;=&rem.%
if "%mName%" == "01-Jan" (
  echo Wrong month number "%mNum%"!
  goto :EOF
  )

echo Month %mNum% is "%mName%".

And start the script with a parameter:

> script 02
Month 02 is "Feb".

> script 13
Wrong month number "13"!

> script foo
Wrong month number "foo"!

However, it does not cover an empty value:

> script
Month  is "Jan".
Henk Wiersema
  • 547
  • 4
  • 5
  • Looks interesting. Can you elaborate as to how (at least) the lines `call set mName=%%mMap:*%mNum%-=%%` and `set mName=%mName:;=&rem.%` work? – user66001 Jul 25 '13 at 18:58