Page 1 of 2

Some content of text file lost when printing to screen

Posted: 19 Apr 2019 13:41
by sincos2007

Code: Select all

:test1
setlocal EnableDelayedExpansion

for /f "delims=" %%i in (txt1.txt) do (
	echo %%i
)

endlocal
goto :eof
content of text file txt1.txt:

Code: Select all

111
222
set var1=!var2!
when I run the code, I found that when printing third line in txt1.txt, !var2! is lost.

How to preserve !var2! in screen?

Thanks

Re: Some content of text file lost when printing to screen

Posted: 19 Apr 2019 15:20
by Squashman
Remove the line that enables delayed expansion.

Re: Some content of text file lost when printing to screen

Posted: 19 Apr 2019 17:11
by sincos2007
Is there any other way? In my function the delayed expansion is necessary.

Re: Some content of text file lost when printing to screen

Posted: 20 Apr 2019 00:27
by sincos2007
If I echo %%i to a text file, is there any way to preserve !var2! in the text file?

Thanks

Re: Some content of text file lost when printing to screen

Posted: 20 Apr 2019 06:40
by Lachdanan
From your current code it seems like you just want to display the whole contents of txt1.txt.

If that's the case, there is no need to use echo in a loop, you can just use the "type" command.

Re: Some content of text file lost when printing to screen

Posted: 20 Apr 2019 09:09
by Squashman
You essentially need to show us a minimal, complete and verifiable example of what you are trying to do. You can't just show a code snippet and then expect us to know there is more to your batch file that is getting affected.

Re: Some content of text file lost when printing to screen

Posted: 20 Apr 2019 11:06
by sincos2007
I 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

Re: Some content of text file lost when printing to screen

Posted: 20 Apr 2019 12:03
by aGerman
Give this a go:

Code: Select all

>>"txt2.txt" findstr /vb "##" "txt1.txt"
Steffen

Re: Some content of text file lost when printing to screen

Posted: 20 Apr 2019 12:15
by Squashman
sincos2007 wrote:
20 Apr 2019 11:06
I 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
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.

Re: Some content of text file lost when printing to screen

Posted: 20 Apr 2019 23:54
by sincos2007
aGerman wrote:
20 Apr 2019 12:03
Give this a go:

Code: Select all

>>"txt2.txt" findstr /vb "##" "txt1.txt"
Steffen
Hi Steffen,

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

Re: Some content of text file lost when printing to screen

Posted: 21 Apr 2019 04:31
by aGerman
What's the maximum length of a line in your txt1.txt. Is there any risk to get close to 1021 characters?

Steffen

Re: Some content of text file lost when printing to screen

Posted: 21 Apr 2019 05:29
by sincos2007
aGerman wrote:
21 Apr 2019 04:31
What's the maximum length of a line in your txt1.txt. Is there any risk to get close to 1021 characters?

Steffen
Hi Steffen,

The text file I deal with is my source code, it is not too long in each line. Every line in my code is not get close to 1021 characters.

Thanks

Re: Some content of text file lost when printing to screen

Posted: 21 Apr 2019 15:07
by aGerman
txt1.txt

Code: Select all

abc
##xyz
def

##foo
bar


*.bat

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

console output

Code: Select all

number of lines beginning with ##: 2

content of txt2.txt after execution

Code: Select all

abc
def
bar
Steffen

Re: Some content of text file lost when printing to screen

Posted: 21 Apr 2019 16:09
by Squashman
sincos2007 wrote:
20 Apr 2019 23:54
aGerman wrote:
20 Apr 2019 12:03
Give this a go:

Code: Select all

>>"txt2.txt" findstr /vb "##" "txt1.txt"
Steffen
Hi Steffen,

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
Scope creep!

Re: Some content of text file lost when printing to screen

Posted: 22 Apr 2019 05:24
by sincos2007
aGerman wrote:
21 Apr 2019 15:07
txt1.txt

Code: Select all

abc
##xyz
def

##foo
bar


*.bat

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

console output

Code: Select all

number of lines beginning with ##: 2

content of txt2.txt after execution

Code: Select all

abc
def
bar
Steffen

Hi Steffen,

I have seen following syntax in bash shell, I have never think the batch file is so powerful.

Code: Select all

<in_file >out_file (
rem read in
set "line=" &set /p "line="
rem write out
echo !line!
)
Your code works.

Thanks