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
Related
i have files and This names the files example :
log(2).txt
log(3).txt
log(4).txt
log.txt
I want Rename all of them to file names with numbers like:
1.txt
2.txt
3.txt
4.txt
system windows or linux
Just save this code into a name.bat file in your folder and run the command
#echo off
setlocal EnableDelayedExpansion
set i=0
for %%a in (*.txt) do (
set /a i+=1
ren "%%a" "!i!.new"
)
ren *.new *.txt
Here in order to avoid conflicts first the files are named as '.new' extenstion and later it is converted back to '.txt' files.
Working file: Projects/fentbase/common/javasrc/validators/PasswordSetupVal.java
Working file: Projects/fentbase/channeladministration/spec/ui/dev/ppdl/AccessSchemeMaintenancePreview.dppdl
Working file: Projects/fentbase/common/javasrc/validators/EmailValidator.java
Working file: Projects/fentbase/common/javasrc/validators/MailIdVal.java
I have this in a file. I need to take out "Working file: " from every line. Please let me know how do I do that.
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%1) do (
set line=%%a
echo !line:Working file=!
)
Usage:
sciprt.cmd file.txt > new_file.txt
move /y new_file.txt file.txt
another way to skin the cat:
for /f "tokens=2,*" %%i in ('type "file.txt"') do #echo(%%j
Here is one way:
type "file.txt" | repl "Working file: " "" L >"newfile.txt"
This uses a helper batch file called repl.bat (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
I have a txt file that contains about 500 values, one per line. I need to check to see of any of those 500 values appear in any of 6 csv files each containing 100k lines. I can search for one value in those 6 csv files using
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
but how do I do multiple searches automatically via command-line or batch file (CaSe SenSiTIve)?
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=C:\destdir"
for /f "delims=" %%a in ('dir /s /b "%sourcedir%\*.csv"') do (
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt"
GOTO :EOF
What you are asking is rather unclear. I used c:\sourcedir as the location of the .csv files and c:\destdir as the location for the reports. Replacing
FINDSTR /N /g:"yourtextfilecontaining500linestomatch.txt" "%%~fa") > "%destdir%\%%~nafound.txt with your original (with the double > would accumulate the lines into a single file - if that's what you want. As it stands, a new file will be created with name the same as your .csv+found.txt
An easy way is to use a batch script. You can loop through each of the files one by one. If you want to do them all at once you need to thread your program.
for /L %%A in (1,1,6) do (
Your code goes here
)
That batch script will loop six times. I am not really sure how you specify the file but if you loop through each file it will work.
So put your current batch script where I said "Your code goes here"
for /f "delims==" %%f in ('dir /s /b "P:\*.txt"') do FIND /N "[SEARCHSTRING]" "%~1%%f" >> "C:\found.txt"
But you need to edit it to point to the file that you want to search. If your files are 1.txt, 2.txt 3.txt then all you need to do is set your file name to the current loop iteration number.
I've been using variants of this shell function for years:
ematch () {
for f in $(find . -type f | grep -v '~' | grep -v \.svn\/) ; do
egrep "$1" "$f" /dev/null 2> /dev/null
done
}
-> ematch "(string1|string2|string3)"
Feel free to adapt to your needs and post your mods here.
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
???
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... ;-)