loop through file saving to variable

戏子无情 提交于 2019-12-13 03:38:19

问题


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...


回答1:


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?]




回答2:


you can write the output file in one shot:

(
  for /l %%i in (0,1,10) do (
   echo line %%i
  )
)>outfile.txt

(much quicker than appending each line separately)



来源:https://stackoverflow.com/questions/32999205/loop-through-file-saving-to-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!