2021年2月3日星期三

How to get current date inside a FOR loop

I have the following code sample in C:\myscript.bat

for /f %%i in ('dir "C:\files" /b /a-d /o-d') do (  if /i "%%~xi"==".vib" (  gsutil cp c:\files\%%i gs://mybucket  call :GetDate  echo %dt% File %%i was uploaded )  )  :done    :GetDate  set X=  for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do if not defined X set X=%%x  set DATE.YEAR=%X:~0,4%&set DATE.MONTH=%X:~4,2%&set DATE.DAY=%X:~6,2%&set DATE.HOUR=%X:~8,2%&set DATE.MINUTE=%X:~10,2%&set DATE.SECOND=%X:~12,2%  set dt=%DATE.MONTH%-%DATE.DAY%-%DATE.YEAR%_%DATE.HOUR%-%DATE.MINUTE%-%DATE.SECOND%  exit /b  

My problem is getting the current date in 'echo %dt% File %%i was uploaded' when calling GetDT inside the FOR loop. As long as I call :GetDate outside the FOR loop it works fine. I've already spent probably 4+ hrs and can't seem to get it to print the 'current' date inside the FOR loop without some kind of error.

I also tried just throwing the entire date code from :GetDate inside the FOR loop but that doesn't work either. I guess it doesn't like the FOR loop inside a FOR loop. Your help would be appreciated.

https://stackoverflow.com/questions/66038389/how-to-get-current-date-inside-a-for-loop February 04, 2021 at 10:08AM

没有评论:

发表评论