Batch file 使用批处理脚本检查文件大小

Batch file 使用批处理脚本检查文件大小,batch-file,Batch File,我正在尝试使用批处理文件“run.bat”检查文件的大小。批处理脚本是- setlocal set file="C:\TestWorks\Project_Testing\new.template" set maxbytesize=2000 FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA if %size% LSS %maxbytesize% ( echo File is ^< %maxbytesize% bytes ) els

我正在尝试使用批处理文件“run.bat”检查文件的大小。批处理脚本是-

setlocal
set file="C:\TestWorks\Project_Testing\new.template"
set maxbytesize=2000

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

if %size% LSS %maxbytesize% (
echo File is ^< %maxbytesize% bytes
) else (
echo File is ^>= %maxbytesize% bytes

这里出了什么问题。

虽然大多数代码都能正常工作,但有些小问题在其他情况下可能会导致更大的问题

@echo off
    setlocal enableextensions

    set "file=C:\TestWorks\Project_Testing\new.template"
    if not exist "%file%" goto :eof

    set "maxbytesize=2000"

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

    if %size% LSS %maxbytesize% (
        echo File is ^< %maxbytesize% bytes
    ) else (
        echo File is ^>= %maxbytesize% bytes
    )
@echo关闭
setLocalEnableExtensions
设置“file=C:\TestWorks\Project\u Testing\new.template”
如果不存在“%file%”转到:eof
设置“maxbytesize=2000”
对于(“%file%”)中的%%A,请设置“大小=%%~zA”
如果%size%LSS%maxbytesize%(
回显文件为^<%maxbytesize%字节
)否则(
回显文件为^>=%maxbytesize%字节
)
变化:

  • 正确引用。虽然这不是失败的原因,但最好正确引用数据

  • /f的
    用于处理数据行。要直接获取对文件的引用,应使用
    的简单
    (不是,仍然不是失败的原因)

  • 代码故障的原因是
    else
    子句中缺少括号(请参阅)

    • 嘿,嘿,嘿

      您缺少
      if
      语句的右括号


      在最后的
      回音
      之后,在所有行上附加一个
      可能批处理神会对你微笑。

      这里只是评论说,由于批处理限制,
      文件大小超过
      2gb
      无法进行比较。
      @echo off
          setlocal enableextensions
      
          set "file=C:\TestWorks\Project_Testing\new.template"
          if not exist "%file%" goto :eof
      
          set "maxbytesize=2000"
      
          FOR %%A IN ("%file%") DO set "size=%%~zA"
      
          if %size% LSS %maxbytesize% (
              echo File is ^< %maxbytesize% bytes
          ) else (
              echo File is ^>= %maxbytesize% bytes
          )