Home > Software design >  How to insert text into multiple text files using a .bat file?
How to insert text into multiple text files using a .bat file?

Time:04-25

I having drouble to create a batch file that can insert text into multiple text files at once.

What I can do just now is that one file that I drop on the batch file is getting inserted with the text "I AM PROGRAMMING". But I would like to drag multiple text files and that all of it/each every text file gets inserted with that text.

@echo off
echo I AM PROGRAMMING> "%~1"

Or if there is somehow possible to do so every text file in a specific place/folder gets inserted with a specific text (for example "I AM PROGRAMMING")?

CodePudding user response:

Your solution processes each matching file in the folder, but you said I would like to drag multiple text files.

It's easy to process "multiple dragged files": %* is "all parameters". Just use a plain for loop to process each of them:

@echo off
for %%a in (%*) do echo I AM PROGRAMMING> "%%~a"

(note that this overwrites the files with the one new line; if you want to append, use >> instead of >)

In case you want to process "all .txt files in the folder of the one file I dragged":

for %%a in ("%~dp1\*.txt") do ...

Or "all files in the same folder of the one file I dragged and the same extension as the dragged file":

for %%a in ("%~dp1*%~x1") do  ...

CodePudding user response:

I found my solution!

@echo off
cd "yourfilepath"
for %%a in (*.txt) do (echo I AM PROGRAMMING> %%a)

I merged these two helpful information.

How can I use a batch file to write to a text file? And Append multiple files using a .bat

  • Related