批处理脚本“for /f ...”
for /F "skip=n tokens=3 delims= " %%i in (myfile.txt) do echo %%i
skip=n ...
是否可以是像 skip=%test% ...
这样的变量,其中 %test%
有一个整数值?
因此,我尝试添加一列数据,并且该表在文件中的位置由字符串给出。
例如:
$startTable
0 1 4
1 2 4
2 1 4
$endTable
因此该表的位置由与 $startTable 关联的行号给出。我将其值存储在变量(!test!)中,所以我需要skip=!test!然后我开始添加第三列,直到点击 $endTable。
当我尝试时;
for /f "skip=!test! tokens=3 delims= " %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "$endTable" goto :break
set /a test2+=%%j
)
:break
我收到以下错误;
!test! tokens=3 delims= " was unexpected at this time.
-GK
for /F "skip=n tokens=3 delims= " %%i in (myfile.txt) do echo %%i
Is it possible for skip=n ...
to be a variable like skip=%test% ...
where %test%
has an integer value?
So I'm trying to add a column of data and the location of this table in the file is given by a string.
For Eg:
$startTable
0 1 4
1 2 4
2 1 4
$endTable
So the location of this table is given by the line number associated with $startTable. I have the value of this stored in a variable(!test!) so I need the skip=!test! and then I start adding the third column until I hit $endTable.
When I try;
for /f "skip=!test! tokens=3 delims= " %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "$endTable" goto :break
set /a test2+=%%j
)
:break
I get the following error;
!test! tokens=3 delims= " was unexpected at this time.
-GK
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
for 不喜欢在参数中采用延迟扩展语法。那应该没问题,因为你不需要它。请改用标准的 % 包装变量 - 这就是 Michael 和 Jebego 的示例所使用的。
如果您确实想使用延迟扩展版本,则需要一个临时变量来执行此操作:
编辑:这是我正在运行的内容,试图与原始参数名称保持接近。我更改了输入以显示 %j 正在更新以及解析了哪些行。
stackoverflow_input.txt
stackoverflow1.bat
stackoverflow2.bat
结果(在 Win 7、Server 2008R2、Server 2003 和 Win XP SP3 上验证相同):
如果禁用命令扩展,您将得到:
/f was意外在此时间。
for doesn't like to take the delayed expansion syntax inside the parameters. That should be fine, since you don't need it. Use the standard % wrapped variable, instead - that's what Michael and Jebego's examples are using.
If you really want to use the delayed expansion version, you'll need a temp variable to do it:
EDIT: Here's what I'm running, trying to stay close to your original parameter names. I changed the input to show that %j is updating and which rows are parsed.
stackoverflow_input.txt
stackoverflow1.bat
stackoverflow2.bat
Results (verified same on Win 7, Server 2008R2, Server 2003, and Win XP SP3):
If command extensions are disabled, you'll instead get:
/f was unexpected at this time.
是的。
这将跳过前两行。您可能只是尝试添加变量!
Yes.
This will skip the first two lines. You could have just tried to add the variable!
是的。这将回显 test.txt 的第四行及后续行
Yes. This will echo the 4th and subsequent lines of test.txt