I have this code in my .bat file:
@echo off
setlocal enableextensions disabledelayedexpansion
//Run sqlcmd command to backup file
//error handler for sqlcmd
if %ERRORLEVEL% == 0(
FOR /F "delims=|" %%I IN ('DIR /s "D:\Temp\*.bak" /b /O:D') DO set latestFile=%%I
echo %latestFile%
) else (
echo Error occured.
)
Problem
The result that I always got from echo %latestFile% is always ECHO is off meaning, the %%I is empty. However, when I move the for loop to outside of the if-else clause and used goto to execute the for loop, this problem does not occur.
echo !latestFile!See blogs.msdn.microsoft.com/oldnewthing/20060823-00/?p=29993