Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch reinitialize global variable inside loop and use it outside loop

firts of all im beginner. i have multiple file in directory which its name contain date, i need to find which file is the latest one(just need the date). this is what i have so far

set currentYear=2013
set logDate=0
set tempLogDate=0
set fileName2=0
setlocal enabledelayedexpansion

for /f "delims=" %%a in ('dir /a-d/b/s "%logDirectory2%"^|findstr /riv "^.*\\[^\\]*%now%[^\\]*$"') do (
set fileName2=%%a
call set fileName2=!fileName2!:!currentYear!=%%
set tempLogDate=%currentYear%!fileName2:~0,4!
if !tempLogDate! GTR %logDate% ( set logDate=%tempLogDate% )
)

echo !logDate!

the problem is variable inside loop didnt get change, thanks in advance

*i change some code but still didnt solve... :( what a horrible weeks start

like image 602
paiseha Avatar asked May 15 '26 07:05

paiseha


1 Answers

In order to access the valeue of a variable as it is changed within a loop, you need to have enabledelayedexpansion invoked (which you have) AND you then need to use !varname! to access the changing value; `%varname% accesses the PARSE-TIME value, that is, the inital variable value BEFORE the loop was STARTED

like image 150
Magoo Avatar answered May 17 '26 15:05

Magoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!