Need to pass current drive letter to various batch files.

Posted on 2009-02-24
Last Modified: 2012-08-13
I am running system commander on a laptop to allow me to use XP or DOS 6.22 for differant applications. Some of the bacth files I run from Dos I would like to run from XP. My problem is when booting from XP the batch files are on drive e & f. When booting from dos they are on drive d & e. I am trying to identify the drive drive I start from and then Increment to the correct drive using dos variable to allow the batch files to run correctly. I can get the drive letter in an xp batch file, but it doesn't run in dos. I can also get the drive letter in a "C" file, but don't know how to pass it to a dos variable.
Question by:cmoore1
    LVL 67

    Expert Comment

    In pure DOS, you can try to do something like

    for %%D in (C D E F G) do if exist %%D:somefile set drive=%%D

    The somefile has to be a file which exists only on the drive you want to use.
    LVL 39

    Accepted Solution

    You could put the folder where the batches reside into PATH environment variable (define it in autoexec.bat for DOS and System Settings - System - Advanced - Environment Variables for XP) . Then you could call them without drive letter.

    Or you do:

    @echo off
    set DRIVE=C:
    dir %DRIVE%\mybatch.bat
    if errorlevel 1 goto D_DRIVE
    goto DONE
    set DRIVE=D:
    dir %DRIVE%\mybatch.bat
    if errorlevel 1 goto E_DRIVE
    goto DONE

    LVL 21

    Expert Comment

    XP(windows) sets up many more environment variables than DOS 6.22 I would check for one of the windows variable and adjust accordingly.

    if defined systemroot (Set BatchFiles=E:\BatchFiles) Else (Set BatchFiles=D:\BatchFiles)

    DOS 6.22
    Set BatchFiles=D:\BatchFiles

    Setup a system environment variable
    REG ADD "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v "BatchFiles" /t REG_SZ /f /d "E:\BatchFiles"
    LVL 21

    Expert Comment

    Oh. One more thing your batch files on d & e or e & f could call it's own drive letter with


    %0 inside your batch file is your batch file typically how you invoked it.

    Try some of these:
    ECHO How did I call this batch file = %0
    ECHO Show me the full path of the batch file = %~f0
    ECHO Show me the drive the batch file is on = %~d0
    ECHO Show me the path the batch file is on = %~p0
    ECHO Show me the drive and path the batch file is on = %~dp0
    ECHO Show me the drive, path and the filename without extension = %~dpn0
    ECHO Show me the file extension = %~x0

    For more %~ check out "for /?"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Are end users causing IT problems again?

    You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

    Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
    NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now