views:

1375

answers:

2

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.

If there is a way to do this in powershell, that would work too.

A: 

Found this:

@echo off
if not "%~1"=="" echo Modified date of %~1 is %~t1

on Experts exchange. Maybe a good point to start from.

KB22
Oooh, linking to the evil hyphen site. Now that's probably frowned upon here :-)
Joey
was'nt aware of that. trying to downvote myself for this... ;)
KB22
Well, since Stack Overflow set out to destroy the evil hyphen site, I just figured so :-). No need to downvote, unless others think it's unhelpful :-)
Joey
+3  A: 

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.

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:

$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
}
Joey