I’d like to put each of the many properties’ file names into variable fileName and echo them out to the command prompt window. But only the last properties file name to be cycled thru is printed out as many times as there are properties files. Is there an easy fix to this problem. I know that …DO echo %%-nxG can do the same thing but I’d like to save the file name in %%~nxG for future use.
FOR %%G IN (C:\ExecutionSDKTest_10.2.2\*.properties) DO (
set fileName=%%~nxG
echo %fileName%
)
You need to use delayed expansion:
Environment variables in
cmdare expanded when a command is parsed – in this case this includes the whole block in parentheses. So%fileName%gets replaced by an empty string because it didn’t have a value before the loop ran. Delayed expansion uses!instead of%and changes variable evaluation so that they are evaluated just before a command is run.help sethas more details about why and when it is necessary. In general, whenever you modify and use a variable within a loop you have to use delayed expansion, but it comes with a few other benefits too.