? on batch file not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

? on batch file not working

#1 Post by eroica70 » 18 Feb 2013 12:36

Hello,
I am a newbie to scripting so please forgive me if this question seems trivial. I have three computers that run a powerpoint .ppsx slide show as a sort of bulletin
board. Throughout the day there are updates to the board. What I have been doing is remoting into each machine editing the .pptx slide show and then saving it as a .ppsx and restarting it on each machine. Obviously a bit of a pain. Then I started reading up on batch files and asking questions in this forum and what I would like is a batch file that monitors a file called today.ppsx on a share folder that all three computers can access and tell if this file has been modified. If it has the batch file should copy over the today.ppsx file to a local folder, stop the presentation and then relaunch powerpoint with the updated today.ppsx file. What I have so far is this:

@echo off
rem This program checks for changes in the date modified value of the file
rem on the network folder "T:\monitor\today.ppsx" and if a change occurs
rem stops the local copy of today.ppsx from running, copies the file
rem T:\monitor\today.ppsx --> C:\today.ppsx and then restarts the local copy
rem on the machine

setlocal
set FileName=T:\monitor\today.ppsx
set Local_FName=C:\today.ppsx
set FileTime=-

:loop
for %%X in (%FileName%) do (
if %FileTime% NEQ %%~tX (
taskkill /F /IM powerpnt.exe
copy "T:\monitor\today.ppsx" "C:\today.ppsx" >nul
start %Local_FName%
)
set FileTime=%%~tX
)
rem wait 15 secs before checking again
sleep 15
goto :loop

I got most of this from another post online and adapted it to my circumstances. The program however does not seem to be staying in the loop and only runs thru once. If any has any ideas on what might be wrong or on how to accomplish this task I would be most appreciative.

Thanks

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: ? on batch file not working

#2 Post by Ocalabob » 18 Feb 2013 20:19

Greetings eroica70,
Are you sure sleep.exe is available? I had sleep.exe on WIN XP in the Resource Kit but I do not have it on WIN 7.
Then I started reading up on batch files and asking questions in this forum and what..[snip]

Which forum are you referring to?

Best wishes!

eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Re: ? on batch file not working

#3 Post by eroica70 » 18 Feb 2013 20:38

Yes you are correct. sleep it would appear has been deprecated. I have removed that from my batch file. Still get error "%~tX was unexpected at this time" after the first time thru the loop. I was looking thru stackoverflow website when I came across someone posting same problem trying to solve.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: ? on batch file not working

#4 Post by abc0502 » 18 Feb 2013 23:47

your for command is missing /F switch to work and the start command also is in-correct.
And when you check time, it's better to compare the time of the shared file with the time of the local time, not with a static value or then you will have to change it every change happen.

Try This one: Not Tested

Code: Select all

@Echo OFF
Title Checking Today.ppsx
rem This program checks for changes in the date modified value of the file
rem   on the network folder "T:\monitor\today.ppsx" and if a change occurs
rem   stops the local copy of today.ppsx from running, copies the file
rem   T:\monitor\today.ppsx --> C:\today.ppsx and then restarts the local copy
rem   on the machine

REM ====[ Settings ]==================================
SET "FileName=T:\monitor\today.ppsx"
SET "Local_FName=C:\today.ppsx"
SET "Xtime=15"

REM ====[ Check and overwrite each Xtime Sec ]=============
:Loop

rem get shared file time/date
For /F %%A In ("%FileName%") Do SET "SharedTime=%%~tA"

rem get local file time/date
For /F %%A In ("%Local_FName%") Do SET "LocalTime=%%~tA"

rem compare shared with local file
IF NOT "%LocalTime%" == "%SharedTime%" (
   Taskkill /F /IM:"powerpnt.exe" >NUL
   Copy "%FileName%" "%Local_FName%" /Y >NUL
   Start "" "%Local_FName%"
)

rem wait and loop
Ping Localhost -n %Xtime% >NUL
GOTO :Loop


Any Changes should be in the Settings section, the Xtime is the time that the batch wait till it loop again "wait time", Also i used Ping command to wait instead of the sleep, it's more general.

The batch get the shared file time/date and store it in a variable, and the same with the local file, then compare both, if the shared file has different time/date it will stop "exe" application, copy and overwrite, then start the copied file. then wait Xtime and loop again.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: ? on batch file not working

#5 Post by mfm4aa » 19 Feb 2013 00:43

The value of "%~tX" depends on your local system settings. Usually the accuracy is set to minutes, not to seconds. Imo it can't be set to seconds.

eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Re: ? on batch file not working

#6 Post by eroica70 » 19 Feb 2013 16:19

Thanks abc0502 and everbody else who responded. The program seems to be working ok except for sometimes the program cant access the file today.ppsx. I get the error messages:
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
-^CTerminate batch job (Y/N)? y

Sometimes it eventually updates and other times it doesnt and cursor will just sit thereand i have ^C out of program. When I then look at C:\today.ppsx it has not been updated to the newest verison of today.ppsx. I should mention that i am currently not at work so am testing the batch file on my laptop and just substituting C:\monitor\today.ppsx for the share on T: drive if that is making any difference I dont think so but maybe I'm wrong. I do have the taskkill command before the copy command so I dont why I'm getting this message sometimes. I also have to look into the /F option more. The one site just says its very powerful switch and refers you to a book.
One last thing, do you have a suggestion on a graceful way to get out of the loop if you need to and end the program? Thanks in advance for your time.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: ? on batch file not working

#7 Post by abc0502 » 20 Feb 2013 00:57

Try this code,

Code: Select all

@Echo OFF
Title Checking Today.ppsx
rem This program checks for changes in the date modified value of the file
rem   on the network folder "T:\monitor\today.ppsx" and if a change occurs
rem   stops the local copy of today.ppsx from running, copies the file
rem   T:\monitor\today.ppsx --> C:\today.ppsx and then restarts the local copy
rem   on the machine

REM ====[ Settings ]==================================
SET "FileName=T:\monitor\today.ppsx"
SET "Local_FName=C:\today.ppsx"
SET "Xtime=15"

SETLOCAL EnableDelayedExpansion
REM ====[ Check and overwrite each Xtime Sec ]=============
:Loop

rem get shared file time/date
For /F %%A In ("%FileName%") Do SET "SharedTime=%%~tA"

rem get local file time/date
For /F %%A In ("%Local_FName%") Do SET "LocalTime=%%~tA"

rem compare shared with local file
IF NOT "!LocalTime!" == "!SharedTime!" (
   Taskkill /F /IM:"powerpnt.exe" >NUL
   Ping Localhost -n 3 >NUL
   Copy "%FileName%" "%Local_FName%" /Y >NUL
   Start "" "%Local_FName%"
)

rem wait and loop
Ping Localhost -n %Xtime% >NUL
GOTO :Loop


we could make it check for the time, and you set a time, if that time was reached the batch exit by himself, or maybe we can make the batch loop for a specific amount of loops then exit.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: ? on batch file not working

#8 Post by Squashman » 20 Feb 2013 06:58

You know we may be able to do this with the Xcopy command by using a combination of the /A /L switches to capture the verbose output of Xcopy. Then use the /M switch to copy and flip the archive bit off if the verbose output is what we want and do the other things as well. If the archive attribute gets turned on every time the file is edited this may work. Or maybe just use the ATTRIB command to see if the Archive attribute is set as well. Kind of the same thing Xcopy /A /L would tell you that it would copy it because the archive attribute is set.

eroica70
Posts: 9
Joined: 18 Feb 2013 12:33

Re: ? on batch file not working

#9 Post by eroica70 » 20 Feb 2013 17:19

Yes that seems to work. Last night I was working on the trouble and found this program from sysinternals that identifies open handles called handle.exe. I incorporated it into the batch file like this:
REM ====[ Settings ]==================================
SET "FileName=C:\monitor\today.ppsx"
SET "Local_FName=C:\today.ppsx"
SET "Xtime=15"

REM ====[ Check to see if today.ppsx is running ] ========
tasklist /FI "WINDOWTITLE eq PowerPoint Slide Show - [today.ppsx]"2>NUL | find /I /N "powerpnt.exe"2>NUL
if "%ERRORLEVEL%"=="0" goto :Loop
if "%ERRORLEVEL%"=="1" start c:\today.ppsx

REM ====[ Check and overwrite each Xtime Sec ]=============
:Loop

rem get shared file time/date
For /F %%A In ("%FileName%") Do SET "SharedTime=%%~tA"

rem get local file time/date
For /F %%A In ("%Local_FName%") Do SET "LocalTime=%%~tA"

rem compare shared with local file also release open handle om today.ppsx using handle.exe
IF NOT "%LocalTime%" == "%SharedTime%" (
for /F "tokens=3,6 delims=: " %%I IN ('handle.exe -accepteula C:\today.ppsx') DO handle.exe -c %%J -y -p %%I >NUL
Taskkill /F /IM:"powerpnt.exe" >NUL
Copy "%FileName%" "%Local_FName%" /Y >NUL
Start "" "%Local_FName%"
)

rem wait and loop
Ping Localhost -n %Xtime% >NUL
GOTO :Loop
I got that snippet of code off of a forum we're somebody had posted how to use handle.exe. i couldn't begin to explain how that line works but it does solve the issue of the handle on today.ppsx being open when the program goes to copy it. I also noticed that if I removed the /F switch on taskkill that also solved the problem somewhat. The .ppsx file would take a couple of seconds to die and I would get a couple of those error messages but within 5-10 secs the copy would take place. I guess the force kill didnt give powerpoint enough time to close the handles it had opened??The delay you introduced with the ping works nicely and it doesnt require any additional program to work.
If you could one more thing. If I wanted to check the date on C:\today.ppsx in the morning when computer reboots and if the date was yesterday make it put up a generic slide till the new today.ppsx slide was introduced for that day any ideas on how one would go about doing this? Thanks again for your help.

P.S. If you have any suggestions on good sites for batch programming please feel free. The batch program language I find very obtuse. I can read a program in higher level language like Pascal or PL/1 but this is a different animal.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: ? on batch file not working

#10 Post by Squashman » 20 Feb 2013 18:25

Wow! Another PL1 guy! I have done Pascal, Fortran and PL1 many years ago and yes they are different then most scripting languages. You pretty much have to learn any of them.

Post Reply