0

Is there any way to loop a text file which will add a EMPTY SPACE in front of each line?

commit.txt

commit c9bee
Merge: 7db
Author: TOM
Date:   Fri Mar 13

Author: TIM

output.txt

 commit c9bee
 Merge: 7db
 Author: TOM
 Date:   Fri Mar 13

 Author: TIM
Mihir
  • 557
  • 1
  • 6
  • 28

4 Answers4

1

-Editing here to mention escaping the space is not required, you can use ECHO 1 to get a (space)1.

Try escaping the space using ^

For example:

C:\Scripts>echo ^ string

string

C:\Scripts>echo string

string

-edit, this is what happens when you rush and don't read the question fully!

Try this:

C:\Scripts>for /f %a in (input.txt) do echo ^ %a

C:\Scripts>echo  test1
 test1
C:\Scripts>echo  test2
 test2
Community
  • 1
  • 1
user4317867
  • 2,397
  • 4
  • 31
  • 57
  • Thanks for the quick reply, but i think syntax should be --> for /f "delims=" %a in (commit.txt) do echo ^ %a. For empty line i'm having same issue "ECHO is on." seen. – Mihir Mar 17 '15 at 13:12
  • for /f "delims=" %i in (file) do @echo. %i will fix empty line issue. – Mihir Mar 17 '15 at 13:35
  • Try this [post](http://stackoverflow.com/a/24289027/4317867) which uses `FindStr /v` to skip blank lines. – user4317867 Mar 17 '15 at 21:19
1

Here's one solution:

for /f "delims=" %i in (file) do @echo  %i

Note that you must double the percents if you use this in a script (as opposed to interactive usage).

bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • thanks for quick reply, your one liner works well. But, if there are any empty line in between, it says "ECHO is on." – Mihir Mar 17 '15 at 13:10
  • for /f "delims=" %i in (file) do @echo. %i will fix empty line issue. – Mihir Mar 17 '15 at 13:34
1

try this. This "overwrites" the original file.If you want to keep the name as output.txt just comment out the call to Overwriteoriginal

@echo off

REM makes sure the user passed in a file name
if [%1]==[] (
    echo Must pass in a file name
) else (
    Call:AddSpaceToEveryLine %1

    Call:OverwriteOriginal %1

    echo processed %1
)

GoTo:EOF

:OverwriteOriginal 
    REM overwrites original file with spaced file
    del %1

    rename output.txt %1
GoTo:EOF

:AddSpaceToEveryLine
    REM there is actually one space after the equals sign
    set space= 
    for /f "tokens=*" %%a in (%1) do (
      echo %space%%%a >> output.txt
    )
GoTo:EOF
andrew
  • 2,451
  • 1
  • 15
  • 22
  • thanks for quick reply. But, if there are any empty line in between, it says "ECHO is off." – Mihir Mar 17 '15 at 13:19
0

It can be done very easily and you don't need a for loop for it. Just declare a variable and and at the end of of your code just to do echo with the declared variable few times to your output file. In this case, I am sending to a file called c:\sample.txt. Here is an example:

@echo off

set blank=

:: Your main code goes here

echo. %blank% >> c:\sample.txt
echo. %blank% >> c:\sample.txt
echo. %blank% >> c:\sample.txt

echo. Author: TIM >> c:\sample.txt
xiawi
  • 1,772
  • 4
  • 19
  • 21