0

I have the following code. What I am trying to do is see if a file exists and if it is greater than a certain size before proceeding:

:: 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% 
)

When running the batch file, with a zero byte file in the directory, I get this:

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>
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Read about delayed expansion, vars set inside (code blocks) need special treatment, either `setlocal EnableDelayedExpansion` and use `!size!` or do a pseudo call with doubled percent signs `call if %%size%% ..` –  Nov 16 '16 at 12:52
  • 1
    The solution is [delayed expansion](http://ss64.com/nt/delayedexpansion.html) as you are changing *and* reading the same variable in the same block of code: `if %size% gtr %minbytesize%` --> `if !size! gtr %minbytesize%`, and `setlocal EnableDelayedExpansion` on top of your script... – aschipfl Nov 16 '16 at 13:03
  • 2
    @LotPings, I'm afraid `call` won't work together with `if`, because the command interpreter recognises the `if` token earlier than `call`; `if`, `for` and `rem` are somehow special to `cmd`, compared to all the other command tokens... – aschipfl Nov 16 '16 at 13:04
  • 1
    @aschipfl Thanks, I still have to read/study [Batch line parser](http://stackoverflow.com/a/4095133/1012053) –  Nov 16 '16 at 13:09
  • 3
    ISTM that `for /F "usebackq" %%A in ('%announcefile%') do if %%~zA gtr %minbytesize% (` in place of `for /F "usebackq" %%A in ('%announcefile%') do set size=%%~zAif %size% gtr %minbytesize% (` would obviate the need for `delayedexpansion` – Magoo Nov 16 '16 at 14:05

1 Answers1

0

The solution is delayed expansion as you are changing and reading the same variable in the same block of code: if %size% gtr %minbytesize% --> if !size! gtr %minbytesize%, and setlocal EnableDelayedExpansion on top of your script..