简体   繁体   中英

Batch file directories and exe files

I've read related posts on this forum an others for the past 6 hours which are close to my question but haven't managed to figure out a solution.

I'm looking to make a batch file to check if a directory exists, and if it does, execute an exe file within that directory. If the directory does not exists I wish it to check another directory for the same exe file and execute it. Furthermore I need to be able to execute the exe file with certain parameters.

For example:

Directory number 1 to check: C:\\Program Files\\Test\\ if it exists execute Test.exe

and if this doesn't exist..

Directory number 2 to check: C:\\Program Files\\Test2\\ if it exists execute Test.exe

Hope the information is sufficient, thanks in advance!

if exist c:\windows echo true

This will match a file or folder. to see if a folder or not

pushd c:\windows&if not errorlevel 1 echo Is a folder

But why not just direct instead of stupidly testing things.

C:\windows\notepad.exe&if not errorlevel 1 c:\windows\system32\notepad.exe

or

c:\windows\notepad.exe||c:\windows\system32\notepad.exe||Echo Neither File Found

& seperates commands on a line.

&& executes this command only if previous command's errorlevel is 0.

|| (not used above) executes this command only if previous command's errorlevel is NOT 0

> output to a file

>> append output to a file

< input from a file

| output of one command into the input of another command

^ escapes any of the above, including itself, if needed to be passed to a program

Try a different approach if you want to check for a specific path ..

  @echo off

  Set path1=C:\Program Files\Test

  Set path2=C:\Program Files\Test2

  setlocal enabledelayedexpansion

  for /r c: %%a in (*) do (

          call :check "%%~pa"

  )

  :check

  If "%~1"=="!path1!" (

        Call "!path1!\test.exe"

  )

  If "%~1"=="!path2!" (

         Call "!path2!\test.exe"

  )

Test this:

@echo off
if exist "C:\Program Files\Test\" (
   cd /d "C:\Program Files\Test\"
   "text.exe" param1 param2
 ) else (
   if exist "C:\Program Files\Test2\" cd /d "C:\Program Files\Test2\" & "text.exe" param1 param2
)
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM