including empty lines in FOR /F " %%i in (file.txt)

M

Miroslav Pragl

Hello,
is there a way how to process EMPTY LINES in simple for script such as

for /f "tokens=*" %i in (soubor.txt) do @echo %i

? These are simply ignored and I couldn't find any obvious way how to
include them (no "for" parameter, no if exist %I ...)

Thanks for pointing me the right direction

MP
 
P

Pegasus \(MVP\)

Miroslav Pragl said:
Hello,
is there a way how to process EMPTY LINES in simple for script such as

for /f "tokens=*" %i in (soubor.txt) do @echo %i

? These are simply ignored and I couldn't find any obvious way how to
include them (no "for" parameter, no if exist %I ...)

Thanks for pointing me the right direction

MP

I'm not aware of one but if you want an authoritative answer then
you should post your question in the "winnt.batch" newsgroup.
Alternatively, if you post the context of your query then it should
be fairly straightforward to give you a scripting solution.
 
M

Miroslav Pragl

Thanks for sane reply but I can't find such a NG here in MS communities
(msnews.microsoft.com) - which NNTP server is it running at?.

Regarding my query: I just want to list content of a file from bottom to top
(last line goes first and so on) for training purposes. "For" is fine to do
that but I just keep losing empty lines

fraction of the code doing the core (lc = line count, fname=file to be
reversed):


:loop
set /a lc -= 1
if %lc% leq 0 goto endloop
FOR /F "skip=%lc% tokens=*" %%i in (%fname%) do (
echo %%I <-----------empty lines ignored here
goto loop
)
:endloop


MP
 
P

Pegasus \(MVP\)

winnt.batch is not a Microsoft newsgroup. You have do access
it via the news server provided by your ISP.

You can solve your problem with this script file:
====================
FileName = "D:\Temp\temp.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set file = objFSO.GetFile(FileName)
Set InputStream = file.OpenAsTextStream(1)

InputStream.ReadAll
count=InputStream.Line - 1
InputStream.Close

ReDim Lines(count)
Set InputStream = file.OpenAsTextStream(1)
For i = 0 To count - 1
Lines(i) = InputStream.ReadLine
Next
InputStream.Close

For i = count - 1 To 0 Step -1
WScript.Echo Right(" " & i, 5) & " " & Lines(i)
Next
=======================

Alternatively, if you want it wrapped in a batch file, try this:

=======================
@echo off
set FileName=d:\Temp\Temp.txt
rem Unwrap the lines below so that they
rem all start with the word "echo"!
echo > c:\TempVBS.vbs FileName = "%FileName%"
echo >> c:\TempVBS.vbs Set objFSO =
CreateObject("Scripting.FileSystemObject")
echo >> c:\TempVBS.vbs Set file = objFSO.GetFile(FileName)
echo >> c:\TempVBS.vbs Set InputStream = file.OpenAsTextStream(1)
echo.>> c:\TempVBS.vbs
echo >> c:\TempVBS.vbs InputStream.ReadAll
echo >> c:\TempVBS.vbs count=InputStream.Line - 1
echo >> c:\TempVBS.vbs InputStream.Close
echo.>> c:\TempVBS.vbs
echo >> c:\TempVBS.vbs ReDim Lines(count)
echo >> c:\TempVBS.vbs Set InputStream = file.OpenAsTextStream(1)
echo >> c:\TempVBS.vbs For i = 0 To count - 1
echo >> c:\TempVBS.vbs Lines(i) = InputStream.ReadLine
echo >> c:\TempVBS.vbs Next
echo >> c:\TempVBS.vbs InputStream.Close
echo.>> c:\TempVBS.vbs
echo >> c:\TempVBS.vbs For i = count - 1 To 0 Step -1
echo >> c:\TempVBS.vbs WScript.Echo Right(" " ^& i, 5) ^& " " ^&
Lines(i)
echo >> c:\TempVBS.vbs Next
rem The lines below this point do NOT start
rem with the word "echo"!
cscript //nologo c:\TempVBS.vbs
del c:\TempVBS.vbs

=======================
 
M

Miroslav Pragl

IC.
Anyway i do want to use windows cmd.exe shell and "for" command - it's for
educational purposes so chickening out to vbs is not the real man solution
:)

Thanks

MP
 
P

Pegasus \(MVP\)

Instead of calling it "chickening out", you might use it as
an incentive to find out about VB Scripting. Batch file
programming has its use and is my preferred tool for
most maintenance functions, but certain things either
cannot be done in batch or else only in a very convoluted
way.
 
M

Miroslav Pragl

As I told you this batch is for educational purposes and I HAVE TO use
cmd/for.

anyway following trick:
for /f "tokens=1* delims=]" %i in ('find /n /v ""^<soubor.txt') do
@echo/%j
did the job. Thanks to foxidrive!

MP
 
P

Pegasus \(MVP\)

Miroslav Pragl said:
As I told you this batch is for educational purposes and I HAVE TO use
cmd/for.

anyway following trick:
for /f "tokens=1* delims=]" %i in ('find /n /v ""^<soubor.txt') do
@echo/%j
did the job. Thanks to foxidrive!

MP

I'm not surprised that the batch file group found a solution.
They are an extremely resourceful lot! Unfortunately it
involves several involved tricks that are both undocumented
and difficult to understand.

Good luck with your endeavours to learn advanced batch
stuff! You'll probably find a lot to learn in the batch group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top