Microsoft KB Archive/70783

Batch File That Checks If a Root Directory Is Empty PSS ID Number: Q70783 Article last modified on 02-08-1993 PSS database name: O_MSDOS

3.x 4.x

MS-DOS

Question:

I want to check if a floppy disk is empty from within a batch file, and skip to another disk if it is. Can I use the “IF EXIST” command to do this or will something else work?

Response:

The following two files, EMPTY.TXT and EMPTY.BAT, can be used to determine if a root directory is empty. Within the batch file, a reference point has been established where an operation can be executed on the disk. If no files are found, the batch file immediately asks for the next disk.

First, there must be a file called EMPTY.TXT that contains the single line:

SET FILENUM=

You can create this file with the following commands at the DOS prompt:

C:>COPY CON EMPTY.TXT SET FILENUM=

NOTE: You must press CTRL+Z before pressing ENTER. Once EMPTY.TXT has been created, the following batch file, EMPTY.BAT, will use it to do the actual checking.

ECHO OFF :Start COPY EMPTY.TXT TEMP.BAT > NUL DIR A:|FIND “file(s)” >> TEMP.BAT CALL TEMP.BAT IF “%FILENUM%”==&quot;&quot; GOTO End REM REM *** You can insert your desired operations on the diskette here. *** REM :End DEL TEMP.BAT > NUL ECHO Insert another diskette or press CTRL+C to exit. PAUSE GOTO Start

While IF EXIST can be used to check for specific files or subdirectories, using it with wildcards “?” and &quot;*&quot; will not detect any files if the first directory entry is empty or holds a subdirectory, even if there are other files in the directory.

To see how to use IF EXIST to verify the existence of a disk drive or directory, query on the following words:

batch and testing and directory and exists

Additional reference words: 3.20 3.21 3.30 3.30a 4.00 4.01 noupd

Copyright Microsoft Corporation 1993.