Batch file to get the cd drive and system drive

Can anyone guide on to create a batch file (Ex Application.bat) for the following conditions.

I need to Execute a open a TestProject from a Test Automation tool and run the automated tests.
Command I am using in the Application.bat file is,

"C:\Program Files\AutomationTool.exe" "G:\ProjectSuite.Pjs" /r /e

Where "C:\Program Files\AutomationTool.exe" is the path where the TestAutomation Tool is installed and "G:\ProjectSuite.Pjs" is the path where the Script Project is available.

On running Application.bat file command is executing fine.

But in the above code, "C:\" and "G:\" drive is hardcoded, Where C:\ is system drive and G:\ is Cd/DVD drive.So the batch file will not run other system, User has to manually the exe path of Testautomation tool and Cd rom path.

Please suggest the command to get the 'system drive' and 'Cd drive' inside the .bat file.
So that the .bat file can be executed in any system without editing.

Thanks in Advance
5 answers Last reply
More about batch file drive system drive
  1. The first is easy - you want to find the "Program Files" location - this is already known in the environment variable "PROGRAMFILES". You don't really want to know the letter of the CD drive (after all, there may be more than one) you need to know the location of the .Pjs file. I'd suggest something like:

    echo off
    for /f "tokens=3 delims=\:" %%k in ('reg query hklm\system\mounteddevices^') do if exist %%k:ProjectSuite.Pjs set CDROM=%%k
    %PROGRAMFILES%\AutomationTool.exe %CDROM%:\ProjectSuite.Pjs /r /e
  2. Hi,
    Thanks for the Reply.
    But I am facing some problem in executing the batch file

    Following is the step for running a exe.
    1. Created a new .bat file say by name 'New.bat'
    2. Right clicked on it and selected Edit option.
    3. Entered the following command line
    echo off
    for /f "tokens=3 delims=\:"%%k in ('reg query hklm\system\mounteddevices^') do if exist %%k:Setup.exe set CDROM=%%k
    "%CDROM%:\Setup.exe" RUN
    Where Setup.exe is in the CD drive.
    Saved the Batch file and closed it.
    Double clicked on batch file and tried executing .It seems to be not working.command prompt window appeard and disappears in a moment.

    4.Again opened Command prompt by entering "CMD" in the Run prompt
    5.COmmand prompt window opened.
    Executed first line,
    echo off
    Then second line,
    for /f "tokens=3 delims=\:"%%k in ('reg query hklm\system\mounteddevices^') do if exist %%k:Setup.exe set CDROM=%%k
    Then the last line,
    "%CDROM%:\Setup.exe" RUN
    Setup.exe got invoked.
    It means when i run these commands step by step it is working. But when all these commands are executed once for all from a batch file it is not working.
    Can you please suggest the solution for this.

  3. What output do you get if you open a command prompt and try to run the batch file from there, rather than the individual commands? It's probably best to remove the "echo off" line for this, so that you can see what is happening.
  4. Hi,
    I did small changes in the above code.Now batch file is running an exe where exe is directly present in the CD drive.
    I have one more query What if the Setup.exe is in a Folder inside the exe.How to run this from Batch file.
    Following is the code for running a file which is residing in folders in the Cd drive
    Echo off
    for /f "tokens=3 delims=\:" %%k in ('reg query hklm\system\mounteddevices^') do if exist %%k:Important Documents\Application\02.00.Beta1_MPXSetup.exe set CDROM=%%k
    "%CDROM%:\Important Documents\Application\02.00.Beta1_MPXSetup.exe" RUN.

    The above code is not working. Is my code correct.If not please suggest a better code for this.

  5. The "RUN." at the end of the last line shouldn't be there. Otherwise this should run OK (although you may need to be Administrator to run the program). I'd suggest, again, that you run it from the command line without the "echo off" line to see exactly what it is trying to do.

    Edit: Just noticed one more thing (my mistake). End of second line should read:

    do if exist %%k:\Important Documents\Application\02.00.Beta1_MPXSetup.exe set CDROM=%%k

    i.e. there should be a backslash after the colon (otherewise it may sometimes work and sometimes not).
Ask a new question

Read More

Command Prompt Windows XP