Dev-Picayune

picayune: of little value or importance

Some Weird DOS String Things

I found myself needing to do a fairly complicated thing. Well, it’s not complicated, but for a DOS batch file, it seemed complicated. The problem I was facing: to insert some “official” DOS command stuff into a .bat file that is run on all computers in our entire organization. I have no idea how many machines were talking about here but I would guess the number to be in the 5,000+ range or higher. Specifically, this is our official domain login script. Yikes. I don’t like having anything to do with something that gets touched this often. One screwup in there and it’s not a good thing. The problem is that we have a special group of machines (about 130) that are not registered on the domain. Now we need them on the domain but we don’t want to run some of the programs that our “Software Taliban” uses for monitoring machines. All of these special machines have a common naming scheme which is the letter S followed by a 7-digit number. So my task was insert some DOS .bat stuff in the existing .bat file that will run without external dependencies (other than whatever is already guaranteed to be there). All I knew for sure was that the environment variable COMPUTERNAME would be filled with the name of the computer and that my special machines were running 2K or XP and that either way, the OS environment variable would be filled with “Windows_NT”.

I also wanted to make sure I didn’t catch machines that were named “Snoopy” or anything like that. So for lack of a better way, I chose the following as my DOS string substitution method:

SET CN=%COMPUTERNAME% 
SET CN=%CN:0=x% 
SET CN=%CN:1=x% 
SET CN=%CN:2=x% 
SET CN=%CN:3=x% 
SET CN=%CN:4=x% 
SET CN=%CN:5=x% 
SET CN=%CN:6=x% 
SET CN=%CN:7=x% 
SET CN=%CN:8=x% 
SET CN=%CN:9=x% 
IF /i "%CN%"=="Sxxxxxx" GOTO END

The magic here is the funky %variable:y=x% stuff. Essentially, it’s string replacement where any ‘y’ is replaced with an ‘x’. This then normalizes the name into something I can compare against. Doing it this way also guarantees that I am comparing the length as well. So while it isn’t pretty, I was pleasantly surprised to find it was possible to do in DOS. There is also a method for getting a substring using a similar syntax: %variable:~s,l% where s is the starting position (1-based) and l is the length of the sub-string. Talk about obscure. But then again, how often do you have to stick to plain old DOS .bat files?

Technorati Tags: ,

4 comments

4 Comments so far

  1. edyalc November 21st, 2007 3:32 am

    you have no idea how this info saved me from doing complicated string manipulations in .bat files. =P

    thanks for sharing.. =D

  2. ScW November 21st, 2007 10:04 am

    Glad it helped… it really is nice to know that it’s not a waisted effort to post things.

  3. Stephen Burke June 26th, 2008 5:27 pm

    Here’s the rest:

    Code:
    ______________________________________

    REM Change date-time format w/out /, create dir and copy files.

    for /F “tokens=1-5 delims=/ ” %%i in (‘date /t’) do (

    set YeaR = %%l

    set Month=%%j

    set Day=%%k

    set Date=%%l-%%j-%%k

    )

    for /F “tokens=1-2 delims=: ” %%m in (‘time /t’) do (

    set time=-%%m

    set second=-%%n

    set time=-%%m-%%n

    )

    set file=%drive%%Date%%time%

    copy c:\macfiltering\calloutfilterfile\maclist.txt

    c:\macfiltering\backup\%file%.sav

    echo %1 >> c:\macfiltering\calloutfilterfile\maclist.txt

    cls

    echo msgbox “%1 now entered into the file. If this is an error, revert to the

    last file in c:\macfiltering\backup\date-time.sav” > 1.vbs

    wscript 1.vbs

    cls

    echo msgbox “Click OK Stop and Start DHCP Server” > 2.vbs

    wscript 2.vbs

    start net stop dhcpserver

    start net start dhcpserver

    :BYE

  4. Stephen Burke June 27th, 2008 8:36 am

    Trying to feed it a mac address via %1. Example, if the batch is nameed test.bat I would like to feed it like this:

    C:\>test.bat 0020E7666999

    Sadly it adds anything I throw at it into the file no matter the size or charecter rather than the defined length of 12, a-f, A-F, 0-9.

    Code:
    _____________________________________
    @ECHO off
    if not exist %1 then goto bye
    SET MYVAL=%1
    SET MYVAL=%MYVAL:x=r%
    SET MYVAL=%MYVAL:0=x%
    SET MYVAL=%MYVAL:1=x%
    SET MYVAL=%MYVAL:2=x%
    SET MYVAL=%MYVAL:3=x%
    SET MYVAL=%MYVAL:4=x%
    SET MYVAL=%MYVAL:5=x%
    SET MYVAL=%MYVAL:6=x%
    SET MYVAL=%MYVAL:7=x%
    SET MYVAL=%MYVAL:8=x%
    SET MYVAL=%MYVAL:9=x%
    SET MYVAL=%MYVAL:A=x%
    SET MYVAL=%MYVAL:B=x%
    SET MYVAL=%MYVAL:C=x%
    SET MYVAL=%MYVAL:D=x%
    SET MYVAL=%MYVAL:E=x%
    SET MYVAL=%MYVAL:F=x%
    SET MYVAL=%MYVAL:a=x%
    SET MYVAL=%MYVAL:b=x%
    SET MYVAL=%MYVAL:c=x%
    SET MYVAL=%MYVAL:d=x%
    SET MYVAL=%MYVAL:e=x%
    SET MYVAL=%MYVAL:f=x%
    IF /i “%MYVAL%”!==!”xxxxxxxxxxxx” GOTO bye
    REM Change date-time format w/out /, create dir and copy files.
    for /F “tokens=1-5 delims=/ ” %%i in (‘date /t’) do (
    set Year=%%l
    set Month=%%j
    set Day=%%k
    set Date=%%l-%%j-%%k
    )
    for /F “tokens=1-2 delims=: ” %%m in (‘time /t’) do (
    set time=-%%m
    set second=-%%n
    set time=-%%m-%%n
    )
    set file=%drive%%Date%%time%
    copy c:\macfiltering\calloutfilterfile\maclist.txt c:\macfiltering\backup\%file%.sav
    echo %1 >> c:\macfiltering\calloutfilterfile\maclist.txt
    cls
    echo msgbox “%1 now entered into C:\macfiltering\backup\date-time.sav” > 1.vbs
    wscript 1.vbs
    start net stop dhcpserver
    start net start dhcpserver
    :bye

    ________________________________________________

    Any ideas? Any help would be greatly appreciated!

    Thanks,
    Stephen

Leave a reply

For spam filtering purposes, please copy the number 5613 to the field below: