2

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
3
  • You might try @Kill, which supposedly can kill based on window names (akill -K "file.doc"). taskkill can find window names, but as you probably are aware, it kills every Word window. Commented Jul 18, 2016 at 5:43
  • Good question. I must have mistyped something when I tried it previously because it closed every Word window I had open. I tried it again and it closed only the specific document. I'm curious, though, since the answer is so easy, why is your answer so complicated? The OP re-uses a document title, so closing only that document is simple. Commented Jul 21, 2016 at 17:03
  • @benJephunneh I'm not sure what you mean by my answer being complicated it's just a basic batch script & you set the variables in it accordingly but I deleted my comment since you saw it already. I was just pointing out to you that TASKKILL has a built-in feature for this method & there's no need to use third party software for this since it's already built into windows to kill a task based on it's title metadata. If that machine has another session and someone has a document opened with that same title, then closing it based on the title metadata will close all those;The PID is what he needs. Commented Jul 22, 2016 at 16:25

2 Answers 2

1

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:

  1. Opens the process\file
  2. Waits an hour
  3. 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



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
0

One idea I have is to open the file in WordPad and then just use task kill on that which won't disturb open word docs!

Edit didn't work, opened up gibberish.

Edit 2:. Used wordviewer.exe and my idea work.

I now use task kill on wordviewer and that leaves my other word docs alone!

Then it's loops every hour to a fresh copy of the file!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .