DOS Devices - Examples

Check if a parameter is a valid device name

These batch files use several strange features of the DIR command when checking a device name.
The AUX, CON and PRN devices are treated separately in the DOS test, since in DOS they are shown by the DIR command with the current date and time.
Both ISDEV.BAT and ISDEV.CMD work in NT.

 

ISDEV.BAT:

Usage:

ISDEV device

Example:

ISDEV COM1

Tested in NT and OS/2 DOS box.
Should also work in MS-DOS 6.* and 7.*, but you'd better test that thoroughly.

@ECHO OFF
:: Checks if %1 is a valid device name.

ECHO.
IF "%1"=="" GOTO Syntax
IF "%1"=="/?" GOTO Syntax

:: DOS or NT?
VER | FIND "Windows NT" >NUL
IF ERRORLEVEL 1 GOTO DOS

:NT
:: Based on the fact that DIR doesn't show a volume label for devices
DIR %1 2>>NUL | FIND /I "Volume" >NUL
IF     ERRORLEVEL 1 ECHO %1 is a valid NT device name
IF NOT ERRORLEVEL 1 ECHO %1 is NOT a valid NT device name
GOTO End

:DOS
:: Based on the assumption that DIR displays
:: no "file" date and time for devices.
:: But first check AUX, CON and PRN, that are
:: displayed with the current time and date.
:: Sorry, this isn't a very elegant solution.
:: If you know a better way, I'm all ears...
SET OPATH=%PATH%
PATH %1
SET DEVICE=%PATH%
PATH %OPATH%
SET OPATH=
IF "%DEVICE%"=="AUX" GOTO Valid
IF "%DEVICE%"=="CON" GOTO Valid
IF "%DEVICE%"=="PRN" GOTO Valid
DIR \%DEVICE% | FIND "%DEVICE%" >NUL
IF ERRORLEVEL 1 GOTO Invalid
DIR \%DEVICE% | FIND "%DEVICE%" | FIND ":" >NUL
IF NOT ERRORLEVEL 1 GOTO Invalid
DIR \%DEVICE% | FIND "%DEVICE%" | FIND "." >NUL
IF NOT ERRORLEVEL 1 GOTO Invalid
DIR \%DEVICE% | FIND "%DEVICE%" | FIND "-" >NUL
IF NOT ERRORLEVEL 1 GOTO Invalid
DIR \%DEVICE% | FIND "%DEVICE%" | FIND "/" >NUL
IF NOT ERRORLEVEL 1 GOTO Invalid
:Valid
ECHO.%DEVICE% is a valid device name
GOTO End
:Invalid
ECHO.%DEVICE% is NOT a valid device name
GOTO End

:Syntax
ECHO IsDev,  Version 2.00 for NT and DOS
ECHO Checks if the specified parameter is a valid device name
ECHO.
ECHO Usage:  ISDEV  device_name

:End
SET DEVICE=
ECHO.
Click to view source Click to download source

 

ISDEV.CMD:

Usage:

ISDEV device

Example:

ISDEV LPT1

Tested in Windows NT 4 and OS/2 Warp 4.
Should also work in previous versions of NT and OS/2, but you'd better test that thoroughly.

@ECHO OFF
:: Checks if %1 is a valid OS/2 or NT device name

ECHO.
IF "%1"=="" GOTO Syntax
IF "%1"=="/?" GOTO Syntax

:: OS/2 or NT?
VER | FIND "Windows NT" > NUL
IF ERRORLEVEL 1 GOTO OS2

:NT
:: Based on the fact that DIR doesn't show a volume label for devices
DIR %1 2>NUL | FIND /I "Volume" >NUL
IF     ERRORLEVEL 1 ECHO.%1 is a valid NT device name
IF NOT ERRORLEVEL 1 ECHO.%1 is NOT a valid NT device name
GOTO End

:OS2
:: Based on the fact that OS/2 devices are
:: always "located" in the "directory" \DEV
DIR %1 2>NUL | FIND "\\DEV" >NUL
IF     ERRORLEVEL 1 ECHO.%1 is NOT a valid OS/2 device name
IF NOT ERRORLEVEL 1 ECHO.%1 is a valid OS/2 device name
GOTO End

:Syntax
ECHO IsDev,  Version 1.20 for NT and OS/2
ECHO Checks if the specified parameter is a valid device name
ECHO.
ECHO Usage:  ISDEV  ˆ<device_nameˆ>

:End
Click to view source Click to download source

 

The devices mentioned on this page are logical (or virtual) DOS devices.
For management of Physical devices in Windows 2000 and later use DEVCON.


page last uploaded: 1 May 2013, 13:20