Windows 批处理 - 将多个文本文件连接成一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30466408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch - concatenate multiple text files into one
提问by SpeedEX505
I need to create a script, which concatenates multiple text files into one. I know its simple to use
我需要创建一个脚本,它将多个文本文件连接成一个。我知道它使用起来很简单
type *.txt > merged.txt
But the requirement is "concatenate files from same day into file day_YYYY-DD-MM.txt" I am linux user and windows batch is hell for me.
但要求是“将同一天的文件连接到文件 day_YYYY-DD-MM.txt”我是 linux 用户,windows 批处理对我来说是地狱。
EDIT: Its Windows XP
编辑:它的 Windows XP
回答by Shan
Windows type
command works similarly to UNIX cat
.
Windowstype
命令的工作方式与 UNIX 类似cat
。
Example 1:Merge with file names (This will merge file1.csv & file2.csv to create concat.csv)
示例 1:合并文件名(这将合并 file1.csv 和 file2.csv 以创建 concat.csv)
type file1.csv file2.csv > concat.csv
Example 2:Merge files with pattern (This will merge all files with csv extension and create concat.csv)
示例 2:合并带有模式的文件(这将合并所有扩展名为 csv 的文件并创建 concat.csv)
type *.csv > concat_csv.txt
回答by Lance
At its most basic, concatenating files from a batch file is done with 'copy'.
在最基本的情况下,从批处理文件中连接文件是通过“复制”完成的。
copy file1.txt + file2.txt + file3.txt concattedfile.txt
回答by Kingsman
Place all files need to copied in a separate folder, for ease place them in c drive.
将所有需要复制的文件放在一个单独的文件夹中,以便于将它们放在c盘中。
Open Command Prompt - windows>type cmd>select command prompt.
打开命令提示符-windows>输入cmd>选择命令提示符。
You can see the default directory pointing - Ex : C:[Folder_Name]>. Change the directory to point to the folder which you have placed files to be copied, using ' cd [Folder_Name] ' command.
您可以看到默认目录指向 - 例如:C:[Folder_Name]>。使用“ cd [Folder_Name] ”命令将目录更改为指向您放置要复制的文件的文件夹。
After pointing to directory - type 'dir' which shows all the files present in folder, just to make sure everything at place.
指向目录后 - 键入“dir”,它显示文件夹中存在的所有文件,只是为了确保所有内容都到位。
Now type : 'copy *.txt [newfile_name].txt' and press enter.
现在输入:'copy *.txt [newfile_name].txt'并按回车键。
Done!
完毕!
All the text in individual files will be copied to [newfile_name].txt
单个文件中的所有文本都将复制到 [newfile_name].txt
回答by Gigasoup
I am reiterating some of the other points already made, but including a 3rd example that helps when you have files across folders that you want to concatenate.
我重申了已经提出的其他一些观点,但包括第三个示例,当您跨文件夹拥有要连接的文件时,该示例会有所帮助。
Example 1 (files in the same folder):
示例 1(同一文件夹中的文件):
copy file1.txt+file2.txt+file3.txt file123.txt
Example 2 (files in same folder):
示例 2(同一文件夹中的文件):
type *.txt > combined.txt
Example 3 (files exist across multiple folders, assumes newfileoutput.txt doesn't exist):
示例 3(文件存在于多个文件夹中,假设 newfileoutput.txt 不存在):
for /D %f in (folderName) DO type %f/filename.txt >> .\newfileoutput.txt
回答by Ghoul Fool
In Win 7, on the command prompt use:
在 Win 7 中,在命令提示符下使用:
copy *.txt combined.txt
回答by Stephen
cat "input files" > "output files"
cat“输入文件”>“输出文件”
This works in PowerShell, which is the Windows preferred shell in current Windows versions, therefore it works. It is also the only version of the answers above to work with large files, where 'type' or 'copy' fails.
这适用于 PowerShell,这是当前 Windows 版本中的 Windows 首选 shell,因此它可以工作。它也是上述答案的唯一版本,可以处理“类型”或“复制”失败的大文件。
回答by Jorgesys
You can do it using type:
你可以使用类型来做到这一点:
type"C:\<Directory containing files>\*.txt"> merged.txt
all the files in the directory will be appendeded to the file merged.txt
.
目录中的所有文件都将附加到文件中merged.txt
。
回答by Fernando Madriaga
Try this:
尝试这个:
@echo off
set yyyy=%date:~6,4%
set mm=%date:~3,2%
set dd=%date:~0,2%
set /p temp= "Enter the name of text file: "
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%temp%.txt
This code ask you to set the name of the file after "day_" where you can input the date. If you want to name your file like the actual date you can do this:
此代码要求您在“day_”之后设置文件名,您可以在其中输入日期。如果你想像实际日期一样命名你的文件,你可以这样做:
FOR /F "tokens=* delims=" %%x in (texto1.txt, texto2.txt, texto3.txt) DO echo %%x >> day_%yyyy%-%mm%-%dd%.txt
回答by Sadheesh
We can use normal CAT command to merge files..
我们可以使用普通的 CAT 命令来合并文件..
D:> cat *.csv > outputs.csv
D:> cat *.csv > 输出.csv