我已编写代码将示例文件连接到单个文件中减去每个文件的标题。
输入文件:
File1中:
[ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy 00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14 00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
文件2:
[ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy 00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43
预期输出:
[ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy 00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43 00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14 00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
实际输出:
[ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy 00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43 00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14 [ Row : Header ],,,,,,,,, ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy 00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
请在下面找到用于此操作的代码:
@echo off
break>Combined.csv
cls
setlocal enabledelayedexpansion
if exist C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv del C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv
dir /a-d /b C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv>C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt
cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\
for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
set /p header=<%%A
if "!header!" neq "" (
(echo(!header!)>Combined.csv
goto :break_for
)
)
:break_for
for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
more +1 %%A>>Combined.csv
)
del dirfiles.txt
}
有人可以帮我解决这个问题。我是批处理脚本的新手,无法调试此问题。
答案 0 :(得分:1)
有关这个问题的几点意见:
编辑:我根据评论中发布的新规范修改了代码:每个文件中有三行标头,但只有第3个必须包含在输出中。
@echo off
setlocal enabledelayedexpansion
cls
REM cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\
set "header3="
(for %%A in (*.csv) do (
if not defined header3 (
(set /p "header1=" & set /p "header2=" & set /p "header3=") <%%A
echo !header3!
)
more +3 %%A
)) > Combined.txt
[ Row : Header ],,,,,,,,,
ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy
00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43
如您所见,输出与您想要的相同。
编辑:我无法测试修改,因为发布的输入文件不包含与真实文件相同的数据...
答案 1 :(得分:0)
不需要包含CSV文件列表的临时文件,您可以使用其{{1}通过标准for
循环和嵌套for /F
循环读取和组合它们删除标题的选项(假设标题总是一行)。初始头可以从另一个skip
/ for
循环结构中获取,该结构在第一次迭代时被破坏:
for /F
如果您需要CSV文件的特定排序顺序,则需要另一个> "C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv" (
for %%F in ("C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv") do (
for /F "usebackq eol=| delims=" %%L in ("%%~F") do (
echo(%%L
goto :LEAVE
)
)
)
:LEAVE
>> "C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv" (
for %%F in ("C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv") do (
for /F "usebackq skip=1 eol=| delims=" %%L in ("%%~F") do (
echo(%%L
)
)
)
循环而不是标准for /F
循环来解析for
命令的输出来完成该工作。以下示例采用两行标题,然后将文件从最旧的修改日期排序到最新修改日期:
dir /B
答案 2 :(得分:0)
如果您想安装awk
- Unix / Linux中最方便的程序之一 - 您的任务将变得非常简单。它可从here开始用于Windows。
然后你可以使用:
awk 'NR<3 || FNR>2' *.csv
要解释该命令,您需要知道NR
是Number of the Record
(即行号),它从第一个文件的第一个记录/行开始,然后随之增加每条记录,只有第一个文件的前两个记录,它将小于3。另一方面,FNR
是File Number of Record
是相同的,但在每个新文件打开时它会重置为1,因此对于每个文件的前两个记录,它将小于2。
因此,总而言之,命令说... “如果它是所有输入文件的前两行之一,或者如果它超过任何文件的第2行,则打印任何行。 “
请注意,您可能需要在Windows上用双引号替换单引号。
请注意,如果您要下载gawk
,则此示例与awk
的工作方式相同。