Checking file size in a batch script

倾然丶 夕夏残阳落幕 提交于 2019-12-11 06:38:38

问题


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:

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>

How do I fix this error?

Edit:

New error:


回答1:


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:

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



回答2:


Within a 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



回答3:


@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
)


来源:https://stackoverflow.com/questions/7881035/checking-file-size-in-a-batch-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!