windows 在批处理脚本中检查文件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7881035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:51:39  来源:igfitidea点击:

Checking file size in a batch script

windowsbatch-filefilesize

提问by Di Zou

I am trying to find the size of a file and if it is greater than 0, I want to do some stuff. I have this code:

我试图找到一个文件的大小,如果它大于 0,我想做一些事情。我有这个代码:

set file="C:\AnalyzerCheck\loaded.txt"
set minbytesize=0
if exist %file% (
FOR /F "usebackq" %A IN ('%file%') DO set size=%~zA
if %size% GTR %minbytesize% (
    //do stuff
) else (
    //do stuff
)

However, I am getting this ouput/error when I run the script:

但是,运行脚本时出现此输出/错误:

C:\AnalyzerCheck>set file=C:\AnalyzerCheck\loaded.txt

C:\AnalyzerCheck>set minbytesize=0

file~zA was unexpected at this time.

C:\AnalyzerCheck>FOR /F "usebackq" file~zA

C:\AnalyzerCheck>

C:\AnalyzerCheck>设置文件=C:\AnalyzerCheck\loaded.txt

C:\AnalyzerCheck>set minbytesize=0

file~zA 这时候出乎意料。

C:\AnalyzerCheck>FOR /F "usebackq" 文件~zA

C:\AnalyzerCheck>

How do I fix this error?

我该如何解决这个错误?

Edit:

编辑:

New error:

新错误:

回答by Aacini

This command:

这个命令:

FOR /F "usebackq" %A IN ('%file%') DO set size=%~zA

have two errors: You must not use /F option (neither "useback" option) because you want not to read the file CONTENTS, but just process the file NAME. Also, if this command is inside a Batch file, the A replaceable parameter must have two percent signs:

有两个错误:您不能使用 /F 选项(既不是“useback”选项),因为您不想读取文件 CONTENTS,而只想处理文件 NAME。此外,如果此命令在批处理文件中,则 A 可替换参数必须有两个百分号:

FOR %%A IN (%file%) DO set size=%%~zA

回答by Mike Q

Within a scipt,

在一个 scipt 中,

This Command:

这个命令:

    for /f %%A in ("myfile.txt") do set size=%%~zA

Or This Command:

或者这个命令:

    set "filename=myfile.txt"
    for %%A in (%filename%) do echo.Size of "%%A" is %%~zA bytes

Or This Command:

或者这个命令:

    set "filename=myfile.txt"
    for /f %%A in (%filename%) do set size=%%~zA

回答by rchacko

@echo off
cd C:\MyFolder\
set file="MyFile.txt"

set maxbytesize=0

FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA

if %size% GTR %maxbytesize% (
    //do stuff
) ELSE (
    //do stuff
)