1

I want to read last two lines of log file, below code is printing all the lines

@echo on 
setlocal EnableDelayedExpansion 
for /f "delims=" %%x in (C:\FICO\test_script\retcodeStartClientKYC.txt) do ( 
 set "previous=!last!" 
 set "last=%%x" 
)
echo !previous! 
set var1=%previous% 
set var2=%var1:*:=% 
echo %var2% >> result1.txt

Can anyone please tell me whats wrong in above code.

Magoo
  • 77,302
  • 8
  • 62
  • 84
Yogesh A
  • 11
  • 1
  • 1
    The code you have posted should output everything after the first colon, `:`, of the second last line from `retcodeStartClientKYC.txt`, to `results.txt`. The content, encoding, and line endings your text file however could cause you issues. The text file should be UTF-8, or ANSI encoded, not UTF-16 LE, and should use Windows CRLF or Unix LF line endings, not MAC CR. However, without the fie content, I cannot advise you whether including more robust code, like doublequotes on lines `3`, `8`, and `9`, and delayed expansion with parentheses on line `10` would help in this specific instance. – Compo Jan 17 '21 at 17:59

1 Answers1

-2

I should advise that if your batch reads exactly as you posted, I have no doubt you'd have trouble.

Here's my reformatted version:

@echo off
setlocal EnableDelayedExpansion 
rem for /f "delims=" %%x in (C:\FICO\test_script\retcodeStartClientKYC.txt) do ( 
for /f "delims=" %%x in (q65761009.txt) do ( 
 set "previous=!last!" 
 set "last=%%x" 
)
echo !previous! 
set "var1=%last%"
set "var2=%var1:*:=%"
echo %var2% 
GOTO :EOF

Noting: You didn't show any sample (if censored) source data from the log file. I used a sample of random text put into a file called q65761009.txt which suits my system.

Now - to use the code, copy/paste and edit as required into a .bat file using a proper text editor like Editplus or Notepad++ (amongst others) - at a pinch, use Notepad but be aware that batch is oddly layout-sensitive and Notepad has a horrible habit of trying to reformat when it saves. Save in ANSI format, not Unicode.

Your manipulation is unexplained, unfortunately. As you've coded it, it seems to be echoing the second-last line and then removing all text before the first colon in that second-last line, and echoing the remainder.

As you've not shown your expectations of the result, we can't be sure of quite what you intended; but that last seems suspect. The crystal ball tells me that you intended to echo the second-last line (previous) and the part of the last line after the first colon, so I've modified the code to do that.

Tip : Use set "var1=data" for setting values - this avoids problems caused by trailing spaces.

Since you are apparently extending a file, you could use >>filename at the end of each appropriate echo. Or perhaps you want to create a file anew - containing only the data produced by the two echo statements; you don't say...

But - I found that the code above generated the two lines (to the console) as (I) expected, and no sign of any of the previous data in the file.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Ok, I admit I don't understand your answer. The first few paragraphs say nothing about the problem. Can you please clarify what is the solution? The code in your solution looks exactly the same as the one in the question. – Dharman Jan 17 '21 at 14:04
  • Sadly, @Dharman, you don't seem to understand the issue. OP's code actually works - it's likely to be a formatting issue (hence advice about editors and cutting/pasting) and there are changes - enclosing `set` commands in quotes and selecting the appropriate variable for the very last output (`last` in place of `previous`) Perhaps you should see the original posting, which I reformatted to the form that now appears. It's obvious that if that's a proper representation of the actual code used, it will give trouble - as I advised. The code is not "printing all the lines" which was OP's problem. – Magoo Jan 17 '21 at 14:29