2015-02-16 4 views
0

Я хотел бы использовать пакетный файл для удаления всех пустых строк в нескольких файлах в каталоге «Данные». Я не хочу переименовывать файлы.удалите пустые строки из нескольких файлов в определенном каталоге

Я видел этот пост, но это не помогает: How to delete blank lines from multiple files in a directory по следующим причинам: * Файлы переименовываются * Файлы должны быть в том же каталоге, что и файл .bat

Если вы могли бы также объяснить командами командного файла, то это было бы весьма полезно.

Спасибо.

ответ

1

Я решил включить все объяснения в качестве комментариев. Есть несколько способов сделать это без операции переименования/перемещения, но они не столь надежны, как это. В любом случае, в конце, файлы будут иметь то же имя, но не пустые строки.

@echo off 
    setlocal enableextensions disabledelayedexpansion 

    rem There are some problems with references to batch files 
    rem that are called with quotes. To avoid the problems, a 
    rem subroutine is used to retrieve the information of 
    rem current batch file 

    call :getBatchFileFullPath batch 

    rem From the full path of the batch file, retrieve the 
    rem folder where it is stored 

    for %%a in ("%batch%") do set "folder=%%~dpa" 

    rem We will use a temporary file to store the valid 
    rem lines while removing the empty ones. 

    set "tempFile=%folder%\~%random%%random%%random%" 

    rem For each file in the batch folder, if the file is 
    rem not the batch file itself 

    for %%a in ("%folder%\*") do if /i not "%%~fa"=="%batch%" (

     rem Now %%a holds a reference to the file being processed 
     rem We will use %%~fa to get the full path of file. 

     rem Use findstr to read the file, and retrieve the 
     rem lines that 
     rem /v   do not match 
     rem /r   the regular expression 
     rem /c:"^$" start of line followed by end of line 
     rem and send the output to the temporary file 

     findstr /v /r /c:"^$" "%%~fa" > "%tempFile%" 

     rem Once we have the valid lines into the temporary 
     rem file, rename the temporary file as the input file 
     move /y "%tempFile%" "%%~fa" >nul 
    ) 

    rem End - Leave the batch file before reaching the subroutine 
    exit /b 

rem Subrotutine used to retrieve batch file information. 
rem First argument (%1) will be set to the name of a variable 
rem that will hold the full path to the current batch file. 

:getBatchFileFullPath returnVar 
    set "%~1=%~f0" 
    goto :eof 

раскомментирована версия

@echo off 
    setlocal enableextensions disabledelayedexpansion 

    call :getBatchFileFullPath batch 
    for %%a in ("%batch%") do set "folder=%%~dpa" 
    set "tempFile=%folder%\~%random%%random%%random%" 

    for %%a in ("%folder%\*") do if /i not "%%~fa"=="%batch%" (
     findstr /v /r /c:"^$" "%%~fa" > "%tempFile%" 
     move /y "%tempFile%" "%%~fa" >nul 
    ) 
    exit /b 

:getBatchFileFullPath returnVar 
    set "%~1=%~f0" 
    goto :eof 
+0

Назад к этому после долгого времени ... Я в конечном итоге копирование файла .bat в той же директории, что и файлы с пустыми строками - однако, как я изменит это, чтобы иметь .bat-файл за пределами этого каталога .. например, структура каталогов Родительский: содержит .bat-файл Parent \ Data: содержит файлы с пустыми строками – user1420372

+1

@ user1420372, измените строку, которая назначает папку для обработки ' для %% a in ("% batch%") установить "folder = %% ~ dpa \ Data" ' –

Смежные вопросы