[英]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. 在过去的6个小时里,我已经阅读了该论坛上的其他相关帖子,这些帖子与我的问题很接近,但是还没有找到解决方案。
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. 我正在寻找一个批处理文件来检查目录是否存在,如果存在,请在该目录中执行一个exe文件。 If the directory does not exists I wish it to check another directory for the same exe file and execute it.
如果该目录不存在,则希望它检查另一个目录中的相同exe文件并执行它。 Furthermore I need to be able to execute the exe file with certain parameters.
此外,我需要能够使用某些参数执行exe文件。
For example: 例如:
Directory number 1 to check: C:\\Program Files\\Test\\ if it exists execute Test.exe 要检查的目录号1:C:\\ Program Files \\ Test \\(如果存在)执行Test.exe
and if this doesn't exist.. 如果不存在
Directory number 2 to check: C:\\Program Files\\Test2\\ if it exists execute Test.exe 要检查的目录号2:C:\\ Program Files \\ Test2 \\(如果存在),执行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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.