循环通过文件保存到变量 [英] loop through file saving to variable

查看:155
本文介绍了循环通过文件保存到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有以下的bat文件工作(允许一个文件添加到文件的每一行的末尾) - 请参见:
bat文件:将if,for和变量一起使用

  @echo off 
setLocal EnableDelayedExpansion

IF EXIST%FileToModify1%(
for / ftokens = * delims =%% a in(%FileToModify1%)do(
echo %% a注意:某些条件适用>>%SaveFile1%


但是,我想保存每一行到一个变量(包括新的行符号)),然后将变量回传到文件末尾。因为文件中有几行文件,每行保存到一个文件的效率真的很低。



我试过谷歌搜索,但答案不符合我的情况。基本上我需要语法连接和保存到一个变量(累积类似+ =在C#),并且还使用新行...


解决方案

实际上,你不需要把一切都放在一个变量,你只需要将重定向放在另一个位置。
试试这个:

  @echo off 
setlocal EnableDelayedExpansion

%FileToModify1%(
for / Fusebackq delims =%% a in(%FileToModify1%)do(
echo %% a注:某些条件适用

)> %SaveFile1%

endlocal

请注意,文件被忽略/ F ,因此它们不会传输到新文件。此外,对于/ F 忽略以; 开头的行(除非更改 eol 选项 - 请参阅 for /?)。



我修改了 for / F 选项:




  • no delims ,因此每行都是按原样输出的(tokens = * delims =,如果存在,每个行都会删除前导空格);

  • usebackq 允许在中包含文件规范, ;






附录A



如果您仍然想将文件内容存储到变量中,可以这样做:

  @echo off 
setlocal EnableDelayedExpansion

rem强制执行以下命令后的两个空行:
set LF = ^


如果存在%FileToModify1% (
setFileContent =
for / Fusebackq delims =%% a in(%FileToModify1%)do(
setFileContent =!FileContent!%% a Note :某些条件适用!LF!

(echo!FileContent!)> %SaveFile1%


endlocal

内容存储在变量 FileContent 中,包括附录注意:某些条件适用 LF 持有新行符号。



注意: $ b变量的长度是非常有限的(据我所知,从Windows XP和2047字节早8191字节)!



[参考文献: br>
将文件输出存储到变量(上一个代码段);

说明dos-batch newline变量hack的工作原理]






附录B



或者,您可以将文件内容存储在数组中,如下所示:

  @echo off 
setlocal EnableDelayedExpansion

如果存在%FileToModify1%(
set / A cnt = 0
for / Fusebackq delims =%% a in(%FileToModify1%)do(
set / A cnt + = 1
setLine [!cnt!] = %% a注意:某些条件适用于


(对于/ L %% i in(1,1,!cnt!)do(
echo!Line [ %%一世]!
))> %SaveFile1%


endlocal

的文件存储在数组 Line [1] Line [2] 行[3] 等,包括附录注意:某些条件适用 cnt 包含总数,这是数组大小。



注意:

实际上,这不是一个真正的数组数据类型,因为它在批处理中不存在,它是一个具有数组风格命名的标量变量的集合( Line [1] Line [2] ,...);因此可以将其称为伪数组。



[参考文献:

将文件输出存储到变量(第一个代码段);

如何在批处理文件中从txt文件创建数组?]


I now have the following bat file working (which allows one to add text to the end of each line of a file) -- please see also: bat file: Use of if, for and a variable all together

@echo off
setLocal EnableDelayedExpansion

IF EXIST "%FileToModify1%"  (
  for /f "tokens=* delims= " %%a in (%FileToModify1%) do (
    echo %%a   Note:  certain conditions apply  >> "%SaveFile1%"
  ) 
)

However, I would like to save each line to a variable (including the new line symbol(s)) and then echo the variable to a file at the end. Since there are several lines in the file it is really inefficient to save to a file with each line.

I tried googling this, but the answers do not fit my situation...

essentially I need the syntax for concatenating and saving to a variable (cumulatively like "+=" in C#), and also using the new lines...

解决方案

Actually you do not need to put everything into a variable, you just need to place the redirection at another position. Try this:

@echo off
setlocal EnableDelayedExpansion

if exist "%FileToModify1%" (
    for /F "usebackq delims=" %%a in ("%FileToModify1%") do (
        echo %%a   Note:  certain conditions apply
    )
) > "%SaveFile1%"

endlocal

Note that empty lines in the original file are ignored by for /F, so they are not transferred to the new file. Also lines starting with ; are ignored by for /F (unless you change the eol option -- see for /?).

I modified the for /F options:

  • no delims are allowed, so the each line is output as is (with "tokens=* delims= ", leading spaces are removed from each line if present);
  • usebackq allows to surround the file specification in "" which is helpful if it contains spaces;

Appendix A

If you still want to store the file content into a variable, you can do this:

@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

The file content is stored in variable FileContent, including the appendix Note: certain conditions apply. LF holds the new-line symbol.

Note:
The length of a variable is very limited (as far as I know, 8191 bytes since Windows XP and 2047 bytes earlier)!

[References:
Store file output into variable (last code fragment);
Explain how dos-batch newline variable hack works]


Appendix B

Alternatively, you could store the file content in a array, like this:

@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

Each line of the file is stored in an array Line[1], Line[2], Line[3], etc., including the appendix Note: certain conditions apply. cnt contains the total number of lines, which is the array size.

Note:
Actually this is not a true array data type as such does not exist in batch, it is a collection of scalar variables with an array-style naming (Line[1], Line[2],...); therefore one might call it pseudo-array.

[References:
Store file output into variable (first code fragment);
How to create an array from txt file within a batch file?]

这篇关于循环通过文件保存到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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