? on batch file not working
Moderator: DosItHelp
? on batch file not working
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
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
Re: ? on batch file not working
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.
Which forum are you referring to?
Best wishes!
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!
Re: ? on batch file not working
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.
Re: ? on batch file not working
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
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.
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
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.
Re: ? on batch file not working
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.
Re: ? on batch file not working
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.
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.
Re: ? on batch file not working
Try this code,
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.
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.
Re: ? on batch file not working
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.
Re: ? on batch file not working
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.
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.
Re: ? on batch file not working
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.