批次:“ FOR” cmd部分起作用,“ skip =”选项不起作用 [英] Batch: 'FOR' cmd works partially, 'skip=' option not working

查看:131
本文介绍了批次:“ FOR” cmd部分起作用,“ skip =”选项不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个设置特定值的脚本,然后将每个值写入文本文档的新行中。之后,它应该阅读文档并将新值设置为文本文档的指定行,然后将其回显。

I want to create a script that sets specific values, then writes each value into a new line of a text document. After that it should read the document and set new values to a specified line of the text document, then echo those out.

我为 skip =# 这并没有改变任何东西。当我尝试在第一个 FOR 中不使用 skip = 0选项时,这会使批处理对所有值回显值三。 (快速编辑:到目前为止,我已经使用此网站获取有关该信息。)

I have tried different values for "skip=#" which didn't change anything. When I tried to not use the "skip=0" option in the first FOR and that makes the batch echo out "Value three" for all values. (Quick edit: I've used this website for information on it so far.)

@ECHO OFF
REM Setting values
SET @valueone=Value one
SET @valuetwo=Value two
SET @valuethree=Value three

REM Saving values
IF EXIST "values.txt" DEL "values.txt"
echo %@valueone% >values.txt
echo %@valuetwo% >>values.txt
echo %@valuethree% >>values.txt

REM Reading values again and echoing them at at the same time.
REM This was separated (first reading then echoing) but it didn't change anything.
FOR /F "skip=0 delims=" %%i IN (values.txt) DO SET @valueonefinal=%%i
echo Value number one:
echo %@valueonefinal%
echo.
FOR /F "skip=1 delims=" %%i IN (values.txt) DO SET @valuetwofinal=%%i
echo Value number two:
echo %@valuetwofinal%
echo.
FOR /F "skip=2 delims=" %%i IN (values.txt) DO SET @valuethreefinal=%%i
echo Value number three:
echo %@valuethreefinal%
pause

控制台中的预期输出:

Value number one:
Value one

Value number two:
Value two

Value number three:
Value three

实际输出:

 delims=" was unexpected at this time.
Value number one:
ECHO is off.

Value number two:
Value three

Value number three:
Value three

我没有那么丰富的经验,但是我怀疑我可能在 skip =#部分做错了。对此的任何帮助都非常感谢!

I'm not that experienced but I suspect that I may be doing the "skip=#" part wrong. Any help with this is greatly apprechiated!

推荐答案

选项 skip = 0 不被 用于/ F 命令 ,指定的数字必须在1到2 31 −之间。 1.要不跳过任何行,就是根本不提供 skip 选项。

The option skip=0 is not accepted by the for /F command, the specified number must be in the range from 1 to 231 − 1. To skip no lines just do not provide the skip option at all.

您似乎试图将某一行的文本分配给变量(例如,第三个):

You seem to try to assign the text of a certain line to a variable (for instance, the third one):

FOR /F "skip=2 delims=" %%i IN (values.txt) DO SET @valuethreefinal=%%i

实际上,这实际上将最后一行的内容分配给了变量,因为在命令行中的 set 命令除跳过的行外,所有循环均执行循环主体。更准确地说, for / F 循环遍历所有不以开头的非空行; eol 选项的默认字符。

Well, this actually assigns the content of the last line to the variable, because the set command in the body of the loop is executed for all but the skipped lines. More precisely said, the for /F loop iterates over all non-empty lines which do not begin with ; which is the default character of the eol option.

要将第三行实际分配给变量,您需要更改代码:

To actually assign the third line to the variable you need to change the code:

rem // Ensure that the variable is initially unset somewhere before:
set "@valuethreefinal="
rem // As soon as the variable is set the `if` condition is no longer going to be fulfilled:
for /F "usebackq skip=2 delims=" %%i in ("values.txt") do if not defined @valuethreefinal set "@valuethreefinal=%%i"

这不一定将第三行分配给变量,它实际上是将第一行的文本分配在不为空且不以开头的(两个)跳过的行之后; (记住 eol 字符)。

This does not necessarily assign the third line to the variable, it actually assigns the text of the first line after the (two) skipped ones that is not empty and does not begin with ; (remember the eol character).

usebackq 选项允许在文件名两边加上引号。在您的情况下,这不是必需的,但是当文件名包含 SPACE 或其他特殊字符时。

The usebackq option allows to put quotation marks around the file name. This is not necessary in your situation, but it is when a file name contains SPACEs or other special characters.

我使用了未记录的引用 set 语法在这里,因为这比未引用的语法更安全,尤其是在涉及特殊字符以及避免意外的尾随空格时。

I used the undocumented quoted set syntax here because this is safer than the unquoted one, particularly when it comes to special characters and also to avoid unintended trailing white-spaces.

要禁用 eol 字符,可以使用未公开的未引用选项字符串语法:

To disable the eol character you could use the undocumented unquoted option string syntax:

for /F usebackq^ skip^=2^ delims^=^ eol^= %%i in ("values.txt") do if not defined @valuethreefinal set "@valuethreefinal=%%i"

可以看到空格 = -符号是 ^ 的转义字符,以转义整个字符字符串作为一个

As you can see the SPACEs and =-signs are escaped by the caret symbol ^ in order to treat the whole option string as a unit.

尽管如此,它仍然跳过空行。为防止这种情况,请在此线程处循环:保留文本中的空行的同时保留文本文件中的空行。 / f 使用批处理时输入文本文件。

This still skips over empty lines though. To prevent this take a loop at this thread: preserve empty lines in a text file while using batch for /f.

由于您要捕获的内容不止一行可以将代码扩展为以下内容:

Since you want to capture more than a single line you could extend the code to the following:

set "@valueonefinal=" & set "@valuethreefinal=" & set "@valuethreefinal="
for /F usebackq^ delims^=^ eol^= %%i in ("values.txt") do (
    if not defined @valueonefinal (
        set "@valueonefinal=%%i"
    ) else (
        if not defined @valuetwofinal (
            set "@valuetwofinal=%%i"
        ) else (
            if not defined @valuethreefinal (
                set "@valuethreefinal=%%i"
            )
        )
    )
)

可以压缩为:

set "@valueonefinal=" & set "@valuethreefinal=" & set "@valuethreefinal="
for /F usebackq^ delims^=^ eol^= %%i in ("values.txt") do (
    if not defined @valueonefinal (
        set "@valueonefinal=%%i"
    ) else if not defined @valuetwofinal (
        set "@valuetwofinal=%%i"
    ) else if not defined @valuethreefinal (
        set "@valuethreefinal=%%i"
    )
)






一种更灵活的方法是在cmd.exe(批处理)中使用伪数组

rem // Initialise an index counter:
set /A "INDEX=0"
rem // Assign every line to an element of a pseudo-array:
for /F usebackq^ delims^=^ eol^= %%i in ("values.txt") do (
    rem // Increment the index counter:
    set /A "INDEX+=1"
    rem // Assign the current line to a pseudo-array element:
    call set "@valuefinal[%%INDEX%%]=%%i"
)

现在,文件 value.txt 的非空行)分配给名为 @valuefinal [1] @valuefinal [2] @valuefinal [3] 等。(批处理脚本中没有数组的概念,这些变量与您的变量完全相同,例如 @valueonefinal 等,这就是为什么我使用术语伪的原因。

The (non-empty) lines of the file value.txt are now assigned to variables called @valuefinal[1], @valuefinal[2], @valuefinal[3], etc. (there is no concept of arrays in batch scripting, the variables are exactly the same as yours, @valueonefinal, etc., that is why I use the term "pseudo").

呼叫命令用于此处,以便能够在同一代码块中写入和读取变量 INDEX ;仅使用 set @valuefinal [%INDEX%] = %% i 会导致赋值并因此覆盖变量 @valuefinal [0] 在每个循环迭代中。

The call command is used here in order to be able to write and read the variable INDEX within the same block of code; just using set "@valuefinal[%INDEX%]=%%i" would result in assigning and therefore overwriting the variable @valuefinal[0] in every loop iteration.

这篇关于批次:“ FOR” cmd部分起作用,“ skip =”选项不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆