1

I need help in to provide output that contain a blank space after each of 2 lines from .txt file containing number of lines without any blank in between.

Example :

Suppose a .txt file name as media.txt contains below input

A
B
C
D
.
.
.
Z

I want to represent above input into output like :

A
B
-----
C
D
----
E
F
----
.
.
.
.

I tried by using odd even concept but failing them to represent in above mentioned output.

Here, is my code in batch script:

set flag=1
for /f "tokens=2 delims==" %%a in ('type media.txt^|findstr "output"') do (
  if %flag%%%2==0 (
    echo %%a
  ) else (
    echo.
    echo ------------------
    echo %%a
    set flag+=1    
  )
)

Please let me know, where i am doing wrong..

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2
    check your variable names! After correcting this, you'll need [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028). – Stephan Aug 09 '20 at 21:55
  • And arithmetic operations cannot be used within `if`, you can do such only in expressions of [`set /A`](https://ss64.com/nt/set.html)… – aschipfl Aug 10 '20 at 07:48
  • 1
    Is there a reason you do not respond or mark the user's questions are correct? You have 2 answers below, if any of them solved your issue, you need to select it as the correct answer by selecting the grey tick mark next to the answer, but only if it answered your question. If not, it is good to comment to get clarification from the poster. – Gerhard Aug 21 '20 at 13:03

2 Answers2

3

This can easily be done in a batch-file or cmd with PowerShell. If you are on a supported Windows system, PowerShell will be available.

powershell -NoLogo -NoProfile -Command ^
  "Get-Content .\media.txt -ReadCount 2 | ForEach-Object { $_; Write-Output '----' }"
lit
  • 14,456
  • 10
  • 65
  • 119
1

You can't use if with a modulus and no % around a variable, and you can't check the current value of a variable in a loop without delayed expansion or some other hoops.

This should do the needful.

@(SETLOCAL ENABLEDELAYEDEXPANSION
  ECHO OFF
  SET /A "Flag=0"
  SET /A "Mod=3"
)

CALL :Main

( ENDLOCAL
EXIT /B
)

:Main
  For /f "tokens=2 delims==" %%_ in ('
    Type media.txt^|findstr "output"
  ') do (
  SET /A "flag= (flag + 1) % mod"
  IF !flag! == 0 (
    echo.
    echo.------------------
  ) 
 echo.%%_
)
Ben Personick
  • 3,074
  • 1
  • 22
  • 29