Jump to content

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


Recommended Posts

Guest Miroslav Pragl
Posted

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

  • Replies 7
  • Created
  • Last Reply

Popular Days

Guest Pegasus \(MVP\)
Posted

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

 

 

"Miroslav Pragl" <miroslav__pragl@hotmail.com> wrote in message

news:103DD1B8-74A9-4108-AF09-52CDE5E9940B@microsoft.com...

> 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.

Guest Miroslav Pragl
Posted

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

 

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

 

"Pegasus (MVP)" <I.can@fly.com.oz> wrote in message

news:u94N2ieeIHA.532@TK2MSFTNGP03.phx.gbl...

> 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.

Guest Pegasus \(MVP\)
Posted

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

 

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

 

=======================

 

"Miroslav Pragl" <miroslav__pragl@hotmail.com> wrote in message

news:E357758F-BDCF-4601-9500-F30678BB0156@microsoft.com...

> 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

>

> "Pegasus (MVP)" <I.can@fly.com.oz> wrote in message

> news:u94N2ieeIHA.532@TK2MSFTNGP03.phx.gbl...

>> 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.

>

>

Guest Miroslav Pragl
Posted

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

 

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

 

"Pegasus (MVP)" <I.can@fly.com.oz> wrote in message

news:eJUlWOfeIHA.6092@TK2MSFTNGP06.phx.gbl...

> 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)

Guest Pegasus \(MVP\)
Posted

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

 

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.

 

 

"Miroslav Pragl" <miroslav__pragl@hotmail.com> wrote in message

news:C3BAEDC0-C009-4F89-AEEF-47366CDDF409@microsoft.com...

> 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

>

> "Pegasus (MVP)" <I.can@fly.com.oz> wrote in message

> news:eJUlWOfeIHA.6092@TK2MSFTNGP06.phx.gbl...

>> 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)

>

>

Guest Miroslav Pragl
Posted

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

 

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

Guest Pegasus \(MVP\)
Posted

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

 

 

"Miroslav Pragl" <miroslav__pragl@hotmail.com> wrote in message

news:0619BCB1-4D17-429D-B679-14FF1C885F88@microsoft.com...

> 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.


×
×
  • Create New...