windows - 针对变量的管道添加 "if arguement"

标签 windows batch-file

我是编写代码和批处理文件的新手。我正在尝试通过允许用户发出以下批处理文件来自动执行用户设置:

执行的命令 = "BVT_AudioDecode_Setup.bat V:\WP\BVT\Audio\Decode 10.42.233.237"

@echo off
::Conditions leading to errors if the batch script is not executed correctly
if "%1"=="" and "%2"=="" goto error1
if "%1"=="" goto error2
if "%2"=="" goto error3

::Allows user to set the "Source Path" to copy the testcase files from
set source=%1
::Allows user to set the "Destination Path" to copy the testcase files from
set IP_Address=%2

::Tells the user what the "Source" and "Destination" Paths are
echo Source Path = %1
echo Destination Path = %2
echo.
powershell.exe -File "bat_script.ps1" %1 %2 -NoProfile -NoExit


goto end
:error1
echo.
echo Error Syntax: BVT_AudioDecode_Setup.bat "Source_Path\AudioDecode_Testcase_Folder" "Device IP Address"
echo.
echo For example: BVT_AudioDecode_Setup.bat V:\WP\BVT\Audio\Decode 10.42.233.237
echo              -or-
echo              BVT_AudioDecode_Setup.bat C:\WP\BVT\Audio\Decode 10.42.233.237
echo.
echo.
goto end
:error2
echo.
echo Error Syntax: BVT_AudioDecode_Setup.bat "Source_Path\AudioDecode_Testcase_Folder" "Device IP Address"
echo.
echo For example: BVT_AudioDecode_Setup.bat V:\WP\BVT\Audio\Decode 10.42.233.237
echo              -or-
echo              BVT_AudioDecode_Setup.bat C:\WP\BVT\Audio\Decode 10.42.233.237
echo.
echo.
goto end
:error3
echo.
echo Error Syntax: BVT_AudioDecode_Setup.bat "Source_Path\AudioDecode_Testcase_Folder" "Device IP Address"
echo.
echo For example: BVT_AudioDecode_Setup.bat V:\WP\BVT\Audio\Decode 10.42.233.237
echo              -or-
echo              BVT_AudioDecode_Setup.bat C:\WP\BVT\Audio\Decode 10.42.233.237
echo.
echo.
goto end
:end

这已经过测试并且有效。

现在我正在尝试添加/测试一个简单的代码块,但找不到正确的语法。是的,过去 3 个小时我一直在谷歌搜索。这是我正在尝试做的事情:

执行的命令 = "Random.bat C:\Users"

执行的命令 = "Random.bat C:\InvalidDirectory"

::Conditions leading to errors if the batch script is not executed correctly
@ECHO OFF
SET "mypath=%1"
ECHO %mypath%
cd %mypath% | find "%invalidpath%"
Echo %invalidpath%

If "%invalidpath%=The system cannot find the path specified." (
goto error1)
else (
goto BeginTest)

::Begins the testcase
:BeginTest
::Do some stuff
goto end
:error1
echo Error: Invalid Path
goto end
:end

如果有人能指出我正确的方向,将不胜感激...

最佳答案

您可以使用 IF EXIST 语句来检查目录是否存在。例如,要测试 C:\WIN,如果存在则更改为 C:\WIN:

C:
IF NOT EXIST "C:\WIN\." GOTO NOWINDIR
CD \WIN
:NOWINDIR

关于windows - 针对变量的管道添加 "if arguement",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15308075/

相关文章:

c++ - Linux相当于windows的EnterCriticalSection

windows - 如何连接 Azure 存储以从 Blob 存储读取 .txt 文件

windows - 谁负责在处理 TB_GETBUTTONTEXT 时放置空终止符?

powershell - 如何使用powershell运行批处理文件

windows - 在windows批处理中打印一段

windows - 使用 libssh 库时,我遇到了 ssh_scp_read 函数的问题

java - 在 Windows 7 SP1 上安装 Hadoop 2.7.1 时出现 CMake 错误

windows - 使用 bat 文件扫描新硬件?

java - 我如何将这个 bat 文件转换为 linux 加载 java 程序的 shell 脚本

windows - 如何在管理员模式下执行批处理文件