2016-11-17 57 views
1

我已經使用這個腳本在這裏發現SO:分割多張的CSV文件

Batch file to split .csv file

@echo off 
setLocal EnableDelayedExpansion 

set limit=50 
set file=export.csv 
set lineCounter=1 
set filenameCounter=1 

set name= 
set extension= 
for %%a in (%file%) do (
    set "name=%%~na" 
    set "extension=%%~xa" 
) 

for /f "tokens=*" %%a in (%file%) do (
    set splitFile=!name!-part!filenameCounter!!extension! 
    if !lineCounter! gtr !limit! (
     set /a filenameCounter=!filenameCounter! + 1 
     set lineCounter=1 
     echo Created !splitFile!. 
    ) 
    echo %%a>> !splitFile! 

    set /a lineCounter=!lineCounter! + 1 
) 

此拆分一個文件分成幾個小文件(50行,每行)。我想獲得它分割特定/當前目錄中的所有文件。

+1

換個'for'環周圍現有的代碼。 – DavidPostill

+0

@DavidPostill我明白那是我需要的。但我不確定如何在現有的代碼中完成它。 –

+1

for/f「tokens = *」%% f in('dir/b * .csv')do @echo用%% f做些什麼 – DavidPostill

回答

0

最終代碼感謝@DavidPostill

@echo off 
setLocal EnableDelayedExpansion 
for /f "tokens=*" %%f in ('dir /b *.csv') do (

    set limit=100 
    set lineCounter=1 
    set filenameCounter=1 

    set name= 
    set extension= 
    for %%a in (%%f) do (
     set "name=%%~na" 
     set "extension=%%~xa" 
    ) 

    for /f "tokens=*" %%a in (%%f) do (
     set splitFile=!name!-part!filenameCounter!!extension!" 
     if !lineCounter! gtr !limit! (
      set /a filenameCounter=!filenameCounter! + 1 
      set lineCounter=1 
      echo Created !splitFile!. 
     ) 
     echo %%a>> !splitFile! 

     set /a lineCounter=!lineCounter! + 1 
    ) 
)