Mihir
Mihir

Reputation: 557

How to add empty space using batch script?

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

Upvotes: 0

Views: 6798

Answers (4)

z_man3007
z_man3007

Reputation: 1

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

Upvotes: 0

user4317867
user4317867

Reputation: 2448

-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

Upvotes: 1

andrew
andrew

Reputation: 2469

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

Upvotes: 1

bgoldst
bgoldst

Reputation: 35314

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

Upvotes: 1

Related Questions