1st Line of Text Shows < >

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

1st Line of Text Shows < >

#1 Post by booga73 » 27 Dec 2013 14:38

Need help with getting 1st line from text.

For what I please need help with is getting the 1st line of text from a file that has the following values:

text file (Login.txt) contents:


    <unknown time> TT\1stName2.LastName
    12/27/2013 1:52:50 PM TT\1stName.LastName



I say about 50% of the time the file, Login.txt, shows <unknown time> instead of date and time stamp, as the 2nd line in the text file shows, of last logged in.

My code here reads the text file, Login.txt, but completely ignores the 1st line of text due to the greater than and less than symbols <,>. When the <,> symbols are present, the output of the batch script always outputs the 2nd line.

Whether the 1st line shows the standard date and time or the <,> symbols, I need to preserve what is shown on the 1st line.


I could use:

Code: Select all


set /p line1=< c:\temp1\Login.txt



but that doesn't interpret the <,> characters when those characters show up and only works when the 1st line shows a standard date and time.


If I can get assistance on this, v/r Booga73



Code: Select all


setlocal
for /f "usebackq tokens=1-10 delims=*" %%a in ("c:\temp1\Login.txt") do (

  set atrib1=%%a
  set atrib2=%%b
  set atrib3=%%c
  set atrib4=%%d
  set atrib5=%%e
  set atrib6=%%f
  set atrib7=%%g
  set atrib8=%%h
  set atrib9=%%i
  set atrib10=%%j

)

set EndUser1=%atrib1% %atrib2% %atrib3% %atrib4% %atrib5% %atrib6% %atrib7% %atrib8% %atrib9% %atrib10%
echo My User: %EndUser1%
pause



1st Example of output, this is an example of my script output when the first line of text doesn't have the <,> characters:

My User: 12/27/2013 1:52:50 PM TT\1stName.LastName
Press any key to continue . . .


2nd Example of intended output if <,> characters are found in the first line of text:

My User: <unknown time> TT\1stName2.LastName
Press any key to continue . . .

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: 1st Line of Text Shows < >

#2 Post by penpen » 27 Dec 2013 17:30

The "set /p" part works as intended, you may check it using:

Code: Select all

set /p line1=< c:\temp1\Login.txt
set line1


Your problem is that after a batch variable is expanded the code is parsed, so

Code: Select all

set atrib1=%%a
set EndUser1=%atrib1% %atrib2% %atrib3% %atrib4% %atrib5% %atrib6% %atrib7% %atrib8% %atrib9% %atrib10%
is expanded to

Code: Select all

set atrib1=<unknown time> TT\1stName2.LastName
set EndUser1=<unknown time> TT\1stName2.LastName
Within the for loop there should be no problem, as the loop variable %%a should be handled as a single token (in this case).
But when setting up EndUser1 there is no loop variable that influences the tokenizer, so the standard input (STDIN ) is redirected to the file "unknown", and the standard output (STDOUT) is redirected to the file "TT\1stName2.LastName" (the "time" part is ignored).
If no file named "unknown" is existing the set should not beeing processed, so Enduser1 stays undefined --> no output.
Another error is, that the for loop is assigning line by line to atrib1 - atrib10, so after the for loop has finished the last line is assigned to these batch variables.

You may fix your code by using delayed expansion to echo the enduser1 variable,
capsulate your batch variable settings with double quotes, and
you may add a goto and a label, to prevent the loop from looping, so this should work (although not tested):

Code: Select all

setlocal enableDelayedExpansion

for /f "usebackq tokens=1-10 delims=*" %%a in ("login.txt") do (

  set "atrib1=%%a"
  set "atrib2=%%b"
  set "atrib3=%%c"
  set "atrib4=%%d"
  set "atrib5=%%e"
  set "atrib6=%%f"
  set "atrib7=%%g"
  set "atrib8=%%h"
  set "atrib9=%%i"
  set "atrib10=%%j"
  goto:next
)
:next
set "EndUser1=!atrib1! !atrib2! !atrib3! !atrib4! !atrib5! !atrib6! !atrib7! !atrib8! !atrib9! !atrib10!"
echo My User: !EndUser1!
pause
or if you use the "set /p": less code :D .

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: 1st Line of Text Shows < >

#3 Post by foxidrive » 27 Dec 2013 19:04

See if this helps:

Code: Select all

set /p line1=< c:\temp1\Login.txt
for /f "delims=" %%a in ("%line1%") do echo %%a

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: 1st Line of Text Shows < >

#4 Post by berserker » 28 Dec 2013 00:45

foxidrive wrote:See if this helps:

Code: Select all

set /p line1=< c:\temp1\Login.txt
for /f "delims=" %%a in ("%line1%") do echo %%a

can this ignore blank first line?

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: 1st Line of Text Shows < >

#5 Post by Yury » 28 Dec 2013 08:11

berserker wrote:can this ignore blank first line?


Try:

Code: Select all

@echo off
for /f "delims=" %%a in (c:\temp1\Login.txt) do (
 set /p line1="%%a" 0>nul
 echo.
 goto:pause
)
:pause
pause>nul
Last edited by Yury on 28 Dec 2013 09:34, edited 1 time in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: 1st Line of Text Shows < >

#6 Post by penpen » 28 Dec 2013 09:29

berserker wrote:
foxidrive wrote:See if this helps:

Code: Select all

set /p line1=< c:\temp1\Login.txt
for /f "delims=" %%a in ("%line1%") do echo %%a

can this ignore blank first line?
No, but (in my opinion) logfiles should not contain any (leading) emty line.
You may create a temporary file with no empty lines:

Code: Select all

findstr /V "^$" "login.txt" > "login.tmp.txt"

If you want to avoid temporary files (for what reasons ever), and want to avoid goto's within loops then you may do it this way:

Code: Select all

@echo off
setlocal enableDelayedExpansion
for /F "tokens=1* delims=:" %%a in ('findstr /V "^$" "c:\temp1\Login.txt"^|findstr /N "^"^|findstr "^1:"') do set "line1=%%b"
echo line1=!line1!
endlocal
goto :eof

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: 1st Line of Text Shows < >

#7 Post by foxidrive » 28 Dec 2013 09:40

Yury wrote:
berserker wrote:can this ignore blank first line?


Try:

Code: Select all

@echo off
for /f "delims=" %%a in (c:\temp1\Login.txt) do (
 set /p line1="%%a" 0>nul
 echo.
 goto:pause
)
:pause
pause>nul



Here's another way:

Code: Select all

@echo off
for /f "delims=" %%a in (Login.txt) do (
   if not defined line1 (
      set line1=%%a
      set /p ="%%a" <nul
      echo.
   )
)
pause

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: 1st Line of Text Shows < >

#8 Post by berserker » 28 Dec 2013 19:35

penpen wrote:No, but (in my opinion) logfiles should not contain any (leading) emty line.

In developing apps, we cannot leave everything to speculation. "This should not be possible" is a bane. A program by right should be coded to handle every possible kind of failure scenario. Having a blank line as the first line is not impossible.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: 1st Line of Text Shows < >

#9 Post by foxidrive » 28 Dec 2013 20:31

berserker wrote:
penpen wrote:No, but (in my opinion) logfiles should not contain any (leading) emty line.

In developing apps, we cannot leave everything to speculation. "This should not be possible" is a bane. A program by right should be coded to handle every possible kind of failure scenario. Having a blank line as the first line is not impossible.


When writing batch files there are a very many cases where you assume that text is in a given format.
That is how the code works, by relying on the format of the text being processed.

In normal cases you examine a sample of the text format to write appropriate code, or assume that the OP has told you what the format is.
If there are blank lines then the OP has to tell you, so you can plan for them.

With batch code, you never plan for every contingency, as there are limitations in the scripting language to start with. Batch code is speshul, but we like it.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: 1st Line of Text Shows < >

#10 Post by berserker » 28 Dec 2013 22:14

foxidrive wrote:
With batch code, you never plan for every contingency, as there are limitations in the scripting language to start with. Batch code is speshul, but we like it.


but checking for blank lines is common especially with that set /p syntax.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: 1st Line of Text Shows < >

#11 Post by penpen » 29 Dec 2013 08:13

berserker wrote:
penpen wrote:No, but (in my opinion) logfiles should not contain any (leading) emty line.

In developing apps, we cannot leave everything to speculation. "This should not be possible" is a bane. A program by right should be coded to handle every possible kind of failure scenario. Having a blank line as the first line is not impossible.
I formulated it weak to not to annoy the OP, but i could also have said:
Per definition a logfile has no empty line, as its purpose is to save events to disk:
Which event should be defined by an empty line?

Because of that most logging tools, even don't support to write empty lines to a file (for example log4j).

The other ones, that "allows" logging empty lines are either "lazy" implemented (for speed purposes) and leave the task to the programmer to secure that no empty line is logged, or they should be allowed to be used as datalogger.

You may say if i log the output of a program, then there are empty lines.
But the result is no logfile, it is a datalogfile the result of a datalogger;
you may see the content of the file as one big entry of a logfile with no timestamp.

As the OP saves (timestamp, user) to a file called "Login.txt" it is clearly a std logfile --> there should be no empty lines.

penpen

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: 1st Line of Text Shows < >

#12 Post by booga73 » 29 Dec 2013 11:45

Thank you DosTips Community, PenPen, Berserker, and FoxiDrive, all your input was appreciated.

I like PenPen's input; I went with that. That permitted me to keep the format of the output I needed.

output that I needed was:

<unknown time> TT\1stName2.LastName

The code also did good in keeping my initial format too with:

12/27/2013 1:52:50 PM TT\1stName.LastName

for example, when I didn't receive <unknown time>, I received the standard 12/27/2013 1:52:50 PM TT\1stName.LastName.

I found this neat info from: http://stackoverflow.com/questions/2453 ... atch-files, item 79, Variable substrings

After finding the initial user login date/time & userName, I was able to format my code to just get <domain>\UserName from using the variable string concatenation.

set MyName=!EndUser1!
%MyName:~-25,20%

End result was formatted to: <domain>\UserName

thanks! v/r Booga73

Post Reply