Variable in FOR wont work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mnicolas
Posts: 1
Joined: 09 May 2019 08:55

Variable in FOR wont work

#1 Post by mnicolas » 09 May 2019 09:04

Hi! I have the following Script:

Code: Select all

@echo off

Set "QuellRoot=P:\...\Dokumente"
Set "ZielRoot=P:\...\Datenraum"

@FOR %%i IN ("%QuellRoot%\*.*") do @(

	echo %%~nxi
	echo %%i
	start "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe" "%%i"


	echo.
	echo Abbrechen mit Strg-C 
	Set /P "Input=Please type anything and press Enter: "
	Set Input
	echo %Input%
	
	echo kill %%~nxi
	taskkill /F /FI "WINDOWTITLE eq %%~nxi - Adobe Reader"
	pause
)
pause
Everything works except the "echo %Input%".
I get following Output:
SKMBT_C284e19050813042.pdf
P:\...\Dokumente\SKMBT_C284e19050813042.pdf

Abbrechen mit Strg-C
Please type anything and press Enter: abc
Input=abc
ECHO ist ausgeschaltet (OFF).
kill SKMBT_C284e19050813042.pdf
ERFOLGREICH: Der Prozess mit PID 12484 wurde beendet.
Drücken Sie eine beliebige Taste . . .
I marked the wrong Output bold.
I dont know, why it wont work! The SET command is working. Proof: The Line "Input=abc" (Marked with unterline), but if I want to use the Variable, it gives back "".
Does anyone know Why?

Win 7 Professional SP1 64bit

aGerman
Expert
Posts: 3629
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Variable in FOR wont work

#2 Post by aGerman » 09 May 2019 10:07

Variables are expanded before a command line or a block of command lines (enclosed in parentheses) is executed. Thus, you can't output variables of variables that are defined or changed inside of the block in the default way. You need to enable delayed expansion and use exclamation points instead of percent signs.

Code: Select all

...
	setlocal EnableDelayedExpansion
	echo !Input!
	endlocal
...
Steffen

Post Reply