I see no need for delayed expansion whatsoever.
Just create your DateTime string %dt%, before the Dir loop: The time of the script will of course be the same for each, but IMO the exact time of each individual gsutil command ending shouldn't be mission critical, the batch run time should be.
@Echo Off
SetLocal EnableExtensions
Set "sd=C:\files"
Set "fg=*.vib"
For /F EOL^=L %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS GET LocalDateTime'
) Do For %%H In (%%~nG) Do Set "dt=%%H"
Set "dt=%dt:~4,2%-%dt:~6,2%-%dt:~,4%_%dt:~8,2%-%dt:~10,2%-%dt:~-2%"
For /F "EOL=? Delims=" %%G In ('Dir "%sd%\%fg%" /B /A:-D /O-D 2^> NUL') Do (
gsutil.exe cp "%sd%\%%G" "gs://mybucket"
Echo %dt% file %%G was uploaded)
Pause
GoTo :EOF
If you really do need to record the time just after each file was processed individually, just do it directly in the same for loop:
@Echo Off
SetLocal EnableExtensions
Set "sd=C:\files"
Set "fg=*.vib"
For /F %%G In ('Dir "%sd%\%fg%" /B /A:-D /O-D 2^> NUL') Do (
gsutil.exe cp "%sd%\%%G" "gs://mybucket" && (
For /F "Tokens=1-6 Delims=/: " %%H In (
'%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^
^| %SystemRoot%\System32\find.exe " 123"') Do (
Echo %%I-%%J-%%H_%%K-%%L-%%M file %%G was uploaded)))
Pause
GoTo :EOF
[Edit /]
Just in case your gsutil does not carry an .exe extension and with an expectaion that it returns an error level of 0 for a successful cp here's a slight modification of the same code.
@Echo Off
SetLocal EnableExtensions
Set "sd=C:\files"
Set "fg=*.vib"
For /F %%G In ('Dir "%sd%\%fg%" /B /A:-D /O-D 2^> NUL') Do (
gsutil cp "%sd%\%%G" "gs://mybucket"
If Not ErrorLevel 1 For /F "Tokens=1-6 Delims=/: " %%H In (
'%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^
^| %SystemRoot%\System32\find.exe " 123"') Do (
Echo %%I-%%J-%%H_%%K-%%L-%%M file %%G was uploaded))
Pause
GoTo :EOF
As a courtesy only, (I see no reason why you cannot use Robocopy.exe), here's a version which uses cscript.exe for generating your DateTime stamp via JScript instead.
@if (@X) == (@Y) @end /*
@Echo Off
SetLocal EnableExtensions
Set "sd=C:\files"
Set "fg=*.vib"
For /F %%G In ('Dir "%sd%\%fg%" /B /A:-D /O-D 2^> NUL') Do (
gsutil cp "%sd%\%%G" "gs://mybucket"
If Not ErrorLevel 1 For /F %%H In (
'%SystemRoot%\System32\cscript.exe /NoLogo /E:JScript "%~f0"') Do (
Echo %%H file %%G was uploaded))
Pause
GoTo :EOF */
var d = new Date();
WScript.Echo (("0" + (d.getMonth() + 1)).slice(-2) + "-" +
("0" + d.getDate()).slice(-2) + "-" + d.getFullYear() + "_" +
("0" + d.getHours()).slice(-2) + "-" +
("0" + d.getMinutes()).slice(-2) + "-" + ("0" + d.getSeconds()).slice(-2));
[Edit2 /] Based upon new question criteria, in the comments below
@Echo Off
SetLocal EnableExtensions
Set "sd=C:\files"
Set "fg=*.vib *.vbk"
PushD "%sd%" 2> NUL || GoTo :EOF
For /F %%G In ('Dir %fg% /B /A:-D /O-D 2^> NUL') Do (
If /I "%%~xG" == ".vbk" GoTo Done
gsutil cp "%%G" "gs://mybucket"
If Not ErrorLevel 1 For /F "Tokens=1-6 Delims=/: " %%H In (
'%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^
^| %SystemRoot%\System32\find.exe " 123"') Do (
Echo %%I-%%J-%%H_%%K-%%L-%%M file %%G was uploaded))
:Done
PopD
Pause
GoTo :EOF