Faylni birlashtirish va o'zgartirish uchun Windows ommaviy kodi

Csv fayli bir jildga juda ko'p. Fayllar OPERATORS _ * csv deb nomlanadi, bu erda * o'zgaruvchidir.

Barcha fayllarni bitta faylga birlashtirish uchun ommaviy ish fayllaridan foydalanib, har bir faylning birinchi qatorini o'chirib tashlang va har bir qatorning oxiriga * qo'shing.

Ushbu kodni sinab ko'rdim:

copy /b OPERATORS_*.csv OPERATORS_FULL.csv

Bu yaxshi, lekin har bir faylning birinchi qatori chop etiladi va fayl nomidagi xususiyatni yo'qotib qo'ydim.

Misol:

OPERATORS_ACTIVITY1.csv

OPT;SALES;REDEMPTION
OPT1;12;75

OPERATORS_ACTIVITY2.csv

OPT;SALES;REDEMPTION
OPT2;22;64

va buni xohlayman:

OPERATORS_FULL.csv

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

Har qanday taklif bormi?

1
Har bir kirish fayli faqat sarlavha bilan birga bitta qatorni o'z ichiga oladimi yoki undan ko'p satr bo'lishi mumkinmi?
qo'shib qo'ydi muallif aschipfl, manba

6 javoblar

Buni ko'ring (yangilash # 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
qo'shib qo'ydi
@TedGatiss: Yana bir yangilash ...
qo'shib qo'ydi muallif zb226, manba
@TedGatiss: Domen nomida ikkinchi _ mavjudmi, deb hisoblay olamizmi? Yoki bu OPERATORS_APP_WHATEVER.csv kabi fayllar bormi?
qo'shib qo'ydi muallif zb226, manba
@TedGatiss: Ha, yangilanishni tekshiring ...
qo'shib qo'ydi muallif zb226, manba
Men kodingizni sinab ko'rdim. Statik "attr" o'zgaruvchiga ega bo'lsam yaxshi ishlaydi. Lekin APP_MKT_SELLING_yyyymmdd yoki MKT_yyyymmdd bo'lishi mumkin ... Sana har doim oxirgi element hisoblanadi. Ikkinchisi juda ... "o'zgarmaydigan": D
qo'shib qo'ydi muallif Ted Gatiss, manba
Ajoyib! Bu yaxshi ishlaydi. Ba'zan fayl nomi - ACTIVITY1_20170310. Bo'lishi mumkin va keyin qatorni oxirigacha kiritish mumkinmi?
qo'shib qo'ydi muallif Ted Gatiss, manba
OPERATORS_APP_MKT_XXX_yyyymmdd bo'lishi mumkin yoki OPERATORS_APP_XXX_yyyymmdd bo'lishi mumkin ... standart "_"
qo'shib qo'ydi muallif Ted Gatiss, manba
GENIUS! Rahmat !!!
qo'shib qo'ydi muallif Ted Gatiss, manba

Buni ko'ring (yangilash # 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
qo'shib qo'ydi
@TedGatiss: Yana bir yangilash ...
qo'shib qo'ydi muallif zb226, manba
@TedGatiss: Domen nomida ikkinchi _ mavjudmi, deb hisoblay olamizmi? Yoki bu OPERATORS_APP_WHATEVER.csv kabi fayllar bormi?
qo'shib qo'ydi muallif zb226, manba
@TedGatiss: Ha, yangilanishni tekshiring ...
qo'shib qo'ydi muallif zb226, manba
Men kodingizni sinab ko'rdim. Statik "attr" o'zgaruvchiga ega bo'lsam yaxshi ishlaydi. Lekin APP_MKT_SELLING_yyyymmdd yoki MKT_yyyymmdd bo'lishi mumkin ... Sana har doim oxirgi element hisoblanadi. Ikkinchisi juda ... "o'zgarmaydigan": D
qo'shib qo'ydi muallif Ted Gatiss, manba
Ajoyib! Bu yaxshi ishlaydi. Ba'zan fayl nomi - ACTIVITY1_20170310. Bo'lishi mumkin va keyin qatorni oxirigacha kiritish mumkinmi?
qo'shib qo'ydi muallif Ted Gatiss, manba
OPERATORS_APP_MKT_XXX_yyyymmdd bo'lishi mumkin yoki OPERATORS_APP_XXX_yyyymmdd bo'lishi mumkin ... standart "_"
qo'shib qo'ydi muallif Ted Gatiss, manba
GENIUS! Rahmat !!!
qo'shib qo'ydi muallif Ted Gatiss, manba
@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

Avval chiqadigan faylni o'chirib tashlang va faylni .txt faylga nusxalashni boshlang, lekin birinchi satrdan keyin ataylab bekor qiling.

har bir fayl uchun %% f faylning nom qismidagi _ kodini nusxa ko'chiring, keyingi satrda _ faylning bir qismi %% t , skip birinchidan ping va .txt faylga qo'shing (tashqi juftlikning Qavslar - bu sintaksik kod butun blok blokining chiqishini qayta yo'naltirishga imkon beradi)

Nihoyat, faylni ko'chiring yoki nomini o'zgartiring.

Oh - bosh sarlavhasini xohlamaysizmi? Birinchi uchun satrini tashlang.

0
qo'shib qo'ydi
Sizning yechimingiz fayl nomining bir qismini so'ralgan ma'lumotlarga qo'shilmaydi ...
qo'shib qo'ydi muallif zb226, manba
@aschipfl: Juda to'g'ri. mos ravishda sozlangan. Men aslida tekshirmaganimdan keyin ...
qo'shib qo'ydi muallif Magoo, manba
@ zb226: - oh - Endi siz meni o'qish savolini ham so'rayapsizmi? youtube.com/watch?v=pc4IFIXcDcs
qo'shib qo'ydi muallif Magoo, manba
for/F kodi * yoki ? kabi yovvoyi kartalarni qabul qilmaydi ...
qo'shib qo'ydi muallif aschipfl, manba
@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

Avval chiqadigan faylni o'chirib tashlang va faylni .txt faylga nusxalashni boshlang, lekin birinchi satrdan keyin ataylab bekor qiling.

har bir fayl uchun %% f faylning nom qismidagi _ kodini nusxa ko'chiring, keyingi satrda _ faylning bir qismi %% t , skip birinchidan ping va .txt faylga qo'shing (tashqi juftlikning Qavslar - bu sintaksik kod butun blok blokining chiqishini qayta yo'naltirishga imkon beradi)

Nihoyat, faylni ko'chiring yoki nomini o'zgartiring.

Oh - bosh sarlavhasini xohlamaysizmi? Birinchi uchun satrini tashlang.

0
qo'shib qo'ydi
Sizning yechimingiz fayl nomining bir qismini so'ralgan ma'lumotlarga qo'shilmaydi ...
qo'shib qo'ydi muallif zb226, manba
@aschipfl: Juda to'g'ri. mos ravishda sozlangan. Men aslida tekshirmaganimdan keyin ...
qo'shib qo'ydi muallif Magoo, manba
@ zb226: - oh - Endi siz meni o'qish savolini ham so'rayapsizmi? youtube.com/watch?v=pc4IFIXcDcs
qo'shib qo'ydi muallif Magoo, manba
for/F kodi * yoki ? kabi yovvoyi kartalarni qabul qilmaydi ...
qo'shib qo'ydi muallif aschipfl, manba

Qayta yo'naltirishni ishlatib, boshqa usul - skriptdagi barcha tavsiflovchi rem izohlarini ko'ring:

@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
0
qo'shib qo'ydi

Qayta yo'naltirishni ishlatib, boshqa usul - skriptdagi barcha tavsiflovchi rem izohlarini ko'ring:

@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
0
qo'shib qo'ydi