Wednesday, 29 February 2012

Lower casing variables in DOS

Occasionally, I will need to use MS-DOS in my role at work. MS-DOS is one of the scripting languages to use while using Microsoft Windows operating system.

One of the problems with MS-DOS is that it is case-insensitive. MS-DOS does not distinguish the difference between upper case or lower case when using it. While this may not cause problems most circumstances while using Windows, it may potentially cause problems when interacting with other systems or programs.

I have used the following snippet of code to convert the %computername% variable, which was set as upper case to lower case. The result will be stored as %mycomputername%

echo>%computername%
dir /b/l %computername%>lower.tmp
set /p mycomputername=<lower.tmp
del lower.tmp
del %computername%


This snippet works by creating an empty file based on the variable that is in upper case. Then a directory listing is made, with the appropriate flags to list the output in lower case, and the output of this is stored in a file called lower.tmp. Then the contents of that file is read into the new variable. Finally the two temporary files are deleted.

I would have thought it would have been easier than that.

1 comment:

  1. I do NOT endorse the on-line store in the comment above, as I have never done any deals with them. Be careful in any purchases you make with them - but I welcome any and all comments about them.

    ReplyDelete