Home > Blockchain >  batch file to list filenames and copy its content
batch file to list filenames and copy its content

Time:03-26

I need to copy content of all text files (more than 100) with their names in a file.

I know I can list names of files.

Dir  /b *.txt > Combine.txt

I can copy content of text files like it.

copy *.txt Combine.txt

But how to mix it?

I need Combine.txt

file1.txt
Content of file1 
file2.txt
Content of file2 
file3.txt
Content of file3 
file4.txt
Content of file4 

CodePudding user response:

Whilst it may not actually look exactly as in your example, I'd suggest you give the following a try:

find /v "" *.txt > allcontent.log

And to do that more correctly:

%SystemRoot%\System32\find.exe /V "" "*.txt" 1>"allcontent.log"

The content would look a little more like this:

---------- FILE1.TXT
Content of file1 

---------- FILE2.TXT
Content of file2 

---------- FILE3.TXT
Content of file3 

---------- FILE4.TXT
Content of file4 

As a side note, I changed the destination filename to use a .log extension, to not have the results try to include its own content, (which would have happened had it also included the .txt extension).

CodePudding user response:

del 2>nul "output.xxx"&(for %J in (*.txt) do @>>output.xxx (echo %J&type "%J"))&move /y "output.xxx" "output.txt" >nul

from the prompt.

Since the output filename matches the mask *.txt, you need to generate the intermediate filename to a non-maskmatching name.

You may need to delete output.txt first to avoid including the original contents of that file in the result file.

  • Related