在批处理脚本分配换行符变量 [英] Assigning newline character to a variable in a batch script

查看:390
本文介绍了在批处理脚本分配换行符变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是批处理脚本我写了

The following is the batch script i have written

@echo off
setlocal enabledelayedexpansion
set finalcontent=
For /F "tokens=1-2* delims=  " %%I in (abc.txt) do (
IF %%J EQU MAJORVER (
set currentline=%%I %%J %1
set finalcontent=!finalcontent!!currentline!
) ELSE IF %%J EQU MINORVER (
set currentline=%%I %%J %2
set finalcontent=!finalcontent!!currentline!
) ELSE IF %%J EQU BUILDNUM (
set currentline=%%I %%J %3
set finalcontent=!finalcontent!!currentline!
) ELSE (
set currentline=%%I %%J %%K%NL%
set finalcontent=!finalcontent!!currentline!
)
)
echo %finalcontent%>>xyz.txt

我想在可变currentline的每一次出现的末尾追加一个换行符。任何人都可以指导我一下吗?

I want a newline character appended at the end of every occurence of the variable currentline. Can anyone guide me on this?

推荐答案

您可以创建一个真正的换行符,并将其分配给一个变​​量。

You can create a real newline character and assign it to a variable.

setlocal EnableDelayedExpansion
set LF=^


rem TWO empty lines are required
echo This text!LF!uses two lines

延迟扩张最好的作品的换行,你也可以用百分比扩展使用它,但随后更复杂一点。

The newline best works with delayed expansion, you can also use it with the percent expansion, but then it's a bit more complex.

set LF=^


rem TWO empty lines are required
echo This text^%LF%%LF%uses two lines
echo This also^

uses two lines

它是如何工作?结果
插入符号是转义字符,它避开了下一个字符,本身被删除。结果
但是,如果下一个字符是一个换行符换行也将被删除,只有下一个字符转义用途不同(即使这也是一个换行符)。

How it works?
The caret is an escape character, it escapes the next character and itself is removed.
But if the next character is a linefeed the linefeed is also removed and only the next character is effectivly escaped (even if this is also an linefeed).

因此​​,两个空行是必需的,LF1被忽略LF2被逃脱,LF3时,只需要完成线。

Therefore, two empty lines are required, LF1 is ignored LF2 is escaped and LF3 is neccessary to finish the "line".

set myLinefeed=^<LF1>
<LF2>
<LF3>

提示:结果
它通常最好使用换行符变量定义的一个完全不同的格式,
以避免无意中删除所需的空行。

Hints:
It's often better to use a quite different format of the newline variable definition, to avoid an inadvertently deletion of the required empty lines.

(SET LF=^
%=this line is empty=%
)

我已删除经常空行之一,然后我搜索永远为什么我的程序没有工作了。

I have removed to often one of the empty lines and then I searched forever why my program didn't work anymore.

和偏执的版本检查也为空格或其他垃圾换行变量。

And the paranoid version checks also the newline variable for whitespaces or other garbage.

if "!LF!" NEQ "!LF:~0,1!" echo Error "Linefeed definition is defect, probably multiple invisble whitespaces at the line end in the definition of LF"

FOR /F "delims=" %%n in ("!LF!") do (
  echo Error "Linefeed definition is defect, probably invisble whitespaces at the line end in the definition of LF"
)

这篇关于在批处理脚本分配换行符变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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