我有多个分号分隔的.csv
文件:
sample.csv
sample1.csv
...etc.
每个都有两个记录、一个标题和数据。
示例sample.csv
:
record-type-cd;record-creation-dt;product-bank-swift-cd;term-deposit-id;saving-global-servicing-bank-name;product-bank-name;customer-account-iban;customer-id;term-deposit-reference;customer-id;term-deposit-reference;payin-iban;payout-iban
ACC;2023-08-18;BBLUK;ABCDEFG-3;check24toprovide;Testcase;DE335666666666666;ABCDEFG-1;2800123456720890;ABCDEFG-1;2800123456720890;DE66110000007000110910000;BE66110000007000110910100
对于每个文件,我需要根据其数据记录的第一、第二和第四字段重命名每个文件。
对于上面的例子,那就是:
ACC2023-08-18ABCDEFG-3.csv
当我使用以下代码时,它给出delimiter__.csv
新名称:
@echo off
setlocal enabledelayedexpansion
REM Specify the input .csv file name and path
set "inputFile=sample.csv"
REM Read the 2nd line of the .csv file
set /a lineNumber=0
for /f "usebackq skip=1 delims=" %%a in ("%inputFile%") do (
set /a lineNumber+=1
if !lineNumber! equ 1 (
set "line=%%a"
goto processLine
)
)
:processLine
REM Set the delimiter used in the .csv file
set "delimiter=;"
REM Split the line into separate columns
set "colIndex=0"
for %%b in ("%line:%delimiter%=" "%") do (
set /a colIndex+=1
set "col!colIndex!=%%~b"
)
REM Get the required columns from the line
set "newFileName=!col1!_!col2!_!col4!.csv"
REM Rename the input file
ren "%inputFile%" "!newFileName!"
echo File renamed to: !newFileName!
pause