如何检测(和删除)的文件,如果使用的是DOS批处理文件是空的? [英] How do I Detect (and Delete) a File if it is Empty using a DOS Batch File?
问题描述
我如何检测一个文件中使用DOS批处理文件是空的?我想删除错误日志,如果他们是空的。
How do I detect if a file is empty using a DOS batch file? I want to delete error logs if they are empty.
我知道为循环解决方案,如<一个href=\"http://anderwald.info/scripting/windows-batch-delete-all-empty-files-in-a-specified-folder/\">http://anderwald.info/scripting/windows-batch-delete-all-empty-files-in-a-specified-folder/但我想知道是否有一个更好的解决方案,如果你知道有关具体的单个文件,需要进行测试,因为我可能不希望在文件夹中删除其它的零字节文件。
I am aware for for loop solutions such as http://anderwald.info/scripting/windows-batch-delete-all-empty-files-in-a-specified-folder/ but I was wondering if there was a more elegant solution if you know the specific single file in question that needs to be tested, as I may not wish to delete other zero byte files in the folder.
推荐答案
在引使用的链接下面的删除所有0长度的文件
The link you cited used the following to delete all 0 length files
for /F "delims=" "%I" in ('dir /B') do if not exist "%I\" if %~zI EQU 0 del "%I"
这是更复杂,效率不高,因为它可以。这是更简单简单地使用:
That is more complicated and not as efficient as it could be. It is much simpler to simply use:
for %F in (*) do if %~zF equ 0 del "%F"
您想删除特定文件,如果它的长度为零。所以,刚刚替补文件名称为 *
通配符。
You want to delete a specific file if it is zero length. So just substitute your file name for the *
wild card.
for %F in ("yourFileName") do if %~zF equ 0 del "%F"
如果您打算比您需要的所有的百分数( %%˚F
, %%〜ZF翻倍使用这个批处理文件
)
If you are going to use this in a batch file than you need to double all the percents (%%F
, %%~zF
)
如果你不想使用FOR循环,你可以使用一个调用参数,而不是 - 它使用相同的修饰符变量。然而,CALL是比(可能你的情况并不显著)
If you don't want to use a FOR loop, you can use a CALL parameter instead - it uses the same modifiers as FOR variables. However, CALL is slower than FOR (probably not significant in your case)
@echo off
call :deleteIfEmpty "yourFileName"
exit /b
:deleteIfEmpty
if %~z1 eq 0 del %1
exit /b
修改
我想过的另一种方式。 的FINDSTR搜索字符串^
将匹配文件的所有行。但是一个空文件没有任何行。所以,简单地删除该文件,如果FINDSTR不匹配行。
I've thought of yet another way. A FINDSTR search string of "^"
will match all lines of a file. But an empty file doesn't have any lines. So simply delete the file if FINDSTR fails to match a line.
>nul findstr "^" "yourFileName" || del "yourFileName"
这篇关于如何检测(和删除)的文件,如果使用的是DOS批处理文件是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!