[英]Write data from one excel sheet to another
I have some .csv files in my folder with names like 1filename.csv
, 2filename.csv
, 3filename.csv
then 1abc.csv
, 2abc.csv
and so on. 我有我的文件夹中的某些文件的.csv与像名
1filename.csv
, 2filename.csv
, 3filename.csv
然后1abc.csv
, 2abc.csv
等。
I need to merge files with same post name and each file has same no. 我需要合并具有相同帖子名称的文件,并且每个文件都具有相同的编号。 of columns with same heading.So final file should have only one row with titles for columns and appended with data.
因此,最终文件应仅包含一行标题列并附加数据的行。
Example: 1. 1filename.csv
, 2filename.csv
, 3filename.csv
should be merged to filename.csv
(where the data is appended one after the other) 示例:1.应该将
1filename.csv
, 2filename.csv
, 3filename.csv
合并到filename.csv
(其中数据一个接一个地追加)
2. 1abc.csv
, 2abc.csv
will be merged and to form abc.csv 2.
1abc.csv
和2abc.csv
将合并并形成abc.csv
Can I achieve this in batch file or with the help of phpexcel? 我可以在批处理文件中还是在phpexcel的帮助下实现此目的? I want to do in batch file but i am not sure that it is possible.
我想在批处理文件中执行操作,但是我不确定是否有可能。
Thanks 谢谢
These are the files which needs to be merged I will have 1000s of files splitted with different domains : 这些是需要合并的文件,我将用不同的域拆分成千上万个文件:
This is how csv files will look like, one file can have maximum of 800 rows and same number of columns with same heading: 这是csv文件的外观,一个文件最多可以包含800行,并且标题相同的列数也相同:
Iin cmd you can use: 您可以使用Iin cmd:
copy /b 1filename.csv+2filename.csv filename.csv 复制/ b 1filename.csv + 2filename.csv filename.csv
copy /b 1abc.csv+2abc.csv abc.csv 复制/ b 1abc.csv + 2abc.csv abc.csv
EDIT : Small bug fixed 编辑 : 修复了小错误
The original code does not take into account that the file names will be processed in alphabetic order, not in post-name groups. 原始代码未考虑文件名将按字母顺序处理,而不是按名称后的组处理。
@echo off
setlocal EnableDelayedExpansion
rem I have some .csv files in my folder
rem with names like 1filename.csv, 2filename.csv, 3filename.csv then 1abc.csv, 2abc.csv and so on.
set "postName="
for /F %%a in ('dir /B *.csv ^| sort /+2') do (
rem I need to merge files with same post name
set "name=%%a"
if "!name:~1!" neq "!postName!" (
set "postName=!name:~1!"
rem and each file has same no. of columns with same heading.
rem So final file should have only one row with titles for columns
echo Merging data into: !postName!
copy "%%a" "!postName!" > NUL
) else (
rem and appended with data.
(for /F "skip=1 delims=" %%b in (%%a) do echo %%b) >> "!postName!"
)
)
You always should post a segment of the files or describe its contents at least, otherwise we can only guess about it... 您总是应该发布文件的一部分或至少描述其内容,否则我们只能猜测一下...
This Batch solution may fail if the files contains special Batch characters, like < > | !
如果文件包含特殊的批处理字符,例如
< > | !
,则此批处理解决方案可能会失败< > | !
< > | !
EDIT : Reply to comments 编辑 : 回复评论
Please, note that the info in a picture can not be copy-pasted as text. 请注意,图片中的信息不能复制粘贴为文本。 You should post data as text enclosed in code tags, like this one:
您应该将数据以文本形式封装在代码标签中,如下所示:
C:\ dir /b
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat
C:\ test
Merging data into: www.ais.csv
Merging data into: www.futureshade.com.au.csv
C:\ dir /b
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat
www.ais.csv
www.futureshade.com.au.csv
Note that this solution is designed for filenames with just one digit in the first character, so it fail if there are more than 9 files in the same post name (this is the type of details that you should post since the very begining). 请注意,此解决方案是为第一个字符中只有一位数字的文件名设计的,因此,如果相同的帖子名称中有9个以上的文件,则此解决方案将失败(这是从一开始就应发布的详细信息类型)。
2ND EDIT : Output testing added 第二编辑 : 添加输出测试
The test below show that this solution works as stated: 下面的测试表明此解决方案按规定工作:
C:\ dir /b
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat
C:\ type 1www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1AIS,ais,AUD,14800,0.01,3.39,--,N,N
1AIS,medicare,AUD,201000,0.04,2.97,--,N,N
C:\ type 2www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
2AIS,ais,AUD,14800,0.01,3.39,--,N,N
2AIS,medicare,AUD,201000,0.04,2.97,--,N,N
C:\ type 3www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
3AIS,ais,AUD,14800,0.01,3.39,--,N,N
3AIS,medicare,AUD,201000,0.04,2.97,--,N,N
C:\ type 1www.futureshade.com.au.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
1FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N
C:\ type 2www.futureshade.com.au.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
2FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
2FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N
C:\ test
Merging data into: www.ais.csv
Merging data into: www.futureshade.com.au.csv
C:\ dir /B
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat
www.ais.csv
www.futureshade.com.au.csv
C:\ type www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1AIS,ais,AUD,14800,0.01,3.39,--,N,N
1AIS,medicare,AUD,201000,0.04,2.97,--,N,N
2AIS,ais,AUD,14800,0.01,3.39,--,N,N
2AIS,medicare,AUD,201000,0.04,2.97,--,N,N
3AIS,ais,AUD,14800,0.01,3.39,--,N,N
3AIS,medicare,AUD,201000,0.04,2.97,--,N,N
C:\ type www.futureshade.com.au.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
1FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N
2FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
2FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.