DOS批处理文件-循环并加1
我有一个批处理文件,它登录到远程计算机上的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会做你想做的。 /L 说明符告诉它像常规的 for 循环编程一样。第一个参数是起始整数,下一个参数是要步进的数字,最后一个参数是计数。所以这将从 1 开始循环,以 1 递增 6 个整数:
如果您想要列出 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:
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;)
假设您实际上使用的是
cmd.exe
而不是 MS-DOS,递增和测试变量的一种方法如下:这是您需要的稍微修改的版本,它与相关位而不是执行它们,它输出:
只需将
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:This is a slightly modified version of what you need which echoes the relevant bits rather than executing them, and it outputs:
Simply take the
echo
off the line and adjust the command to put back the other bits.如果您有权访问 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.