X
X
xsash2019-12-27 16:27:06
cmd/bat
xsash, 2019-12-27 16:27:06

Why is the counter reset in the bat script?

There is a simple script, as a payload in the example of creating a folder with a pass number.
If there is a ping and there is no goto (or call) transition, everything is fine. The variable "N" is incremented with each pass.

@echo off
setlocal EnableDelayedExpansion

SET ip=192.168.0.33
SET folder=testfolder

for /l %%n in (1,1,10) do (

:: Ожидаем устройство в сети
:loop
timeout /t 5 /nobreak >nul
ping -n 1 %ip% |find "TTL=" || goto :loop

echo ==============================================================
echo Start: %%n pass || %folder%
echo ==============================================================

echo Wait 5 sec
timeout /t 5 /nobreak >nul

mkdir %folder%-%%n
)

But if the ping failed and there was a transition to ":loop" - instead of a number, just a text value is displayed
Reply from 192.168.0.33: bytes=32 time<1ms TTL=64
==============================================================
Start: %n pass
==============================================================
Wait 5 sec

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wisgest, 2019-12-27
@wisgest

Command files are processed line by line unless a compound command is found that spans multiple lines. But it doesn't go beyond that: there is no precompilation, no tokenization, no syntax checking of the batch file as a whole, and no even storing its contents in memory. When moving to the next line or a command spanning multiple lines, the previous one is forgotten. There is no difference between GOTO exiting a FOR loop and jumping to a line inside it - both terminate the loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question