其实你不需要把所有的东西都放到一个变量中,你只需要把重定向放在另一个位置。
试试这个:
@echo off
setlocal EnableDelayedExpansion
if exist "%FileToModify1%" (
for /F "usebackq delims=" %%a in ("%FileToModify1%") do (
echo %%a Note: certain conditions apply
)
) > "%SaveFile1%"
endlocal
请注意,for /F 会忽略原始文件中的空行,因此它们不会转移到新文件中。以; 开头的行也会被for /F 忽略(除非您更改eol 选项——请参阅for /?)。
我修改了for /F 选项:
- 不允许使用
delims,因此每行都按原样输出(使用"tokens=* delims= ",如果存在,则从每行中删除前导空格);
-
usebackq 允许在 "" 中包围文件规范,如果它包含空格,这将很有帮助;
附录 A
如果你还想把文件内容存入一个变量,你可以这样做:
@echo off
setlocal EnableDelayedExpansion
rem the two empty lines after the following command are mandatory:
set LF=^
if exist "%FileToModify1%" (
set "FileContent="
for /F "usebackq delims=" %%a in ("%FileToModify1%") do (
set "FileContent=!FileContent!%%a Note: certain conditions apply!LF!"
)
(echo !FileContent!) > "%SaveFile1%"
)
endlocal
文件内容存储在变量FileContent中,包括附录Note: certain conditions apply。 LF 包含换行符。
注意:
变量的长度非常有限(据我所知,Windows XP 以来为 8191 字节,更早为 2047 字节)!
[参考:
Store file output into variable(最后一个代码片段);
Explain how dos-batch newline variable hack works]
附录 B
或者,您可以将文件内容存储在数组中,如下所示:
@echo off
setlocal EnableDelayedExpansion
if exist "%FileToModify1%" (
set /A cnt=0
for /F "usebackq delims=" %%a in ("%FileToModify1%") do (
set /A cnt+=1
set "Line[!cnt!]=%%a Note: certain conditions apply"
)
(for /L %%i in (1,1,!cnt!) do (
echo !Line[%%i]!
)) > "%SaveFile1%"
)
endlocal
文件的每一行都存储在数组Line[1]、Line[2]、Line[3]等中,包括附录Note: certain conditions apply。 cnt 包含总行数,即数组大小。
注意:
实际上,这不是真正的数组数据类型,因为它不存在于批处理中,它是具有数组样式命名的标量变量的集合(Line[1],Line[2],...);因此可以称之为伪数组。
[参考:
Store file output into variable(第一个代码片段);
How to create an array from txt file within a batch file?]