Windows批处理代码合并文件并编辑



我在文件夹中有很多CSV文件。文件命名为 OPERATORS_*.csv,其中 *是一个变量。

我想要使用批处理文件,将所有文件合并到一个文件中,删除每个文件的第一行,然后在每行末尾添加*

我尝试了此代码:

copy /b OPERATORS_*.csv OPERATORS_FULL.csv

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

示例:

操作员_activity1.csv

OPT;SALES;REDEMPTION
OPT1;12;75

operators_activity2.csv

OPT;SALES;REDEMPTION
OPT2;22;64

我想要这个:

操作员_full.csv

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

有什么建议?

尝试以下(更新#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

这是一种使用重定向的不同方法 - 请参阅脚本中的所有解释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
@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文件,但在第一行之后故意中止。

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

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

哦 - 您不想要标题线吗?省略第一个for行。

最新更新