批处理脚本“for /f ...”

发布于 2024-11-16 03:33:13 字数 707 浏览 1 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

权谋诡计 2024-11-23 03:33:13

for 不喜欢在参数中采用延迟扩展语法。那应该没问题,因为你不需要它。请改用标准的 % 包装变量 - 这就是 Michael 和 Jebego 的示例所使用的。

如果您确实想使用延迟扩展版本,则需要一个临时变量来执行此操作:

set for_parameters="skip=!test! tokens=3 delims= "
for /f %for_parameters% %%j in (!INPUTFILE!) do ( echo %%j

编辑:这是我正在运行的内容,试图与原始参数名称保持接近。我更改了输入以显示 %j 正在更新以及解析了哪些行。

stackoverflow_input.txt

$startTable

0 1 3

1 2 4

2 1 5

$endTable

stackoverflow1.bat

@setlocal enabledelayedexpansion
@echo off
set INPUTFILE=stackoverflow_input.txt
set test=3
set test2=0
set for_parameters="skip=!test! tokens=3 delims= "
for /f %for_parameters% %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "#endTable" goto :break
set /a test2+=%%j
)
:break
echo Sum: %test2%
endlocal

stackoverflow2.bat

@setlocal enabledelayedexpansion
@echo off
set INPUTFILE=stackoverflow_input.txt
set test=3
set test2=0
for /f "skip=%test% tokens=3 delims= " %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "#endTable" goto :break
set /a test2+=%%j
)
:break
echo Sum: %test2%
endlocal

结果(在 Win 7、Server 2008R2、Server 2003 和 Win XP SP3 上验证相同):

D:\temp>stackoverflow1.bat
4
5
Sum: 9

D:\temp>stackoverflow2.bat
4
5
Sum: 9

如果禁用命令扩展,您将得到: /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:

set for_parameters="skip=!test! tokens=3 delims= "
for /f %for_parameters% %%j in (!INPUTFILE!) do ( echo %%j

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

$startTable

0 1 3

1 2 4

2 1 5

$endTable

stackoverflow1.bat

@setlocal enabledelayedexpansion
@echo off
set INPUTFILE=stackoverflow_input.txt
set test=3
set test2=0
set for_parameters="skip=!test! tokens=3 delims= "
for /f %for_parameters% %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "#endTable" goto :break
set /a test2+=%%j
)
:break
echo Sum: %test2%
endlocal

stackoverflow2.bat

@setlocal enabledelayedexpansion
@echo off
set INPUTFILE=stackoverflow_input.txt
set test=3
set test2=0
for /f "skip=%test% tokens=3 delims= " %%j in (!INPUTFILE!) do (
echo %%j
if %%j == "#endTable" goto :break
set /a test2+=%%j
)
:break
echo Sum: %test2%
endlocal

Results (verified same on Win 7, Server 2008R2, Server 2003, and Win XP SP3):

D:\temp>stackoverflow1.bat
4
5
Sum: 9

D:\temp>stackoverflow2.bat
4
5
Sum: 9

If command extensions are disabled, you'll instead get: /f was unexpected at this time.

何处潇湘 2024-11-23 03:33:13

是的。

set num=2
for /F "skip=%num% tokens=3 delims= " %%i in (myfile.txt) do echo %%i

这将跳过前两行。您可能只是尝试添加变量!

Yes.

set num=2
for /F "skip=%num% tokens=3 delims= " %%i in (myfile.txt) do echo %%i

This will skip the first two lines. You could have just tried to add the variable!

青萝楚歌 2024-11-23 03:33:13

是的。这将回显 test.txt 的第四行及后续行

set sk=3  
for /f "skip=%sk% delims=" %%L in (test.txt) do (  
    echo %%L  
    )  

Yes. This will echo the 4th and subsequent lines of test.txt

set sk=3  
for /f "skip=%sk% delims=" %%L in (test.txt) do (  
    echo %%L  
    )  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文