Windows 批处理文件 - 检查文件是否已被修改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1520643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows batch file - check if file has been modified
提问by DataStream3
I'm configuring a windows machine to continuously run a powerpoint presentation. The ppt file is located on a samba share (windows file sharing) and will be updated periodically. I need a way to make a batch file that will restart the ppt slide show if the file has been changed. The batch script will run on a a regular interval, and will hopefully check to see if the file has been updated, and re-start the slideshow if it has.
我正在配置 Windows 机器以连续运行 PowerPoint 演示文稿。ppt 文件位于 samba 共享(windows 文件共享)上,会定期更新。我需要一种方法来制作一个批处理文件,如果文件已更改,该文件将重新启动 ppt 幻灯片放映。批处理脚本将定期运行,并有望检查文件是否已更新,如果已更新,则重新启动幻灯片。
If there is a way to do this in powershell, that would work too.
如果有办法在 powershell 中做到这一点,那也行。
回答by Joey
Well, in a batch, the easiest way I would think of would be to periodically check whether the file's last modification date and/or its size has changed.
好吧,在批处理中,我想到的最简单的方法是定期检查文件的上次修改日期和/或其大小是否已更改。
You can get both via the following:
您可以通过以下方式获得两者:
for %%X in (myfile) do set size=%%~zX&set filetime=%%~tX
You can then cache those values and compare whether they have changed since the last iteration. Using a delay (a few seconds maybe) via
然后,您可以缓存这些值并比较它们自上次迭代以来是否发生了变化。通过使用延迟(可能几秒钟)
ping -n 11 localhost >nul 2>nul
(for 10 seconds delay) can help in not checking too often.
(延迟 10 秒)可以帮助不检查太频繁。
So, it might look a little like the following:
所以,它可能看起来有点像下面这样:
@echo off
setlocal
set FileName=MyPresentation.pptx
set FileTime=-
:loop
for %%X in (%FileName%) do (
if %FileTime% NEQ %%~tX (
rem just an example
taskkill /f powerpnt.exe
start %FileName%
)
set FileTime=%%~tX
)
rem wait 5 seconds before checking again
ping -n 6 localhost >nul 2>nul
goto :loop
In PowerShell the code wouldn't look too different, except that you get to the relevant properties a little easier:
在 PowerShell 中,代码看起来不会有太大不同,只是您可以更轻松地访问相关属性:
$FileName = "MyPresentation.pptx"
$FileTime = Get-Date
# endless loop
for () {
$file = Get-Item $FileName
if ($FileTime -ne $file.LastWriteTime) {
Get-Process powerpnt* | Stop-Process
Invoke-Item $file
}
$FileTime = $file.LastWriteTime
Start-Sleep 5
}
回答by DataStream3
Try the code below. It checks for the current time and checks when the file was last modified. If the two values are the same it is assumed the file was altered.
试试下面的代码。它检查当前时间并检查文件上次修改的时间。如果两个值相同,则假定文件已更改。
@echo off &setlocal
TITLE File Monitor
Set file=Test.txt
:CheckforAlter
Set modif_time=
Set allm=
cls
for /f "tokens=1-3 delims=:,. " %%A in ("%time%") do (
set "Hour=%%A"
set "Min=%%B"
set "Sec=%%C"
)
set /a Hour = Hour %% 12
if %Hour%==0 set "Hour=12"
set "Allm=%Hour%:%Min%:%Sec%"
for /f %%i in ('"forfiles /m %file% /c "cmd /c echo @ftime" "') do set modif_time=%%i
echo.
echo.
IF %modif_time%==%Allm% (
echo File was altered.
start "" "%file%"
Timeout /t 1 >nul
)
echo file wasn't modified.
GOTO CheckforAlter
回答by KB22
Found this:
发现这个:
@echo off
if not "%~1"=="" echo Modified date of %~1 is %~t1
on Experts exchange. Maybe a good point to start from.
关于专家交流。也许是一个很好的起点。