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」からの「not-found-answer」は、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」コマンド。ただし、その IP アドレスに対して ping を実行することはできます。

成功した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

ああ、エラーレベルの使用法を調べた後、ついにそれを見つけました

::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 に答える
0

出力を非表示にするには >nul を使用します

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

1>nul「通常の」出力をニルヴァーナに送信します

2>nulエラーメッセージを nirvana に送信します

したがって、" 1>nul" (または単に " >nul") を使用すると、画面にエラー メッセージが表示されますが、"応答" や "統計" などは表示されません。

「予期しない goto」が今のところ理解できません。それについては明日調べます。

于 2013-03-09T21:26:47.430 に答える
0

IP アドレスのリストではまだ正しく動作していませんが、あなたの助けでより近くなりました. ping を実行する有効な IP アドレスを使用してバットを実行すると、次のようになります。

::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 に答える