2011-02-01 22 views
2

我有這個批處理文件,在遠程計算機上登錄到SQL運行存儲過程,然後將輸出發送到文本文件。我希望它將IP地址中的第3個八位字節和輸出文本文件的名稱加1並循環,因此我不必一遍又一遍地重複該命令。另外,我希望它在達到一定數量時停止。有沒有辦法做到這一點?DOS批處理文件 - 循環並增加1

sqlcmd -U user -P password -S 192.168.1.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput01.txt 
sqlcmd -U user -P password -S 192.168.2.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput02.txt 
sqlcmd -U user -P password -S 192.168.3.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput03.txt 
sqlcmd -U user -P password -S 192.168.4.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput04.txt 
sqlcmd -U user -P password -S 192.168.5.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput05.txt 
+1

你實際上是在談論DOS嗎?還是你在寫一個.BAT文件,這個文件將由當前的Windows系統cmd.exe執行?我猜這是後者。 – 2011-02-01 08:19:26

回答

2

在那你實際上使用cmd.exe而不是MS-DOS,一個方式來增加和測試一個變量是如下的假設:

@setlocal enableextensions enabledelayedexpansion 
    @echo off 
    set /a "i = 1" 
:loop 
    if !i! leq 15 (
     if !i! lss 10 (
      echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput0!i!.txt 
     ) else (
      echo sqlcmd -S 192.168.!i!.2 -o c:\sql\ouput!i!.txt 
     ) 
     set /a "i = i + 1" 
     goto :loop 
    ) 
    endlocal 

這是一個稍微修改的版本是什麼你需要呼應的相關位,而不是執行它們,它輸出:

sqlcmd -S 192.168.1.2 -o c:\sql\ouput01.txt 
sqlcmd -S 192.168.2.2 -o c:\sql\ouput02.txt 
sqlcmd -S 192.168.3.2 -o c:\sql\ouput03.txt 
sqlcmd -S 192.168.4.2 -o c:\sql\ouput04.txt 
sqlcmd -S 192.168.5.2 -o c:\sql\ouput05.txt 
sqlcmd -S 192.168.6.2 -o c:\sql\ouput06.txt 
sqlcmd -S 192.168.7.2 -o c:\sql\ouput07.txt 
sqlcmd -S 192.168.8.2 -o c:\sql\ouput08.txt 
sqlcmd -S 192.168.9.2 -o c:\sql\ouput09.txt 
sqlcmd -S 192.168.10.2 -o c:\sql\ouput10.txt 
sqlcmd -S 192.168.11.2 -o c:\sql\ouput11.txt 
sqlcmd -S 192.168.12.2 -o c:\sql\ouput12.txt 
sqlcmd -S 192.168.13.2 -o c:\sql\ouput13.txt 
sqlcmd -S 192.168.14.2 -o c:\sql\ouput14.txt 
sqlcmd -S 192.168.15.2 -o c:\sql\ouput15.txt 

乾脆把echo下線並調整comman d放回其他位。

8

這將做你想做的。/L說明符告訴它像循環的常規編程那樣工作。第一個參數是起始整數,下一個是步數,最後一個是計數。因此,這將循環從1開始,以1爲6點的整數遞增:

@echo off 
FOR /L %%G IN (1, 1, 6) DO (
    echo sqlcmd -U user -P password -S 192.168.%%G.2 -i c:\sql\storecreditfix.sql -o c:\sql\ouput0%%G.txt 
) 

,如果你想要做的IP地址列表,而不是一個範圍,你可以離開關/ L,只是列出的數字。例如 FOR %% G IN(1,2,3,99,121)DO ...

明顯的 「回聲」 之前SQLCMD只是爲了測試;)

+0

一旦你取出回聲,這應該工作,但你可能會考慮使用START/WAIT,這取決於你的查詢需要多長時間。 – Vinnie 2011-02-01 05:22:36

0

如果您有機會獲得cmd.exe的,那麼你也有權訪問cscript,它允許你用Javascript寫一個DOS批處理文件。