COMMAND.COM
has very few string-manipulation features. Back in DOS days, “complicated” batch files relied on a mixture of crazy tricks and external utilities.
To extract portions of a variable, you could use PC Magazine’s STRINGS
utility, featured in volume 10, number 15:
STRINGS _month = MID %_date%, 1, 2
or
STRINGS _month = LEFT %_date%, 2
(Case isn’t significant here.)
The PC Magazine utility collections are somewhat difficult to find, but STRINGS
was updated several times over the years and uploaded to the “batch utilities” collection on Simtel, and it can be found in Simtel archives. The latest version can be installed as a COMMAND.COM
extension and provides many more functions:
STRINGS _date = DATE
STRINGS _month = LEFT %_date%, 2
STRINGS _year = RIGHT %_date%, 4
As phuclv says, you might want to use another shell such as 4DOS. This includes many functions, including string- and date/time-manipulation functions:
set month=%@left[%date,2]
or more directly, use the special %_month
and %_year
variables (containing the current month and year).
As you say, the web nowadays is a poor source of information on DOS-specific batch file programming, especially since many pages confuse DOS COMMAND.COM
and Windows CMD.EXE
features. Batch file programming was popular before the web existed, and as such is documented in books, magazine articles and batch file collections traded on BBSes or uploaded to FTP sites; if you’re interested in learning more, look through Timo Salmi’s BATFAQ
and batch file collection, the various utilities on Garbo and Simtel, and books such as Neil J. Rubenking’s PC Magazine DOS Batch File Lab Notes or Rex Last’s MS-DOS Revealed (which concentrates on extending DOS batch files by using assembly language programs, coded using DEBUG
). Google Books has an extensive collection of old PC Magazine issues, many of which include batch file tips and tricks.
for %a in (/abc) do echo %a
trick, but that gets painful quite quickly ;-).