1

I have a script that downloads files from an s3 bucket and I want to log the time it takes. So I have a for loop that goes through each s3 file and then if the size is greater that a set value I download the file. From within the if statement I echo out a line that includes the name of the file and the time. The problem is the time value does not change. I have created a very simple example that shows the issue.

@echo off

setlocal enabledelayedexpansion

set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%

set SUBFILENAME=%CUR_YYYY%%CUR_MM%%CUR_DD%

cd C:\Users\Public\code
echo %time% %date%> Outside_IF_%SUBFILENAME%.log
timeout /t 10
echo %time% %date%>> Outside_IF_%SUBFILENAME%.log
timeout /t 10
echo %time% %date% - Welcome Here>> Outside_IF_%SUBFILENAME%.log
timeout /t 10
echo %time% %date% - Welcome Here Again>> Outside_IF_%SUBFILENAME%.log
if not exist "1.txt" (
   echo %time% %date%> Inside_IF_%SUBFILENAME%.log
   timeout /t 10
   echo %time% %date%>> Inside_IF_%SUBFILENAME%.log
   timeout /t 10
   echo %time% %date% - Welcome Here>> Inside_IF_%SUBFILENAME%.log
   timeout /t 10
   echo %time% %date% - Welcome Here Again>> Inside_IF_%SUBFILENAME%.log
)

So how do I get the Outside_IF to work in Inside_IF

Any help will be greatly appreciated

1 Answers1

2

The issue is that when your parenthesised block of code is initially parsed, any variables are expanded then, not when the lines of code within it are actually run. (That is why you should see each '%TIME%' value as exactly the same). As you've already enabled delayed expansion, you need to use it in order for those values to be evaluated when the lines are run instead.

Example:

@Echo Off
SetLocal EnableDelayedExpansion

For /F "Tokens=1-3 Delims=/ " %%G In (
    '%SystemRoot%\System32\Robocopy.exe \: . /NJH /L ^
     ^| %SystemRoot%\System32\find.exe " 123"'
) Do Set "SUBFILENAME=%%G%%H%%I"

CD /D "%PUBLIC%\code" 2> NUL || GoTo :EOF

1> "Outside_IF_%SUBFILENAME%.log" Echo %TIME% %DATE%
"%SystemRoot%\System32\timeout.exe" /T 10
1>> "Outside_IF_%SUBFILENAME%.log" Echo %TIME% %DATE%
"%SystemRoot%\System32\timeout.exe" /T 10
1>> "Outside_IF_%SUBFILENAME%.log" Echo %TIME% %DATE% - Welcome Here
"%SystemRoot%\System32\timeout.exe" /T 10
1>> "Outside_IF_%SUBFILENAME%.log" Echo %TIME% %DATE% - Welcome Here Again

If Not Exist "1.txt" (
    1> "Inside_IF_%SUBFILENAME%.log" Echo !TIME! !DATE!
    "%SystemRoot%\System32\timeout.exe" /T 10
    1>> "Inside_IF_%SUBFILENAME%.log" Echo !TIME! !DATE!
    "%SystemRoot%\System32\timeout.exe" /T 10
    1>> "Inside_IF_%SUBFILENAME%.log" Echo !TIME! !DATE! - Welcome Here
    "%SystemRoot%\System32\timeout.exe" /T 10
    1>> "Inside_IF_%SUBFILENAME%.log" Echo !TIME! !DATE! - Welcome Here Again
)
Compo
  • 36,585
  • 5
  • 27
  • 39