Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

TBINewest Not Calculating Correctlly in CMDebug

Jan
179
2
This is an excerpt from the log created by the script when run outside of CMDebug. The important lines are bolded.

CallingName=P:\Backup\BackupScripts\IFWBackupRotate.cmd, MessageText="1st TBICntFull=24"
06/12/2026 05:02 AM P:\Backup\BackupScripts\IFWBackupRotate.cmd 31 "1st TBICntFull=24"
CallingName=P:\Backup\BackupScripts\IFWBackupRotate.cmd, MessageText="TBINewest=HPEnvy-E-2026-07-09-1537__FULL.TBI"
06/12/2026 05:02 AM P:\Backup\BackupScripts\IFWBackupRotate.cmd 32 "TBINewest=HPEnvy-E-2026-07-09-1537__FULL.TBI"

CallingName=P:\Backup\BackupScripts\IFWBackupRotate.cmd, MessageText="TBIOldest=HPEnvy-E-2026-04-13-0502__FULL.TBI"
06/12/2026 05:02 AM P:\Backup\BackupScripts\IFWBackupRotate.cmd 33 "TBIOldest=HPEnvy-E-2026-04-13-0502__FULL.TBI"
CallingName=P:\Backup\BackupScripts\IFWBackupRotate.cmd, MessageText="TBINewest=HPEnvy-E-2026-07-09-1537 s/b everything but __FULL.TBI"
06/12/2026 05:02 AM P:\Backup\BackupScripts\IFWBackupRotate.cmd 34 "TBINewest=HPEnvy-E-2026-07-09-1537 s/b everything but __FULL.TBI"

CallingName=P:\Backup\BackupScripts\IFWBackupRotate.cmd, MessageText="Differential images found in newest Full set: 0 of 0"
06/12/2026 05:02 AM P:\Backup\BackupScripts\IFWBackupRotate.cmd 35 "Differential images found in newest Full set: 0 of 0"
CallingName=P:\Backup\BackupScripts\IFWBackupRotate.cmd, MessageText="Number of Full images will exceed 21. Delete HPEnvy-E-2026-04-13-0502__FULL.TBI."

The value displayed in those lines by is generated by these statements:

set /a Number1=0
if exist "%TBIBase%\%TBIName%*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%*__FULL.tbi" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)

When calculated in CMDebug, it set's the value of TBINewest to this:

Screenshot_20260612_222152_RVNC Viewer.webp
 
I'm not even going to try to untangle that Byzantine horror, but I can't help noticing the self-contradictory DIR options: /P /-P /W /-W. What were they thinking?
 
I don't think this is a debugger issue. I think you need to add another set of parentheses to enclose all three commands you want executed as part of the For/Do loop.

Otherwise only the first command is executed on each iteration of the For/Do loop. And when your last command is executed the For variable (%a) no longer exists.

So try changing this line:

Code:
if exist "%TBIBase%\%TBIName%*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%*__FULL.tbi" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)

to this

Code:
if exist "%TBIBase%\%TBIName%*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%*__FULL.tbi" /o-d /b /p /-p /w /-w') do ((set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a))
 
I didn't write it. I just know it's not getting the correct result in the debugger. I noted the same thing to the orginal writer many years ago.
 
I'm not even going to try to untangle that Byzantine horror, but I can't help noticing the self-contradictory DIR options: /P /-P /W /-W. What were they thinking?
I asked about the Dir options before. Because dir can have a preset format setup the same option with the hyphen ensures that they are overridden if set. /-p ensures no pause will occur but it's only effective with the /p otherwise.
 
I don't think this is a debugger issue. I think you need to add another set of parentheses to enclose all three commands you want executed as part of the For/Do loop.

Otherwise only the first command is executed on each iteration of the For/Do loop. And when your last command is executed the For variable (%a) no longer exists.

So try changing this line:

Code:
if exist "%TBIBase%\%TBIName%*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%*__FULL.tbi" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)

to this

Code:
if exist "%TBIBase%\%TBIName%*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%*__FULL.tbi" /o-d /b /p /-p /w /-w') do ((set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a))
It has to be the debugger because it works fine stand-alone.
 
The syntax you are using isn't parsed the same way for CMD prompts and TCC/CMDebug. If you add the extra parentheses which I suggested, then it will work for both CMD and CMDebug


Here's a batch file with a simplified version of the line in question. Without the added parentheses you can see that TCC parses and executes the line differently from CMD.

Code:
@echo off
setlocal EnableDelayedExpansion
set Number1=
set TBINewest=
set TBIOldest=

set Number1=0
set TBINewest=X

REM ----- Original Line
REM if exist "%TBIBase%\%TBIName%*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%*__FULL.tbi" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)

REM Simplified version of Original line
REM for /f "delims=" %%a in ('dir "C:\batfile\*.tws" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)

    REM ---- With extra parens
    @echo on
    for /f "delims=" %%a in ('dir "C:\batfile\*.tws" /o-d /b /p /-p /w /-w') do ((set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a))
    @echo off

@echo off
echo.
echo --- Number1
set Number1
echo --- TBI Newest
set TBINewest
echo --- TBI Oldest
set TBIOldest
 
Last edited:
The syntax you are using isn't parsed the same way for CMD prompts and TCC/CMDebug. If you add the extra parentheses which I suggested, then it will work for both CMD and CMDebug


Here's a batch file with a simplified version of the line in question. Without the added parentheses you can see that TCC parses and executes the line differently from CMD.

Code:
@echo off
setlocal EnableDelayedExpansion
set Number1=
set TBINewest=
set TBIOldest=

set Number1=0
set TBINewest=X

REM ----- Original Line
REM if exist "%TBIBase%\%TBIName%*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%*__FULL.tbi" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)

REM Simplified version of Original line
REM for /f "delims=" %%a in ('dir "C:\batfile\*.tws" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)

    REM ---- With extra parens
    @echo on
    for /f "delims=" %%a in ('dir "C:\batfile\*.tws" /o-d /b /p /-p /w /-w') do ((set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a))
    @echo off

@echo off
echo.
echo --- Number1
set Number1
echo --- TBI Newest
set TBINewest
echo --- TBI Oldest
set TBIOldest
I get what you're saying but it's not my code. If I change it then I have to keep changing it to get it to run in CMDebug and the likelyhood of that I would say is probaby unikely but I still don't know what happened that caused this whole automated backup to just stop running so it's not impossible. I will run a test on it to validate it works but realize this is like 1 out of 3 lines that are similar. The goal is to ID the oldest file and the newest file in the list and how many backup sets there are then delete as many of the oldest to get back to the maximum backups.
 
I verified that adding the extra parens to the statement still seems to work properly running in batch. I'm reluctant to change code coming from other entities. I will contact them and show them the code correction. I'm going to leave it in place until I find what's preventing the code from executing. Like I mentioned before there is similar code used several times throughout the script that would only make sense to update as well.

if exist "%TBIBackupFindFull%" (
for /f "delims=" %%a in ('dir "%TBIBackupFindFull%" /o-d /b /p /-p /w /-w') do (
set "TBIOldest=%%a"
set /a Number1+=1
if "!TBINewest!"=="X" set TBINewest=%%a"
)
)

For some reason the code loses the tabs when I save it.
 
Back
Top