2017-02-13 114 views
0

我期待通過批處理文件刪除我的.csv文件的最後11(總是11)行,並最好不創建新命名的文件。我發現下面刪除前3行,但我不知道如何修改此刪除最後11.任何想法?謝謝你的幫助!批處理文件,刪除最後11行的CSV

@echo off 
set "csv=MyFile.csv" 
more +3 "%csv%" >"%csv%.new" 
move /y "%csv%.new" "%csv%" >nul 
+0

[Windows批處理,如何刪除第一行和最後一行並保存爲新的文本文件]的可能的重複(http://stackoverflow.com/questions/31444879/windows-batch-how-to-remove-first -line-and-last-line-and-save-as-a-new-text-fil) –

+0

不是重複的,我要求只刪除最後11行,也不想保存爲如果可能的話,新的文本/ csv文件。謝謝! – slybitz

+0

@Squashman:這是跳過前11行的計劃,而不是刪除***最後*** 11行。 – abelenky

回答

1

下面的代碼片段替換行more +3 "%csv%" >"%csv%.new"你想要做什麼,使用臨時文件:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_CSV_FILE=%~1" & rem // (specify CSV file here; `%~1` is first argument) 
set "_NUM_FRST=0" & rem // (specify number of lines to remove from beginning) 
set "_NUM_LAST=11" & rem // (specify number of lines to remove from end) 

rem // Count number of available lines in file: 
for /F %%C in ('^< "%_CSV_FILE%" find /C /V ""') do set /A "COUNT=%%C" 
set /A "COUNT-=_NUM_LAST" 
rem /* Process file, regarding and maintaining empty lines; 
rem lines must be shorter than about 8190 bytes: */ 
> "%_CSV_FILE%.tmp" (
    set /A "INDEX=0" 
    for /F "delims=" %%L in ('findstr /N "^" "%_CSV_FILE%"') do (
     set /A "INDEX+=1" 
     set "LINE=%%L" 
     setlocal EnableDelayedExpansion 
     if !INDEX! GTR %_NUM_FRST% if !INDEX! LEQ %COUNT% echo(!LINE:*:=! 
     endlocal 
    ) 
) 
rem // Overwriting original file: 
> nul move /Y "%_CSV_FILE%.tmp" "%_CSV_FILE%" 

endlocal 
exit /B 

下面的方法不使用臨時文件:

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
set "_CSV_FILE=%~1" & rem // (specify CSV file here; `%~1` is first argument) 
set "_NUM_FRST=0" & rem // (specify number of lines to remove from beginning) 
set "_NUM_LAST=11" & rem // (specify number of lines to remove from end) 

rem // Count number of available lines in file: 
for /F %%C in ('^< "%_CSV_FILE%" find /C /V ""') do set /A "COUNT=%%C" 
set /A "COUNT-=_NUM_LAST" 
rem /* Process file, regarding and maintaining empty lines; 
rem lines must be shorter than about 8190 bytes: */ 
set /A "INDEX=0" 
for /F "delims=" %%L in ('findstr /N "^" "%_CSV_FILE%" ^& ^> "%_CSV_FILE%" rem/') do (
    set /A "INDEX+=1" 
    set "LINE=%%L" 
    setlocal EnableDelayedExpansion 
    if !INDEX! GTR %_NUM_FRST% if !INDEX! LEQ %COUNT% >> "!_CSV_FILE!" echo(!LINE:*:=! 
    endlocal 
) 

endlocal 
exit /B 
+0

這仍然寫入到一個新的文件,海報明確表示要避免。 – abelenky

+1

@abelenky,它寫一個臨時文件並最終將其移動到原始文件上;我添加了一個沒有臨時文件的變體... – aschipfl

0

這裏是一個簡單的命令來計算文件中的行。

但是,我沒有看到無論如何不寫入一個單獨的中間文件截斷文件。

@echo off 
for /F "delims=[]" %%a in ('find /N /V "" myFile.TXT') do set LINE_COUNT=%%a 
echo %LINE_COUNT% 
-3

通過head -n -11 "%csv%" > "%csv%.new"

+3

'head'不是Windows命令。 – abelenky

+0

我錯過了提及我在機器上安裝了Gow(Gnu On Windows)的問題。如果您有權執行此操作,請參閱以下鏈接: https://blogs.msdn.microsoft.com/matt-harrington/2012/06/03/from-unix-to-windows-run-gnu如果您不想部署整個可執行文件,只需複製文件C:\ Program Files(x86)\ Gow \ bin \ head.exe。 –

相關問題