Windows Batch File with Type Command

Windows Batch File with Type Command

I have 10 files like below:

  • file1.txt (file content = "i contain text for file1")
  • file2.txt (file content = "i contain text for file2")
  • file3.txt (file content = "i contain text for file3")

and so


I am trying to do a TYPE command like:

type file*.txt > OUTPUT.txt

this works to output the file contents only. i need output to also have filename for each file.

my output.txt file should look like this:

file1.txt = "i contain text for file1"
file2.txt = "i contain text for file2"
file3.txt = "i contain text for file3"
2

3 Answers

Create a batch file called proc.bat which contains:

echo %1=>>output.txt
type %1>>output.txt

Then use this command:

for %v in (file*.txt) do proc %v

Use next command from cmd window:

>output.txt (for /F "tokens=1* delims=:" %G in ('findstr "^" "file*.txt"') do @echo %G = "%H")

If used in a batch script, double percent signs in for loop inner variables (i.e. %%G, %%H instead of %G, %H respectively):

@echo off
>output.txt (for /F "tokens=1* delims=:" %%G in ('findstr "^" "file*.txt"') do echo %%G = "%%H")

Resources (required reading):

0

You can try:

for /l %f in (1,1,3) do echo|set /p="File %f = " >> output.txt & type file%f.txt >> output.txt

This will yield a file that looks like:

File 1 = Text in file1
File 2 = Text in file2
File 3 = Text in file3

The for /l command counts from 1 to 3 in increments. In general (start,increment,end).

Your Answer

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

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.