为变量管道添加“if argument”

时间:2013-03-09 06:42:53

标签: 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

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

7 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

因为“cd xxxx”中的“not-found-answer”会因Windows安装的语言而异,所以您应该选择与语言无关的解决方案。 使用%errrorlevel%。

cd c:\existingpath\将返回错误级别“0”

cd c:\notexistingpath\将返回“1”

所以一个好的解决方案是:

SET "mypath=%1"
ECHO %mypath%
cd %mypath% 
If %errorlevel%=0 goto BeginTest else goto error1

...

答案 2 :(得分:1)

很有效,谢谢。以下是我遇到的其他问题:

::Conditions leading to errors if the batch script is not executed correctly
@ECHO OFF
SET "source=%1"
ECHO %source%
IF NOT EXIST "%source%" goto error1
ECHO Directory Exists
cd %source%

goto end
:error1
echo Error: Invalid Path
goto end
:end

现在我需要一个真正IP地址的if条件

::Conditions leading to errors if the batch script is not executed correctly
@ECHO OFF
SET "IP_Address=%1"
ECHO %IP_Address%

::Checks if the IP Address is valid
IF NOT EXIST "%IP_Address%" goto error2
ECHO Directory Exists

goto end
:error1
echo.
echo Error: Invalid Path
echo %source%
goto end
:error2
echo.
echo Error: Invalid IP Address
echo %IP_Address%
goto end
:end

它会自动检查IP地址是否为路径。我可以在“IF NOT EXIST”\\%IP_Address%“goto error2”前添加“\\”,但问题是计算机将无法连接到设备,除非它使用任一telnet ,或通过powershell的“Open-Device”命令。但它可以ping该IP地址。

是否有办法设置读取成功的ping与不成功的ping ??

答案 3 :(得分:1)

%errorlevel%

的相同解决方案
ping -n 1 -w 100 %ip-address%
if %errorlevel%=0 then goto doesexist else goto cannotreach

...

所有命令都将返回错误级别。通常为零,如果一切正常,则为非零值的错误。

答案 4 :(得分:0)

它仍然无法正常使用IP地址列表,但我更接近你的帮助。以下是我使用有效的IP地址执行bat来ping时的内容:

::Conditions leading to errors if the batch script is not executed correctly
@ECHO OFF
SET IP_Address=%1
ECHO %IP_Address%

::Checks if the IP Address is valid
ping -n 1 -w 1000 %IP_Address%
if not "%errorlevel%=0" goto error2

goto end
:error2
echo.
echo Error: Invalid IP Address
echo %IP_Address%
goto end
:end

执行时,这是我在cmd中看到的:

c:\>Random.bat 192.168.1.46
192.168.1.46

Pinging 192.168.1.46 with 32 bytes of data:
Reply from 192.168.1.46: bytes=32 time=1ms TTL=128

Ping statistics for 192.168.1.46:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 1ms, Maximum = 1ms, Average = 1ms
goto was unexpected at this time.

一旦解决了这个问题,我也想知道一种隐藏输出的方法:

Pinging 192.168.1.46 with 32 bytes of data:
Reply from 192.168.1.46: bytes=32 time=1ms TTL=128

Ping statistics for 192.168.1.46:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 1ms, Maximum = 1ms, Average = 1ms

答案 5 :(得分:0)

隐藏输出使用> nul

ping -n 1 -w 1000 %IP_Address% 1>nul 2>nul

1>nul会将“正常”输出发送给nirvana

2>nul会将错误消息发送给nirvana

因此,如果您使用“1> nul”(或只是“> nul”),您将在屏幕上显示错误消息,但不会显示“回复”,“统计信息”以及此类内容。

我现在不明白“意外的转到” - 明天我会看一下。

答案 6 :(得分:0)

啊我查找了errorlevel的用法

后终于找到了它
::Conditions leading to errors if the batch script is not executed correctly
@ECHO OFF
SET IP_Address=%1
ECHO %IP_Address%

::Checks if the IP Address is valid
ping -n 1 -w 100 %IP_Address% 1>nul
if "%errorlevel%"=="1" goto error2

:error2
echo.
echo Error: Invalid IP Address
echo %IP_Address%
goto end
:end

感谢大家的帮助。

相关问题