Microsoft KB Archive/68929

Keeping Track of Current Drive Within a Batch File PSS ID Number: Q68929 Article last modified on 02-05-1993 PSS database name: O_MSDOS

3.20 3.21 3.30 3.30a 4.00 4.01

MS-DOS

Question:

I have a batch file that changes the current drive during its execution. How can I return the computer to the drive it was on before terminating the batch file?

Response:

Below is an example of how to use the MS-DOS DEBUG utility to create a program file called CDRIVE.COM. CDRIVE.COM will return an errorlevel, which can be used to determine the current drive within a batch file. For example:

Errorlevel Current Drive ———- ————-

0                          A:   1                           B:   2                           C:   .. .                          . Follow these steps exactly to create CDRIVE.COM:

  Create a DEBUG script file C:.SCR using a file editor. (MS-DOS provides a file editor known as EDLIN. For further information on the use of EDLIN, refer to your “Microsoft MS-DOS User’s Guide and User’s Reference”). C:.SCR should contain exactly the following lines (where  means to press the the ENTER key): a 100 mov ah,19 int 21 mov ah,4c int 21  r cx 0008 n cdrive.com w q   The following command will cause DEBUG to create the CDRIVE.COM program: C:>DEBUG < CDRIVE.SCR DEBUG will place the CDRIVE.COM file in the current directory, which in this example will cause it to be created in the C: directory. </li></ol>

Now you may call the CDRIVE.COM program from within a batch file to get information about the current drive. By making use of environment variables, you can store information about the current drive for later use. Below is an example of such a batch file:

c:.com if errorlevel 2 goto C if errorlevel 1 goto B if errorlevel 0 goto A goto END


 * C set cdr=C: goto NEXT


 * B set cdr=B: goto NEXT


 * A set cdr=A:


 * NEXT

REM ************************** REMARK ***************************** REM * Now you can change to any other drive, and then restore to the  REM  previous drive by referencing the environment variable cdr.  REM ************************* REMARK *****************************


 * RESTORE %cdr%


 * END

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

Copyright Microsoft Corporation 1993.