i am extrely novice at batch files but have been searching a way to add a line of text to a text file
the test file is called test.txt and all i need is the batch file to add a line of text at the top of the file lets say "welcome" without over righting the file
how would i achieve this?
at the moment i have a install.bat that is blank and no text in the test.txt file
You can write "Welcome" in a new file, append the content of "test.txt" to that new file, and then rename the new file to "test.txt".
#echo off
echo Welcome > new.txt
echo. >> new.txt
type test.txt >> new.txt
copy /y new.txt test.txt
del new.txt
I'm a Linux user, but in the past I had to deal with the limitations of Windows' command-line interpreter. I still do, occasionally. Anyway, here's my contribution:
#echo off
rem AddToTop - Add line to top of file
if [%2] == [] goto help
set file=%1
set line=%2 %3 %4 %5 %6 %7 %8 %9
if exist %file% (
echo %line%>%file%.tmp
type %file%>>%file%.tmp
del %file%
rename %file%.tmp %file%
) else (
echo File "%file%" not found.
)
goto end
:help
echo Syntax: addtotop.bat file line
:end
Hope it helps. That's all I can do with this crappy CMD.EXE... ;-)
Related
I want to write, a script output into logfile at the end with new line..
with this command, it replace the file.
echo "hai" > /tmp/syslog.txt
but I need add "hai" with existing file content at the end with new line..
thanks in advance..
just simple like this:
echo "hai" >> /tmp/syslog.txt
:)
Using option \n – New line with backspace interpreter -e treats new line from where it is used.
echo -e "\nHai" >> /tmp/syslog.txt
I need a batch file that will add the text "Write In" in a new line at the beginning of the content of hundreds of .txt files without removing any existing text. I found something on here that did not work for me. Anyone have suggestions?
This is the code I was working with:
for /r %%a in (*.txt) do (
echo ---- %%a before ----
type "%%a"
echo --------------------
echo Write In > "%%a.tmp"
type "%%a" >> "%%a.tmp"
del "%%a"
move "%%a.tmp" "%%a"
echo ---- %%a after ----
type "%%a"
echo --------------------
)
pause
It did nothing
I would most probably do it like this:
rem // Create temporary header file:
> "head.txt" echo Write In
rem // Iterate all text files in current directory:
for %%F in ("*.txt") do (
rem /* Combine header and currently iterated text file into a temporary file;
rem there cannot arise any file name conflicts (like temporary files becoming
rem iterated also unintendedly, or temporary files overwriting files to handle),
rem because the extension of the temporary files differ from the text files: */
copy /B "head.txt"+"%%~F" "%%~F.tmp"
rem // Overwrite original text file by temporary file, erase the latter:
move /Y "%%~F.tmp" "%%~F"
)
rem // Erase the temporary header file:
del "head.txt"
I found a way that is not very clean and will take a while for bigger files, but it works out for me:
#echo off
for /r "%~dp0" %%f in (*.txt) do (
echo Processing file %%f
>"%%~f.2" (
echo "Text to append here"
type "%%~f"
)
del "%%~f"
ren "%%~f.2" "%%~nxf"
)
pause
Loops recursively through all .txt -files in the batch-files directory.
Creates a new file with the name oldName.txt.2 and files it with the text to add and the rest of the oldfiles content.
Deletes the old file and renames the new file to the name of the old one.
The addition of .2 to the file ending is needed to make sure it does not get processed again by the loop.
Of course you can add multiple lines by multiplying the echo lines.
You can as well add text to the end of the file like this with adding the echo lines after the type line.
You can simply use the Linux Sed command to insert a header into a file.
sed -i '1s/^/This is my header\n/' filename
e.g. sed -i '1s/^/Write In\n/' myfile.txt
This can be applied to multiple files under a directory:
For .txt files
for file in *.txt
do
sed -i '1s/^/This is my header\n/' $file
done
For CSV files
for file in *.csv
do
sed -i '1s/^/This is my header\n/' $file
done
i have to write the file right in the terminal, not in vim. Something like this vim -someflag "text for my file" filename. Any ideas ?
In bash, just do:
$ echo "text for my file" >filename
To append, use:
$ echo "text for my file" >> filename
Why use vim if you want to write like this?
Use echo instead:
echo "text for my file" > filename
man echo for more information - documentation
'cat' or 'echo' would be the best options for this.
In the absence of an input argument, cat reads from the standard input:
cat > myfile.txt
[enter your text, then hit ctrl-d]
Alternatively, echo will write its arguments to a file with redirection:
echo "text for my file" > myfile.txt
I have 300 files in a folder. I have to append one new line at the end of all files in a folder.
How can i achieve it using grep.
I tried the following command but its not working
sed 's/$/\n/' /Path/filename.txt
Just say echo "" >> file. This will append a new line at the end of file.
To do it in all the files in the folder:
for file in *
do
echo "" >> "$file"
done
From the comments, in your case you have to say:
for file in /path/*.txt
do
echo "" >> "$file"
done
i want to write a batch file to find a particular string in a file. If the string is found then i want to redirect the whole line which contains the string to another file. for ex:
suppose a file myfile.txt contains the following text
abcwerthfdh
qwerewtretywr
weqreqwrtabcwerwe
wqerweqabcqwewq
when i launch the batch file giving myfile.txt and abc as command line arguments, then the output should be into a file called newfile.txt and it should contain only the lines which contain the text "abc". if i run this code again it should append to newfile.txt and not delete existing contents. in this case it should push the lines 1, 3 and 4 into newfile.txt
#echo off&setlocal
for /f "delims=" %%a in ('findstr "%~2" "%~1"') do (echo(%%a)>>newfile.txt
You could use FINDSTR:
FINDSTR abc myfile.txt >> newfile.txt
type myfile.txt | findstr /n "abc" >> newfile.txt
???