批处理脚本检查文件大小的方法呢? [英] Batch script checking file size methods?

查看:125
本文介绍了批处理脚本检查文件大小的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的剧本之一以下code,但它似乎并不可靠。看来,如果该文件实际上是小于1它可能会失败。或者至少是没有的人。难道我做错了什么或有更好的方法来做到这一点?

I have the following code in one of my scripts, but it doesn't seem to be reliable. It seems that if the file is actually less than 1 it might fail. Or at least it did for someone. Am I doing something wrong or is there a better way to do this ?

在短,检查file.txt的> 0

In short, check that file.txt is > 0 :

for /f %%i in ("file.txt") do set size=%%~zi
    rem added THIS:  if not defined size set size=0
if %size% gtr 0 (
    type file.txt >> %FNAME%
) else (
    echo     No Data Found On System >>  %FNAME%
)

编辑:问题是与code我改变上游创建。基本上说得那么file.txt的可能是从来没有建立。我改名file.txt的东西像code复制问题foobar.txt。把如果没有定义大小设置大小= 0似乎解决了这个问题。

Issue was created with code I changed upstream. Basically made it so file.txt potentially was never created. I renamed file.txt to something like foobar.txt in the code to replicate the issue. Putting "if not defined size set size=0" seemed to resolve this problem.

推荐答案

我能想到的唯一的事情就是该文件必须不存在,在这种情况下, %%〜滋什么也不扩展(而不是0)。这很容易被后来如果没有定义的大小尺寸明确设置为0的固定。

The only thing I can think of is the file must not exist, in which case %%~zi expands to nothing (not 0). That is easily fixed by explicitly setting size to 0 afterward if size is not defined.

for /f %%i in ("file.txt") do set "size=%%~zi"
if not defined size set size=0
if %size% gtr 0 (
    type file.txt >> %FNAME%
) else (
    echo     No Data Found On System >>  %FNAME%
)

这篇关于批处理脚本检查文件大小的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆