1

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

执行的命令 = "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

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

4

7 回答 7

2

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

C:
IF NOT EXIST "C:\WIN\." GOTO NOWINDIR
CD \WIN
:NOWINDIR
于 2013-03-09T08:58:02.023 回答
2

因为“cd xxxx”中的“未找到答案”会随 Windows 安装的语言而变化,所以您应该更喜欢与语言无关的解决方案。为此使用 %errorlevel%。

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

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

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

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

...

于 2013-03-09T17:40:16.647 回答
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?

于 2013-03-09T14:53:36.063 回答
1

与 %errorlevel% 相同的解决方案

ping -n 1 -w 100 %ip-address%
if %errorlevel%=0 then goto doesexist else goto cannotreach

...

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

于 2013-03-09T17:47:01.067 回答
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
于 2013-03-09T20:42:17.497 回答
0

隐藏输出使用 >nul

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

1>nul将“正常”输出发送到涅槃

2>nul将错误消息发送到涅槃

因此,如果您使用“1>nul”(或仅使用“>nul”),您将在屏幕上看到错误消息,但不会出现“回复”、“统计”之类的东西。

我不明白刚才的“意外转到” - 我明天会看看。

于 2013-03-09T21:26:47.430 回答
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

感谢大家的帮助。

于 2013-03-09T23:14:50.933 回答