2011-02-01 21 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批处理文件。