简体   繁体   English

批处理程序不会打开最后一个文件

[英]Batch program won't open last file

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... 它适用于第一个图像,但是当我将新图像添加到文件夹时,它不会更新...

You really use Internet Explorer as image viewer? 您真的使用Internet Explorer作为图像查看器吗?

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. 但是,您的批处理文件将等待执行,直到Internet Explorer被用户终止。 Therefore the next command below starting Internet Explorer is not executed immediately after starting IE. 因此,启动IE之后,启动Internet Explorer下方的下一个命令不会立即执行。

The ping with just 1 try is always successful within 1 millisecond as localhost is the own PC. 由于本地主机是自己的PC,只需尝试1次即可ping总是在1毫秒内成功。 So this command is completely useless as used here for a wait of 1 second which I think you wanted with this command. 因此,此命令在这里等待1秒,我认为您希望使用此命令完全没有用。

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. 而且,检查最新映像是否与IE中已经打开的映像不相同甚至更重要,否则批处理文件会一次又一次地打开相同的映像,并且用户将无法再使用Windows,直到杀死批处理为止Windows任务管理器打开文件。

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 . 除了使用命令ping来让批处理文件等待X秒(例如使用命令超时)以外,其他方法请参见在批处理文件中休眠

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. 注意: %%~ti引用文件的日期和时间,而我的计算机上没有秒,只是日期,小时和分钟。 So with this batch code renewals of the file within a minute are not recognized. 因此,使用此批处理代码无法在一分钟内更新文件。

As it stands, the script waits until you close internet explorer before it continues. 就目前而言,该脚本会一直等到您关闭Internet Explorer之后才能继续。 Which is good, because else it would open the latest image every second anew, flooding your PC with instances of internet explorer. 这样做很好,因为否则它将每秒重新打开最新图像,从而使Internet Explorer实例淹没您的PC。 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). 您无法通过这种方式自动更新显示的图像,但是如果您可以通过关闭浏览器进行手动更新,那么就可以了(在这种情况下,您甚至可以删除ping行)。

For loop: should work as follows For循环:应如下工作

@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 当看到良好的输出(适合您)时取消/取消所需的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM