helldoc

Batch for loop und Variablen

Ich verstehe die Variablen in Batch nicht wirklich, da sie manchmal einfach nicht gesetzt werden und leer bleiben z.B. die Variable text hier:
@echo off
set count=0;
for /f "tokens=*" %%i in (path.txt) do (  
	echo %%i
    	set text[!count!]=%%i
	echo %text[!count!]%
	set /a count+=1
)
echo %text%
echo %text[1]%
pause
Auf Facebook teilen
Auf X (Twitter) teilen
Auf Reddit teilen
Auf Linkedin teilen

Content-ID: 3237052781

Url: https://administrator.de/forum/batch-for-loop-und-variablen-3237052781.html

Ausgedruckt am: 11.05.2025 um 11:05 Uhr

jjflash
Lösung jjflash 02.07.2022 um 14:27:40 Uhr
Goto Top
Hallo.
"setlocal ENABLEDELAYEDEXPANSION"
Ist dein Freund.
Ich spare mir die Bemerkung über die Forensuche.

Gruß Jjfl.
TwistedAir
TwistedAir 02.07.2022 um 14:40:34 Uhr
Goto Top
Hallo helldoc,

dieses Tutorial könnte dir bei deiner Frage helfen: Tutorial zur FOR-Schleife

Grüße
TA
ricardobohner
ricardobohner 02.07.2022 um 15:53:59 Uhr
Goto Top
Dieser Strichpunkt hat doch hier nichts verloren:
set count=0;

Du kannst Variablen die zwichen Prozentzeichen sind nicht aktualiziert vom inneren vom for abrufen. Da du text[#] im for krierst hast und es dann gleich abrufst weiss das Batch Program nicht was "%text[!count!]%" ist da nur die Variable !count! aktualiert ist aber nicht %text[!count!]%. Dieses Kommando:

SetLocal EnableDelayedExpansion

Bewierkt das Variablen in Echtzeit aktualisiert werden aber nur die, die zwischen Ausrufezeichen sind ex: (!count!)

Hier wäre ein beispiel wie du es machen könntest:

@echo off
setlocal EnableDelayedExpansion
set count=0
for /f "tokens=*" %%i in (path.txt) do (  
                                        set /a count+=1
                                        set text[!count!]=%%i                                        
                                       )

for /L %%a in (1,1,%count%) do echo !text[%%a]!

pause
https://i.imgur.com/Ov5VFJJ.gif
helldoc
helldoc 02.07.2022 um 19:04:36 Uhr
Goto Top
ich komme von c++ passiert manchmal automatisch mit dem ";" aber habe nie gemerkt, dass es irgenwas verändert.
Also danke für deine Hilfe face-smile (btw ich will nicht respektlos klingen aber ganz bisschen auf die Rechtschreibung achten könnte man schon)