1

こんにちは、以下の要件があります
コマンドラインから入力を提供するようにユーザーに促すことにより、3つのパラメーターを入力として受け取るバッチファイル

1) サーバー名
2) 開始
日 3) 終了日

バッチ ファイルは、次の方法でプロンプトを表示する必要があります。

Prompt Enter servername
Prompt Enter startdate
Prompt Enter enddate
Prompt user to ask if want to add one more set , IF enter Y , repeats the same 3 prompts, else, starts executing execute with single dataset 
Ex:-user enters server1 , 20130101 , 20130930 
asks to enter Y or N
If Y user enters server2 , 20130101 , 20130930 ,                                         asks to enter Y or N
If N, continues with 2 datasets

ここで、2 つのデータセットで同じロジックを繰り返す for ループをバッチ ファイルに記述する必要があります。これを達成する方法を教えてください。私はWindows 7マシンで作業しています

4

3 に答える 3

0

おそらく、for ループではなく do ループが必要です。バッチには do ループはありませんが、Goto コマンドと Goto :Eof コマンドを使用してシミュレートできます。

タイプ

goto /?
set /?
choice /? (choice was removed from windows in XP or 2000, then upgraded, and put back in Vista)
if /?
call /?

そうです(テストは高い数値から低い数値まで行う必要があることに注意してください)。また、あなたが何を求めているのかよくわからないので、YまたはNが実際に何をすべきかを理解できません. したがって、N は別のコード ブロックを実行し、Y は再度質問します。

:StartLoop
set /p sServerName=Enter Server Name
set /p sStartDate=Enter Start date
set /p sEndDate=Enter End date
choice /m "Enter Yes or No."
Rem Testing for N
if errorlevel 2 Goto AnotherBlockOfCode
if errorlevel 1 Goto StartLoop
goto :eof

:AnotherBlockOfCode
echo You chose No
goto :eof

ジャンプするのではなくサブする必要がある場合は、call コマンドを使用します。

于 2013-10-16T21:13:36.477 に答える
0
@echo off

set counter=0

:StartLoop
set /a counter=counter + 1

set /p sServerName%counter%=Enter Server Name
rem    set /p sStartDate%counter%=Enter Start date
rem    set /p sEndDate%counter%=Enter End date

set ss

If "%counter%"=="3" Goto AnotherBlockOfCode

choice /m "Enter Yes or No."
Rem Testing for N
if errorlevel 2 Goto AnotherBlockOfCode
Rem Testing for Y
if errorlevel 1 Goto StartLoop
goto :eof

:AnotherBlockOfCode
echo.
echo.
echo You chose No or already chosen 3 servers
echo.
goto :eof
于 2013-10-16T23:36:54.160 に答える
0

データ入力を取得する別の方法を次に示します。

バッチ ファイルは、データ入力を終了する方法を示し、次にデータ セットの入力を求めます (入力された 2 つのデータ セットを示しました)。次に、ループを使用して、入力されたデータ セットを表示します。

無制限の数のデータ セットを入力できます。

画面表示は次のとおりです。

Press enter when you have finished
Enter data in this format: servername,startdate,enddate \\server1,20010301,20010331
Enter data in this format: servername,startdate,enddate \\server2,20030102,20030103
Enter data in this format: servername,startdate,enddate

"servername=\\server1"
"startdate=20010301"
"enddate=20010331"

"servername=\\server2"
"startdate=20030102"
"enddate=20030103"

Press any key to continue . . .

コードは次のとおりです。コメントからの追加機能で編集されています

@echo off
del data.csv 2>nul
set "MyLogFile=c:\temp3\copy.log"
set "targetdir=c:\temp3"
echo Press enter when you have finished
:loop
   set "data="
   set /p "data=Enter data in this format: servername,startdate,enddate "
      if defined data (
         >>data.csv echo %data%
         goto :loop
      )
for /f "tokens=1,2,3 delims=," %%a in (data.csv) do (
   rem echo.
   rem echo "servername=%%a"
   rem echo "startdate=%%b"
   rem echo "enddate=%%c"
  pushd "%%a"
   for %%x in (*web_feed.out.gz) do (
      for /f "delims=." %%y in ("%%x") do (
         echo comparing server "%%a" : file "%%x"
         if %%y geq %%b if %%y leq %%c (
              echo      copying "%%x"
              echo              - date range %%b to %%c and %%y found
              copy "%%x" "%targetDir%\%%x" >> "%MyLogFile%"
         )
      )
   )
  popd
 )
pause

このファイルdata.csvは、データ エントリのセットを格納するために使用され、入力されたデータのセットをエコーするために data.csv を読み取る for-in-do ループで使用されます。

2 行目は、data.csv が存在する場合は削除し、以前のデータ セットが再利用されないようにします。

于 2013-10-17T03:57:58.307 に答える