You really use Internet Explorer as image viewer?
That is not a good idea in my point of view.
However, your batch file waits with execution until Internet Explorer is terminated by the user. Therefore the next command below starting Internet Explorer is not executed immediately after starting IE.
The ping with just 1 try is always successful within 1 millisecond as localhost is the own PC. So this command is completely useless as used here for a wait of 1 second which I think you wanted with this command.
A working batch code would be:
@echo off
set "PreviousImage=none"
:loop
set "NewestImage="
echo Looking for new image.
for /f %%i in ('dir /b /a-d /o-d /t:c *.bmp *.gif *.jpg *.jpeg *.png 2^>nul') do (
set "NewestImage=%%~fi"
goto CheckImage
)
:CheckImage
if not "%NewestImage%"=="" (
if not "%PreviousImage%"=="%NewestImage%" (
echo Siste filen lagd er %NewestImage%
start "Display Image" "C:\Program Files\Internet Explorer\Iexplore.exe" -k "%NewestImage%"
set "PreviousImage=%NewestImage%"
)
)
rem Wait 5 seconds (5000 milliseconds).
%SystemRoot%\System32\ping.exe 1.1.1.0 -n 1 -w 5000 >nul
goto loop
It is important to check if an image file is stored in current directory at all.
And it is even more important to check if the newest image is not the same as opened already in IE before as otherwise the batch file opens the same image again and again and again and the user will not be able to use Windows anymore until killing batch file with Windows task manager.
See Sleeping in a batch file for other methods than using command ping to let the batch file wait X seconds like using command timeout.
Please note that batch file above requests that a newer image file has a different name than the previous image file. Otherwise it would be necessary to compare the file times.
Here is a modified version working with file date/time:
@echo off
set "PreviousTime=none"
:loop
set "NewestImage="
echo Looking for new image.
for /f %%i in ('dir /b /a-d /o-d /t:c *.bmp *.gif *.jpg *.jpeg *.png 2^>nul') do (
set "NewestImage=%%~fi"
set "NewestTime=%%~ti"
goto CheckImage
)
:CheckImage
if "%NewestImage%"=="" goto WaitSeconds
if "%PreviousTime%"=="%NewestTime%" goto WaitSeconds
echo Siste filen lagd er %NewestImage%
start "Display Image" "C:\Program Files\Internet Explorer\Iexplore.exe" -k "%NewestImage%"
set "PreviousTime=%NewestTime%"
:WaitSeconds
rem Wait 5 seconds (5000 milliseconds).
%SystemRoot%\System32\ping.exe 1.1.1.0 -n 1 -w 5000 >nul
goto loop
Attention: %%~ti
references the file date and time without seconds on my computer, just date, hour and minute. So with this batch code renewals of the file within a minute are not recognized.