1

I have 3 text files in a particular folder,

A.txt B.txt C.txt

Using batch file i like to input the word hello in all the text files in the particular folder.

I am trying,

@echo off
echo hello>>*.txt

Unfortunately, it doesn't work. Please guide me how to do it correctly.

I like to have output like,

hello in the file A.txt
hello in the file B.txt
hello in the file C.txt

  • 4
    for %%f in (A.txt B.txt C.txt) do (echo hello>>%%f) – DavidPostill Mar 17 '19 at 17:20
  • @Biswapriyo that would double the extension and also process subfolders. Also I'd call it appending to a file not input. – LotPings Mar 17 '19 at 19:53
0

Microsoft Windows [Version 10.0.17134.648]

append hello w/ batch:

for %%f in (A.txt B.txt C.txt) do echo hello>>%%f

append hello w/ command line:

for %f in (A.txt B.txt C.txt) do echo hello>>%f

append hello to all .txt files in directory w/ batch. Works if a space is in the path/filename:

for /f "tokens=*" %%f in ('dir /b path-to-parent-folder-with-or-without-double-quotes\*.txt') do echo hello>>%%f

append hello to all .txt files in directory w/ command line. Works if a space is in the path/filename:

for /f "tokens=*" %f in ('dir /b path-to-parent-folder-with-or-without-double-quotes\*.txt') do echo hello>>%f
| improve this answer | |
0

To add hello to each of the text files in a folder, you can use a For loop with /r:

@echo off

set "src=C:\your\folder"

for /r "%src%" %%A in (*.txt) do (
    echo hello >> "%%~fA"
)

For each text file (*.txt) rooted in the source (src) directory: set as parameter %%A and add the line hello at the end. %%~fA is the fully qualified path name (including the extension) of the %%A parameter.

To limit it to just your three files (A.txt, B.txt, and C.txt), you can specify those instead of using the wildcard above - which would look like this:

@echo off

set "src=C:\your\folder"

for /r "%src%" %%A in (A.txt B.txt C.txt) do (
    echo hello >> "%%~fA"
)

Either way - if you're aiming for a specific folder, for /r is what you're looking for.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.