简体   繁体   English

将文本文件复制到一个包含文件名的文件中

[英]Copy text files into one file including filenames

From the Windows command prompt it's possible to collect all text files into one file using 在Windows命令提示符下,可以使用以下命令将所有文本文件收集到一个文件中

copy *.txt all_text_files.txt

Is it possible to add the name of each file added in front of the file in question? 是否可以在问题文件之前添加每个文件的名称? In my example I have two text files a & b. 在我的示例中,我有两个文本文件a和b。

a.txt consists of one line: a.txt包含一行:

Cheese

b.txt also consists of one line: b.txt也包含一行:

Monkeys

The new file would be: 新文件将是:

a.txt
Cheese
b.txt
Monkeys

If you write it directly in the command line, this should work. 如果您直接在命令行中编写它,那应该可以。 If you want to make a script I believe you need to put double % 如果您想编写脚本,我相信您需要加倍%

for %f in (*.txt) do (
    echo "%f"
    type "%f"
) > all_text_files.txt
type *.txt >allfiles.out 2>&1
ren allfiles.out allfiles.txt

type types all matching files. type所有匹配文件的类型。 The file contents goes to STDOUT, the filenames are printed to STDERR. 文件内容转到STDOUT,文件名打印到STDERR。 So just redirect both to the new file. 因此,只需将它们都重定向到新文件即可。 Choose a different extension to avoid recursive handling. 选择其他扩展名以避免递归处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理将多个文本文件的第三行复制到新文本文件中,并将原始文件名附加到每行 - batch to copy third line of multiple text files into a new text file and append original filenames to each line 将文本文件复制到其他位置,然后将它们合并为一个文本文件 - copy text files in different location and combine them to one text file Output 文件夹中的文件名到文本文件 - Output Filenames in a Folder to a Text File 从文本文件复制文件的批处理脚本 - Batch Script to copy files from a Text file 在批处理脚本(Windows)中使用随机文件名创建n个文件 - 我的脚本只创建一个文件 - Create n files with random filenames in batch script (Windows) - my script creates only one file 如何将文本文件复制到同一文本文件中的指定路径 - How to copy files in text to, to specified path in same text file cmd从部分文件名列表中搜索文件,然后复制到文件夹 - cmd Search for files from list of partial filenames then copy to folder PowerShell Output 文本文件的特定扩展名的文件名 - PowerShell Output Filenames of a Specific Extension to a Text File 寻找正则表达式以在文本文件中查找长文件名 - Looking for a regex to find long filenames in a text file 想要根据文件扩展名将文件名复制到目录中的文本中 - want to copy names of files to text in a directory based on their file extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM