
148149
Mar 07, 2022, updated at 14:16:20 (UTC)
Batch: Check if for loop was successful
Hello,
I am currently trying to write a backup script in batch.
I have a folder called "backups" and a folder called "long-term-backups"
In the "backups" folder is a backup from the last 30 days.
In the "long-term-backups" folder is the backup from the 1. of each month.
The problem is that if the 1. of a month is on a weekend no backup is made and there is no backup to be moved to "long-term-backups".
So I decided to write a few lines to check if a backup from the current month already exists in the "long-term-backups" folder.
If there is none, the script should search the oldest backup in the "backups" folder from the current month.
My problem is, that I dont know how to check if my "for loop", searching the "long-term" folder was successful or not.
My code below.
%test% is an empty variable, I created to test if %%i is empty. It works if there is a backup, but if there is none I just get "File not found"
I would prefer to check if the for loop found something by using some kind of return value (like: 1 = found smth, 0 = found nothing).
I appreciate a little help.
Thank you in advance.
With kind regards
Tobias
I am currently trying to write a backup script in batch.
I have a folder called "backups" and a folder called "long-term-backups"
In the "backups" folder is a backup from the last 30 days.
In the "long-term-backups" folder is the backup from the 1. of each month.
The problem is that if the 1. of a month is on a weekend no backup is made and there is no backup to be moved to "long-term-backups".
So I decided to write a few lines to check if a backup from the current month already exists in the "long-term-backups" folder.
If there is none, the script should search the oldest backup in the "backups" folder from the current month.
My problem is, that I dont know how to check if my "for loop", searching the "long-term" folder was successful or not.
My code below.
FOR /f %%i IN ('dir /ad /o-d /b %BackupPfad%\Langzeitsicherungen\%aktuellesJahr%-%aktuellerMonat%-??') DO ( rem prüfen ob aktueller monat in langzeitsicherungen vorhanden ist
IF NOT %%i == %test% (
echo Monat %aktuellerMonat% ist in Langzeitsicherungen
)
IF %%i == %test% (
echo Monat %aktuellerMonat% nicht in Langzeitsicherungen
)
)
%test% is an empty variable, I created to test if %%i is empty. It works if there is a backup, but if there is none I just get "File not found"
I would prefer to check if the for loop found something by using some kind of return value (like: 1 = found smth, 0 = found nothing).
I appreciate a little help.
Thank you in advance.
With kind regards
Tobias
Please also mark the comments that contributed to the solution of the article
Content-ID: 2084496185
Url: https://rootdb.com/en/batch-check-if-for-loop-was-successful-2084496185.html
Printed on: February 23, 2025 at 07:02 o'clock
1 Comment

I would prefer to check if the for loop found something by using some kind of return value (like: 1 = found smth, 0 = found nothing).
@echo off
set "found="
FOR /f %%i IN ('dir /ad /o-d /b %BackupPfad%\Langzeitsicherungen\%aktuellesJahr%-%aktuellerMonat%-?? 2^>nul') DO set found=1
if defined found (
echo found
) else (
echo not found
)