0

I need to open the last image in fullscreen. And have the script listening and when a newer image is created in the dir this needs to be opened. I have this code but it does not work correctly.

Can someone help me?

:loop
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Siste filen lagd er %LAST%
"C:\Program Files\Internet Explorer\Iexplore.exe" -k "%CD%/%LAST%"
ping localhost -n 1 > nul
goto loop

It is working for the first image but when I add a new image to the folder it wont update...

Crytrus
  • 551
  • 1
  • 7
  • 14
  • 1
    It would seem to be a little pointless to post a question if the routine *was* working "correctly", so we can conclude that it isn't working "correctly." It would help if you were to tell us what it is displaying and whether that is the correct file. AT a wild guess, I'd change `"%CD%/%LAST%"` to `"%CD%\%LAST%"`since under Windows, `\` is a directory-separator and `/` a switch-indicator. – Magoo Dec 17 '14 at 09:32
  • It is working, it is displaying the image. But when a new image is in the folder it does not update. – Crytrus Dec 17 '14 at 09:45

3 Answers3

1

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.

Community
  • 1
  • 1
Mofi
  • 38,783
  • 14
  • 62
  • 115
0

As it stands, the script waits until you close internet explorer before it continues. Which is good, because else it would open the latest image every second anew, flooding your PC with instances of internet explorer. You can't auto-update the displayed image this way, but if you're ok with manual updating by closing the browser, that would work (you can even remove the ping line in this case).

ths
  • 2,508
  • 1
  • 12
  • 18
0

For loop: should work as follows

@ECHO OFF >NUL
@SETLOCAL enableextensions enabledelayedexpansion
for /f %%i in ('dir /b/a-d/od/t:c') do (
   set LAST=%%i
   echo Siste filen lagd er !LAST!
   echo "C:\Program Files\Internet Explorer\Iexplore.exe" -k "%CD%\!LAST!"
   ping localhost -n 1 > nul
   rem pause
)
@ENDLOCAL
@goto :eof

Unrem / unecho desired lines when you see good (for you) output

JosefZ
  • 22,747
  • 4
  • 38
  • 62
  • this isn't doing what the OP wants. it processes each file, instead of only the last one. – ths Dec 17 '14 at 11:58
  • Oh, my fault... Then the OP could switch to `VBScript` rather according to [How Can I Automatically Run a Script Any Time a File is Added to a Folder?](http://blogs.technet.com/b/heyscriptingguy/archive/2004/10/11/how-can-i-automatically-run-a-script-any-time-a-file-is-added-to-a-folder.aspx) – JosefZ Dec 17 '14 at 12:15