如果文件存在且小于大小

时间:2016-11-16 12:42:51

标签: batch-file cmd

我有以下代码。我要做的是在继续之前查看文件是否存在以及是否大于某个大小:

:: Create file dir variables
set logfile=nightly_maint.log
set logbackup=c:\sbbs\logBackups\
set announcefile=announce.txt
set minbytesize=100

:: Create date time string
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET dirname=%mm%-%dd%-%yyyy%

cd c:\sbbs\
move %logfile% %logbackup%%logfile%.%dirname%.bak

echo Nightly Maintenance running for Split Inifnity BBS on %dirname%. >> %logfile% 
echo Checking for daily received files list. >> %logfile% 
:: Create files received announcement
cd c:\tinytic
if exist %announcefile% (
    for /F "usebackq" %%A in ('%announcefile%') do set size=%%~zA
      if %size% gtr %minbytesize% (
        echo New files received list found, proceeding with announcement generation. >> %logfile% 
        :: Add a text header and footer to my announcement
        echo Creating message file for posting. >> %logfile% 
        copy header.txt+announce.txt+footer.txt final.txt
        echo Creating new files announcement for FidoNet Allfix echo. >> %logfile%
        c:\sbbs\exec\smbutil ic:\tinytic\final.txt c:\sbbs\data\subs\fidoallfixfi.shd < c:\tinytic\fidoreq.hdr
        echo Creating new files announcement for Fidonet File Announce. >> %logfile%
        c:\sbbs\exec\smbutil ic:\tinytic\final.txt c:\sbbs\data\subs\fidofdnannou.shd < c:\tinytic\fidoreq.hdr
        echo Creating new files announcement for Fidonet File Announce. >> %logfile%
        c:\sbbs\exec\smbutil ic:\tinytic\final.txt c:\sbbs\data\subs\fido-req.shd < c:\tinytic\fidoreq.hdr
        echo Completed posting new files messages. >> %logfile%
        echo Running ADDFILES for extended descriptions in file base. >> %logfile%
        del announce.txt
        del final.txt
        cd c:\sbbs\
        c:\sbbs\exec\ADDFILES * /S /N /Z
        echo Completed ALLFILES scan. >> %logfile%
        echo Creating FREQ files list. >> %logfile%
        c:\sbbs\exec\FILELIST *  /ALL /ULD /EXT c:\taurus\files.txt
      ) else (
        echo Announce.txt was zero bytes in size, bypassing new files received posting. >> %logfile%
      )
) else (
    echo Announce.txt file not found, bypassing new files received posting. >> %logfile% 
)

当运行批处理文件时,目录中有一个零字节文件,我得到了这个:

c:\sbbs>move nightly_maint.log c:\sbbs\logBackups\nightly_maint.log.11-16-2016.bak
        1 file(s) moved.

c:\sbbs>echo Nightly Maintenance running for Split Inifnity BBS on 11-16-2016.   1>>nightly_maint.log

c:\sbbs>echo Checking for daily received files list.   1>>nightly_maint.log

c:\sbbs>cd c:\tinytic
100 was unexpected at this time.

c:\tinytic>       if  gtr 100 (

c:\sbbs>

1 个答案:

答案 0 :(得分:0)

解决方案是延迟扩展,因为您正在更改并在同一代码块中读取相同的变量:if%size%gtr%minbytesize% - &gt;如果!大小! gtr%minbytesize%和setlocal EnableDelayedExpansion在您的脚本之上..