Q:

Variable is empty when exporting to file

I'm trying to set a variable every time + 1 with a batch file.  So when the batch file opens it calls the file with the var and then redefines it plus 1.  But when i open the file and then open count.bat i see this(in count.bat): set backupcount= instead of 
set backupcount=1 which it should be (1 can also be 2, 3, 4, 5, enz). 
This is the code i'm using:
@echo off
if exist "backup-tool\count.bat" call "backup-tool\count.bat"

if not exist "backup-tool\count.bat" echo set backupcount=0 > "backup-tool\count.bat"

call "backup-tool\count.bat"

if "%backupcount%"=="8" (
    echo set backupcount=1 > "backup-tool\count.bat"
) else (
    set /a "backupcount=backupcount+=1"
    echo set backupcount=%backupcount% > "backup-tool\count.bat"
)

pause >nul

Anyone that knows what i'm doing wrong and tell me how i should do it? 
All help is very much appreciated! 

A:

This one needs to use setlocal ENABLEDELAYEDEXPANSION, because you work in a block of code where you manipulate that variable and you want to use it right there(not the variable that is out of the block).  This should work:
@echo off
setlocal ENABLEDELAYEDEXPANSION
if exist "backup-tool\count.bat" call "backup-tool\count.bat"

if not exist "backup-tool\count.bat" echo set backupcount=0 > "backup-tool\count.bat"

call "backup-tool\count.bat"

if "! backupcount!"=="8" (
    echo set backupcount=1 > "backup-tool\count.bat"
) else (
    set /a "backupcount=backupcount+=1"
    echo set backupcount=!backupcount!  > "backup-tool\count.bat"
)

pause >nul

