问题
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