• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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?
0
NickMalloy
Asked:
NickMalloy
  • 3
1 Solution
 
Michael DyerSenior Systems Support AnalystCommented:
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
0
 
NickMalloySystems LeadAuthor Commented:
awesome. How would I extend this to add some text to the end of each line?
0
 
Michael DyerSenior Systems Support AnalystCommented:
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
0
 
Michael DyerSenior Systems Support AnalystCommented:
This example adds text at the beginning and the end of the record:

REM add text to the beginning and end of each record
Echo off
cls
ECHO Processing Records - please wait
ECHO.

REM create a environmental variable with the text in it

set $fronttext=TEXT TO BE APPENDED at the beginning
set $backtext=TEXT TO BE APPENDED at the end

REM process FILE.TXT

      IF exist new_file.txt del new_file.txt

      setlocal EnableDelayedExpansion
       set x=%$fronttext%
                   set y=%$backtext%

      for /f "delims=" %%i in (file.txt) do (
        echo !x!%%i!y! >> new_file.txt
      )

REM output file is new_file.txt
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now