DOS批处理文件-循环并加1

发布于 2024-10-15 05:29:44 字数 634 浏览 0 评论 0原文

我有一个批处理文件,它登录到远程计算机上的 sql,运行一个存储过程,然后将输出发送到一个文本文件。我希望它将 IP 地址中的第三个八位字节和输出文本文件的名称都增加 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

I have this batch file that logs into sql on a remote machine runs a stored procedure and then sends the output to a text file. I would like it to increment both the 3rd octet in the IP address and the name of the output text file by 1 and loop so I don't have to repeat the command over and over. Also, I would like it to stop when it reaches a certain number. Is there a way to do this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情出卖 2024-10-22 05:29:44

这会做你想做的。 /L 说明符告诉它像常规的 for 循环编程一样。第一个参数是起始整数,下一个参数是要步进的数字,最后一个参数是计数。所以这将从 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 之前的“echo”只是用于测试;)

this will do what you want. the /L specifier tells it to act like a regular programming for loop. the first parameter is the starting integer, the next is the number to step, and the last is the count. so this will loop starting at 1, incrementing by 1 for 6 integers:

@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
)

and if you want to do a list of IPs instead of a range, you can leave off the /L and just list the numbers. e.g.
FOR %%G IN (1,2,3,99,121) DO ...

obviously the "echo" before sqlcmd is just for testing;)

战皆罪 2024-10-22 05:29:44

假设您实际上使用的是 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 从线路上取下并调整命令以放回其他位。

On the assumption that you're actually using cmd.exe rather than MS-DOS, one way to increment and test a variable is as follows:

    @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

This is a slightly modified version of what you need which echoes the relevant bits rather than executing them, and it outputs:

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

Simply take the echo off the line and adjust the command to put back the other bits.

灰色世界里的红玫瑰 2024-10-22 05:29:44

如果您有权访问 cmd.exe,那么您还可以访问 cscript,它允许您用 Javascript 编写 DOS 批处理文件。

If you have access to cmd.exe then you also have access to cscript which allows you to write a DOS batch file in Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文