Link to home
Start Free TrialLog in
Avatar of NickMalloy
NickMalloyFlag for United States of America

asked on

Adding characters to the beginning of each line

How can I add some text to the beginning of each line in a txt file within notepad?
Avatar of Michael Dyer
Michael Dyer
Flag of United States of America image

Here is a batch file that will add a text string to the beginning of each line in a file.  This example reads file.txt and creates new_file.txt

REM Add text to the beginning of each record
REM Change $text to the correct string you want appended to each record

Echo off
cls
ECHO Processing Records - please wait
ECHO.

REM create a environmental variable with the text in it
  set $text=TEXT TO BE APPENDED

REM process FILE.TXT

      IF exist new_file.txt del new_file.txt

      setlocal EnableDelayedExpansion
       set x=%$text%
      for /f "delims=" %%i in (file.txt) do (
        echo !x!%%i >> new_file.txt
      )

REM output file is new_file.txt
Avatar of NickMalloy

ASKER

awesome. How would I extend this to add some text to the end of each line?
you just have to edit the echo line - this is where the text is combined with the record.  %%i represents the original record.  !x! is the new text.  

for example, to add the text at the end of the line you would change this to:

echo %%i!x! >> new_file.txt
ASKER CERTIFIED SOLUTION
Avatar of Michael Dyer
Michael Dyer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial