Writing a batch file to copy network file and open it then close and loop
We have a file on the network that is regularly updated and I need it open and up to date, but when its open no one else can access it to update it for me.
so I thought I might have a script that will:
- copy the file to my desktop
- open the file and wait an hour
- close winword (only that instance not all)
- delete the file
- go back to start.
I cannot find a way to only close the file rather than every winword.
Here is the sript so far (edited out anything private)
echo off
cls
echo.
TIMEOUT /T 10 /NOBREAK
echo.
:start
echo.
echo Updating File...
echo.
copy "T:\Shared\file.doc" "C:\Users\USERNAME\Desktop"
echo.
echo.
TIMEOUT /T 2
echo.
echo Opening File
echo.
start "File" "file.doc"
echo.
echo Waiting For One Hour...
echo.
TIMEOUT /T 3600
echo.
echo Deleting Old File
echo.
del "C:\Users\USERNAME\Desktop\file.doc"
goto start
You could capture the PID of the process to call or the file to open with WMIC and set the process ID number to a variable, and then later use that variable with TASKKILL using the /PID
switch which I posted the example batch script below and then another below that based on your logic how I think this needs to fit into it but adjust accordingly as needed.
This logic creates a couple dynamic temp files, using a FOR /F to parse the process ID which is piped to the temp file to capture just the numerical value that'll be used with TASKKILL to kill that specific process ID (i.e./PID
switch).
This logic does the following:
- Opens the process\file
- Waits an hour
- Closes only that instance of the process\opened file
Batch Script Example
(Start a process, capture the [PID] Process ID of that process, and then later kill that process based on the same Process ID)
This script sets the application variable and might need to be the full path to the app on the machine (SET App=
), and the full path to the file which that app will open, etc. (SET AppFile=
).
@ECHO ON
:::SET App=C:\Program Files\Microsoft Office 15\root\office15\WINWORD.EXE
SET App=%windir%\%Notepad.exe
:::SET AppFile=C:\Users\User\Desktop\Test\ENTER_EXIT_List.docx
SET AppFile=C:\Users\User\Desktop\Test\List.txt
SET DynamicTempFile1=%temp%\~tmpProcessID1_%~nx0.dat
SET DynamicTempFile2=%temp%\~tmpProcessID2_%~nx0.dat
IF EXIST "%DynamicTempFile1%" DEL /Q /F "%DynamicTempFile1%"
IF EXIST "%DynamicTempFile2%" DEL /Q /F "%DynamicTempFile2%"
::start a process and pass it a file, etc. to open or process and then pipe the process ID to a temp file
WMIC PROCESS CALL CREATE "%App% %AppFile%" | FIND "ProcessId">>"%DynamicTempFile1%"
::parse out unneeded characters for only numeric values from temp file
FOR /F "DELIMS==; TOKENS=2" %%F IN (%DynamicTempFile1%) DO ECHO %%~F >> "%DynamicTempFile2%"
::sets the PID to a variable with the value in the temp file
SET /P processid= < %DynamicTempFile2%
::wait 3600 seconds or one hour and then run the taskkill command
PING /n 3600 127.0.0.1 > nul
::kill process by PID from the previously set variable
TASKKILL /PID %processid%
GOTO EOF
Further Resources
- WMIC
- TASKKILL
- FOR /F
Script example based on your logic
This assumes you are opening up and killing the file which is copied but just set the variable up top.
ECHO OFF
::://Set application full path and the app file full path that the app will open
:::SET App=%windir%\%Notepad.exe
:::SET AppFile=C:\Users\User\Desktop\Test\List.txt
SET App=C:\Program Files\Microsoft Office 15\root\office15\WINWORD.EXE
SET DesktopFolder=C:\Users\<USERNAME>\Desktop
::SET AppFile=%DesktopFolder%\file.doc
SET AppFile=C:\Users\<USERNAME>\Desktop\file.doc
SET CopyFile=T:\Shared\file.doc
SET DynamicTempFile1=%temp%\~tmpProcessID1_%~nx0.dat
SET DynamicTempFile2=%temp%\~tmpProcessID2_%~nx0.dat
IF EXIST "%DynamicTempFile1%" DEL /Q /F "%DynamicTempFile1%"
IF EXIST "%DynamicTempFile2%" DEL /Q /F "%DynamicTempFile2%"
CLS
ECHO.
TIMEOUT /T 10 /NOBREAK
ECHO.
:start
ECHO.
ECHO Updating File...
ECHO.
COPY /Y "%CopyFile%" "%DesktopFolder%"
ECHO.
ECHO.
TIMEOUT /T 2
ECHO.
ECHO Opening File
ECHO.
:::START "File" "file.doc"
::start a process and pass it a file, etc. to open or process and then pipe the process ID to a temp file
WMIC PROCESS CALL CREATE "%App% %AppFile%" | FIND "ProcessId">>"%DynamicTempFile1%"
::parse out unneeded characters for only numeric values from temp file
FOR /F "DELIMS==; TOKENS=2" %%F IN (%DynamicTempFile1%) DO ECHO %%~F >> "%DynamicTempFile2%"
::sets the PID to a variable with the value in the temp file
SET /P processid= < %DynamicTempFile2%
ECHO.
ECHO Waiting For One Hour...
ECHO.
TIMEOUT /T 3600
ECHO.
ECHO Deleting Old File
ECHO.
::kill process by PID from the previously set variable
TASKKILL /PID %processid%
DEL /Q /F "%AppFile%"
GOTO start