Some content of text file lost when printing to screen

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

Some content of text file lost when printing to screen

#1 Post by sincos2007 » 19 Apr 2019 13:41

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

Squashman
Expert
Posts: 4106
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 19 Apr 2019 15:20

Remove the line that enables delayed expansion.

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#3 Post by sincos2007 » 19 Apr 2019 17:11

Is there any other way? In my function the delayed expansion is necessary.

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#4 Post by sincos2007 » 20 Apr 2019 00:27

If I echo %%i to a text file, is there any way to preserve !var2! in the text file?

Thanks

Lachdanan
Posts: 2
Joined: 14 Jun 2017 15:36

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

#5 Post by Lachdanan » 20 Apr 2019 06:40

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.

Squashman
Expert
Posts: 4106
Joined: 23 Dec 2011 13:59

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

#6 Post by Squashman » 20 Apr 2019 09:09

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.

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#7 Post by sincos2007 » 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

aGerman
Expert
Posts: 3639
Joined: 22 Jan 2010 18:01
Location: Germany

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

#8 Post by aGerman » 20 Apr 2019 12:03

Give this a go:

Code: Select all

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

Squashman
Expert
Posts: 4106
Joined: 23 Dec 2011 13:59

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

#9 Post by Squashman » 20 Apr 2019 12:15

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.

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#10 Post by sincos2007 » 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

aGerman
Expert
Posts: 3639
Joined: 22 Jan 2010 18:01
Location: Germany

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

#11 Post by aGerman » 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

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#12 Post by sincos2007 » 21 Apr 2019 05:29

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

aGerman
Expert
Posts: 3639
Joined: 22 Jan 2010 18:01
Location: Germany

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

#13 Post by aGerman » 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

Squashman
Expert
Posts: 4106
Joined: 23 Dec 2011 13:59

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

#14 Post by Squashman » 21 Apr 2019 16:09

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!

sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

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

#15 Post by sincos2007 » 22 Apr 2019 05:24

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

Post Reply