Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bat function to edit a file (add line to start of a file)

In my bat script, what do I use to open a file called open.txt and add the following line to the top

SOME TEXT TO BE ADDED

Can small edits like this be handled in a .bat script

like image 331
Berming Avatar asked Oct 01 '10 02:10

Berming


People also ask

How do you make a new line in a batch file?

How can you you insert a newline from your batch file output? You can insert an invisible ascii chr(255) on a separate line which will force a blank new line. Hold down the [alt] key and press 255 on the keypad.

What is %% in a bat file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

How do you add a line to a text file in CMD?

Windows: `Echo` Newline (Line Break) [\n] – CMD & PowerShell. In a Windows Command Prompt (CMD) and PowerShell when you execute the echo command to print some text or redirect it to a file, you can break it into multiple lines. In Linux you can do this by using the \n .

How do I append to a batch file?

Content writing to files is also done with the help of the double redirection filter >>. This filter can be used to append any output to a file. Following is a simple example of how to create a file using the redirection command to append data to files.


1 Answers

Sure, with something like:

copy original.txt temp.txt
echo.SOME TEXT TO BE ADDED>original.txt
type temp.txt >>original.txt
del temp.txt

The first line makes a temporary copy of the file. The second line overwrites the file with the line you want to add (note particularly the lack of spaces between the text being added and the > redirection operator - echo has a nasty habit of including such spaces).

The third line uses the append redirection operator >> to add the original file to the end of the new one, then the final line deletes the temporary file.

like image 50
paxdiablo Avatar answered Oct 19 '22 01:10

paxdiablo