如何将最终输出.csv文件的名称设置为文件夹的名称?

时间:2015-06-17 05:20:56

标签: batch-file cmd

我有一个可用的Windows批处理脚本,它将多个带有相同标题的CSV文件合并到一个大的CSV文件中。它如下:

@echo off

ECHO Set working directory
pushd %~dp0

ECHO Deleting existing combined file
del combined.csv

setlocal ENABLEDELAYEDEXPANSION

set cnt=1

for %%i in (*.csv) do (

  if !cnt!==1 (
    for /f "delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv
  ) else if %%i NEQ combined.csv (
    for /f "skip=1 delims=" %%j in ('type "%%i"') do echo %%j >> combined.csv
  )
  set /a cnt+=1
)

我希望输出文件与文件夹名称相同,而不是combined.csv

例如,如果文件夹的名称是ABC,则输出组合的CSV文件应为ABC.csv

1 个答案:

答案 0 :(得分:0)

@echo off
    setlocal enableextensions disabledelayedexpansion    

    pushd "%~dp0"

    set "first=1"
    for %%a in ("%~dp0\.") do (
        del "%%~nxa.csv" 2>nul 
        (for %%i in (*.csv) do (
            if /i not "%%~nxi"=="%%~nxa.csv" if defined first (
                set "first="
                type "%%~fi"
            ) else (
                (for /f "skip=1 usebackq delims=" %%j in ("%%~fi") do echo(%%j)
            )
        )) > "%%~fa\%%~nxa.csv"
    )