Dro*_*ari 3 windows batch-file
我需要创建一个写入文本文件的批处理文件(可能包括变量)。在带有bash的Unix中,这很简单:
#!/bin/bash
ver="1.2.3"
cat > file.txt <<EOL
line 1, ${ver}
line 2
line 3
...
EOL
Run Code Online (Sandbox Code Playgroud)
用批处理文件执行此操作的最简单方法是什么?
注意:对于Windows批处理,有一个完整的解决方案在Heredoc上使用'heredoc'例程吗?但是对于临时使用来说似乎太复杂了。我正在寻找一种快速简单的方法。
如果只有一个数据块(文本),则跟踪工作正常
@ECHO OFF
set string=beautyful
REM get linenumber for Data:
for /f "tokens=1 delims=:" %%a in ('findstr /n /b "REM StartData" "%~f0"') do set ln=%%a
REM print Data (use call to parse the line and evaluate variables):
(for /f "usebackq skip=%ln% delims=" %%a in ("%~f0") do call echo %%a)>"new.txt"
type "new.txt"
goto :eof
REM StartData
hello %string% world
line 2
line 3
Run Code Online (Sandbox Code Playgroud)
如果数据中没有变量,请省略call。优势:无需逃脱任何东西。即使通常是有毒字符也可以工作(代替for循环,您也可以只使用more +%ln% "%~f0")。可悲的是,当您用于call分析变量行时,您失去了这一优势。