Unexpected windows batch behaviour - Variable reset after assignment - Getting file date using FOR %f in FILE and SET -
i trying compare creation date of file today's date, , have working solution:
for %%f in (%archive_file%) set filedate=%%~tf if [%filedate:~0,-6%] == [%current_date%] goto finish
for reason, when deploying batch script on windows machine other ones i've tested on (two different windows machines), filedate variable set expected result, e.g. "dd/mm/yy mm:ss", , reset nothing.
putting:
echo %filedate% pause
between 2 lines shows filedate being assigned correctly , being assigned "" straight after. cannot find answer on this, , wondered if there rare .batch masters might have idea of what's going on :/.
thanks help.
edit:
moved forfiles solution, add looking alternative:
forfiles /p %archive_dir% /m %file_name% /d 0 && ( goto finish ) || ( goto start )
this should suffice, have yet check on machine need for.
it still nice find out why above initial solution failed horribly, haha.
i suppose code different part posted.
these types of problems occours in command blocks surrounded parenthesis.
it's problem of expansion phases of batch parser, percent expansion occours before block executed.
should switch delayed expansion
setlocal enabledelayedexpansion %%f in (%archive_file%) ( set "filedate=%%~tf" echo doesn't work %filedate% echo works !filedate! if "!filedate:~0,-6!" == "%current_date%" goto finish ) :finish
Comments
Post a Comment