批处理脚本循环& NSLOOKUP [英] Batch script for loop & nslookup
问题描述
我之前看过这个问题,但没有解决我的问题。我只是试图遍历包含主机名的文本文件。当我从命令行尝试相同的命令(省略额外的百分号)时,它将工作一两次然后给出下面指出的错误。将其作为批处理文件运行,批处理文件将不做任何事情而退出。
以下是批处理文件中的代码:
@echo off
set OUTPUTFILE = Results / Results.txt
set lookup = HostNames.txt
FOR / F %% i in(%lookup%)do
FOR / Fusebackq skip = 3 delims =:tokens = 2%j in(`nslookup%i`)
do @echo %% i %% j>> %OUTPUTFILE%
在命令行中,我得到:
)在这个时候是意外的。
当我在一个命令当我在批处理文件中运行时,我正在取出额外的百分号。我也在命令行中使用绝对路径,以确保它不是我设置的环境变量的问题。
解决方案 $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ in(%lookup%)do(FOR / Fskip = 3 delims =:tokens = 2%% j in('echo(^ | nslookup %% i')do @echo %% i %% j>>%OUTPUTFILE%
)
问题:
/
是winbatch中的一个开关; \
是一个目录级别分隔符。
do(
必须与在同一实体行上
%i
应该是 %% i
%j
应该是 %% j
我已经删除了 usebackq
,并将引号从反引号改为了单引号,因为它没有必要使用 usebackq
here。
(添加了一些小修正)
闪烁的光标是由 由于我的设置与您的设置有很大的不同,我只能做原始验证。 I've seen this question posted before, but none of the solutions address my issue. I'm simply trying to iterate through a text file that contains host names. When I try the same command (omitting the extra percent signs) from the command line, it will work once or twice then then give the error noted below. Running it as a batch file, the batch file exits without doing anything. This one has really stumped me. Here's the code in my batch file: At a command line I get: i`) was unexpected at this time. When I run it at a command line I am taking out the additional percent signs needs when it runs in a batch file. I'm also using absolute paths at the command line, to ensure it this isn't an issue with the environment variables I've set. Problems:
I've removed the (minor fixes added) The "flashing cursor" is caused by Since my setup differs radically from yours, I can do but primitive verification. 这篇关于批处理脚本循环& NSLOOKUP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! nslookup
从键盘请求信息。添加
echo(
提供换行符来终止 nslookup
和 ^ |
是一个将 echo
的输出指向 nslookup
的输入的转义管道。 b
$ b @echo off
set OUTPUTFILE=Results/Results.txt
set lookup=HostNames.txt
FOR /F %%i in (%lookup%) do
FOR /F "usebackq skip=3 delims=: tokens=2" %j in (`nslookup %i`)
do @echo %%i %%j >> %OUTPUTFILE%
@echo off
set OUTPUTFILE=Results\Results.txt
set lookup=HostNames.txt
FOR /F %%i in (%lookup%) do (
FOR /F "skip=3 delims=: tokens=2" %%j in ('echo(^|nslookup %%i') do @echo %%i %%j >> %OUTPUTFILE%
)
/
is a switch in winbatch; \
is a directory-level-separator.do (
must be on the same physical line as its for
%i
should be %%i
%j
should be %%j
usebackq
and changed the quotes from backticks to single-quotes as it's not necessary to use usebackq
here.
nslookup
requesting information from the keyboard. Adding echo(
supplies a newline to terminate nslookup
and ^|
is an escaped-pipe which directs the output of that echo
to the input of nslookup
.