Code: Select all
:test1
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (txt1.txt) do (
echo %%i
)
endlocal
goto :eof
Code: Select all
111
222
set var1=!var2!
How to preserve !var2! in screen?
Thanks
Moderator: DosItHelp
Code: Select all
:test1
setlocal EnableDelayedExpansion
for /f "delims=" %%i in (txt1.txt) do (
echo %%i
)
endlocal
goto :eof
Code: Select all
111
222
set var1=!var2!
Code: Select all
>>"txt2.txt" findstr /vb "##" "txt1.txt"
This thread could have been 2 posts long. Remember that everyone here is volunteering their time to help other people. When you don't provide the needed information to solve your task you are wasting everyone's time.sincos2007 wrote: ↑20 Apr 2019 11:06I am trying to read each line from a text file, and if the line does not begin with ##, append this line to another text file.
thanks
Hi Steffen,aGerman wrote: ↑20 Apr 2019 12:03Give this a go:SteffenCode: Select all
>>"txt2.txt" findstr /vb "##" "txt1.txt"
Hi Steffen,
Code: Select all
abc
##xyz
def
##foo
bar
Code: Select all
@echo off &setlocal
set "in_file=txt1.txt"
set "out_file=txt2.txt"
setlocal EnableDelayedExpansion
set "n=0"
<"!in_file!" >"!out_file!" (
for /f %%i in ('type "!in_file!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
set "line=" &set /p "line="
if not defined line (
REM line was empty
) else if "!line:~0,2!"=="##" (
REM ## found at the beginning of the line
set /a "n+=1"
) else (
REM do whatever you want, e.g. redirect to the other file
echo(!line!
)
)
)
echo number of lines beginning with ##: %n%
pause
Code: Select all
number of lines beginning with ##: 2
Code: Select all
abc
def
bar
Scope creep!sincos2007 wrote: ↑20 Apr 2019 23:54Hi Steffen,aGerman wrote: ↑20 Apr 2019 12:03Give this a go:SteffenCode: Select all
>>"txt2.txt" findstr /vb "##" "txt1.txt"
With your code, how can I get count of lines starting with ##
And I want to do some operations on the line not starting with ##, what should I do?
Thanks
aGerman wrote: ↑21 Apr 2019 15:07txt1.txtCode: Select all
abc ##xyz def ##foo bar
*.batCode: Select all
@echo off &setlocal set "in_file=txt1.txt" set "out_file=txt2.txt" setlocal EnableDelayedExpansion set "n=0" <"!in_file!" >"!out_file!" ( for /f %%i in ('type "!in_file!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do ( set "line=" &set /p "line=" if not defined line ( REM line was empty ) else if "!line:~0,2!"=="##" ( REM ## found at the beginning of the line set /a "n+=1" ) else ( REM do whatever you want, e.g. redirect to the other file echo(!line! ) ) ) echo number of lines beginning with ##: %n% pause
console outputCode: Select all
number of lines beginning with ##: 2
content of txt2.txt after executionSteffenCode: Select all
abc def bar
Code: Select all
<in_file >out_file (
rem read in
set "line=" &set /p "line="
rem write out
echo !line!
)