简体   繁体   English

在命令提示符下执行时无法在批处理文件中运行命令

[英]Can't run a command in a batch file while in command prompt it executes

Been stuck on this for a while now, I've tried finding the answer on this and similar forums but nothing worked. 在此问题上停留了一段时间,我尝试在此论坛和类似论坛上找到答案,但是没有任何效果。

I got this command: 我得到以下命令:

 FOR /F "tokens=3 skip=1" %i IN (C:\logoff\query.txt) DO logoff %i /server:192.168.0.231

Where basically it takes number from query.txt file and it loggs off the user from server by his session id number. 基本上,它从query.txt文件中获取数字,并通过其会话ID号将用户从服务器注销。

It works if I execute command in a command prompt, but I can't seems to get it working on a batch. 如果我在命令提示符下执行命令,它会起作用,但是我似乎无法使其批量运行。 I've tried doubling the percentage signs, like someone suggested in forums - nothing. 我已经尝试将百分比符号加倍,就像在论坛中建议的那样-什么都没有。

I assume to confirm the command should work on batch it should execute using "Run...", it doesn't though.. 我假设确认命令应可批量使用,但应使用“运行...”执行,但不是。

Is the problem that the command starts with FOR or what is the case? 命令以FOR开头的问题是什么? I give up.. 我放弃..

The for replaceable parameters (the "variable" containing the current value of the iteration) uses the %x syntax. for可替换参数(包含迭代当前值的“变量”)使用%x语法。 But inside a batch file, the percent sign needs to be escaped and the syntax is %%x (see for /? ). 但是在批处理文件中,百分号需要转义,语法为%%x (请参阅for /? )。

FOR /F "tokens=3 skip=1" %%i IN (C:\logoff\query.txt) DO logoff %%i /server:192.168.0.231
                         ^^                                     ^^

Try this: 尝试这个:

for /F "usebackq tokens=3 skip=1 delims=|" %%i in ("C:\logoff\query.txt") do logoff %%i /server:192.168.0.231

I thing you problem is like @MC ND said and you forget to use delims . 我的问题是,就像@MC ND所说的那样,您忘记使用delims了

in you query.txt must something like this: query.txt中必须是这样的:

1|1|111
2|2|222
3|3|333

output: 输出:

logoff 222 /server:192.168.0.231
logoff 333 /server:192.168.0.231

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM