用于合并文件和编辑的Windows批处理代码

时间:2017-03-10 08:51:06

标签: windows batch-file cmd

我在文件夹中有很多csv文件。 这些文件的名称类似于OPERATORS_*.csv,其中*是变量。

我希望使用批处理文件将所有文件合并为一个文件,删除每个文件的第一行,并在每行的末尾添加*

我试过这段代码:

copy /b OPERATORS_*.csv OPERATORS_FULL.csv

这种方式很好,但每个文件的第一行都打印出来,我丢失了文件名中的属性。

示例:

OPERATORS_ACTIVITY1.csv

OPT;SALES;REDEMPTION
OPT1;12;75

OPERATORS_ACTIVITY2.csv

OPT;SALES;REDEMPTION
OPT2;22;64

我想要这个:

OPERATORS_FULL.csv

OPT1;12;75;ACTIVITY1
OPT2;22;64;ACTIVITY2

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

试试这个(更新#2)

@ECHO OFF
SETLOCAL EnableDelayedExpansion

IF EXIST OPERATORS_FULL.csv DEL OPERATORS_FULL.csv
IF EXIST OPERATORS_FULL.tmp DEL OPERATORS_FULL.tmp

FOR %%A IN ( OPERATORS_*.csv ) DO (
    :: get attribute from filename
    SET "attr=%%A"
    SET "attr=!attr:OPERATORS_=!"
    SET "attr=!attr:.csv=!"
    :: get date suffix
    SET tmp=!attr:_= !
    FOR %%G IN ( !tmp! ) DO (
        SET date_=%%G
    )
    :: if we have a date (i.e. a numeric value)
    IF !date_! EQU +!date_! (
        :: ...remove date from attr with leading underscore
        CALL SET attr=%%attr:_!date_!=%%
    ) ELSE (
        :: ...else clear date variable
        SET date_=
    )
    :: dump CSVs, skipping each header line, adding the attribute from the filename
    FOR /F "skip=1 tokens=*" %%G IN ( %%A ) DO ECHO %%G;!attr!;!date_! >> OPERATORS_FULL.tmp
)

REN OPERATORS_FULL.tmp OPERATORS_FULL.csv

答案 1 :(得分:0)

@echo off
setlocal
del operators_full.csv 2>nul >nul 
FOR %%f IN (operators_*.csv) DO for /f "usebackqdelims=" %%a in ("%%f") do echo %%a>operators_full.txt&goto body
:body
(
FOR %%f IN (operators_*.csv) DO FOR /f "tokens=1*delims=_" %%s IN ("%%~nf") DO for /f "skip=1usebackqdelims=" %%a in ("%%f") do echo %%a;%%t
)>>operators_full.txt

move operators_full.txt operators_full.csv

首先,删除输出文件(如果存在),然后开始将文件复制到.txt文件,但在第一行后故意中止。

然后,对于每个文件,文件_的名称部分中的%%f上的标记复制每一行,并在{{1}中附加文件名的后_部分},%%t ping第一个并附加到skip文件(注意外部括号对的位置 - 此语法允许重定向整个代码块的输出)

最后,移动或重命名文件。

哦 - 你不想要标题行?省略第一个.txt行。

答案 2 :(得分:0)

以下是使用重定向的不同方法 - 请参阅脚本中的所有解释性rem备注:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_INPUT=OPERATORS_*.csv"     & rem // (input files)
set "_OUTPUT=OPERATORS_FULL.csv" & rem // (output file)
set /A "_SKIP=1" & rem // (number of lines to skip for each input file)

rem // Redirect the whole output at once:
> "%_OUTPUT%" (
    rem // Iterate over all the input files:
    for %%F in ("%_INPUT%") do (
        rem // Store the current file name to get the attribute name later:
        set "NAME=%%~nF"
        rem // Exclude the output file from being processed:
        if /I not "%%~nxF"=="%_OUTPUT%" (
            rem // Determine the number of lines of the current input file:
            for /F %%E in ('^< "%%~F" find /C /V ""') do set /A "CNT=%%E"
            rem // Read current input file:
            < "%%~F" (
                setlocal EnableDelayedExpansion
                rem // Loop over every line:
                for /L %%E in (1,1,!CNT!) do (
                    rem // Read current line:
                    set "LINE=" & set /P LINE=""
                    rem // Return current line if it is not to be skipped:
                    if %%E GTR %_SKIP% echo(!LINE!;!NAME:*_=!
                )
                endlocal
            )
        )
    )
)

endlocal
exit /B