バッチファイルを使用して、ディレクトリ「Data」内の複数のファイルのすべての空白行を削除したいと思います。ファイルの名前を変更したくありません。
私はこの投稿を見ましたが、役に立ちません:次の理由でディレクトリ内の複数のファイルから空白行を削除する方法:*ファイルの名前が変更されます*ファイルは.batファイルと同じディレクトリにある必要があります
バッチファイルのコマンドについても説明していただければ幸いです。
ありがとう。
すべての説明をコメントとして含めることにしました。名前の変更/移動操作なしでそれを行う方法はいくつかありますが、これほど信頼性は高くありません。とにかく、最後に、ファイルは同じ名前になりますが、空の行はありません。
@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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加