Discussion Locked

Need an NT batch script

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I need NT batch script that would disconnect all the users from certain file abc123.mdb. File name should be an argument. I think I need to use NET FILE command but I am not sure how to filter out all the connection IDs.

7 replies

by ndambrosio

The "NET FILE" Command will close an open file and release the locked records, but you'll have to execute batch this command from the server where the file is shared. To tell you the truth as far as connection IDs go the IDs are randomly assigned depending on the open shares on that server. You might want to post another question asking about syntax on this specific command..

by roman_empire

The idea is to write the script that would dump all active user connections to all files then parse it to find connections to a particular file then extract corresponding connection IDs and then feed them to the NET FILE command for closure.

by bryan

Here's your batchfile:

--begin here --

if "%1"=="" (echo You must specify a filename and path as the argument to this command) & goto eof
if not "%2"=="" echo Too many arguments
net file > junk.tmp
for /f "skip=4 tokens=1,2 delims= " %%a in(junk.tmp) do call :parseit %%a %%b %1
del junk.tmp
goto :eof

:parseit
::%1 is %%a from above (the file id)
::%2 is %%b from above (the filename with path as output from the NET FILE command)
::%3 is the specified filename (as provided via commandline argument)
if /I "%2"=="%3" net file %1 /close & echo id %1 (%2) was closed
goto :eof

-- end here --

Please notice that you have to specify the full path to the file (d:\path\file.txt) as the argument.

by bryan

The html sets word wrap too narrow. Here's the same code, and I have put a "|ret|" in each place there should be a carriage return:

--begin here --

if "%1"=="" (echo You must specify a filename and path as the argument to this command) & goto:eof|ret|
if not "%2"=="" (echo Too many arguments) & goto :eof|ret|
net file > junk.tmp|ret|
for /f "skip=4 tokens=1,2 delims= " %%a in (junk.tmp) do call :parseit %%a %%b %1
del junk.tmp|ret|
goto :eof|ret|
|ret|
:parseit|ret|
::%1 is %%afrom above (the file id)|ret|
::%2 is %%b from above (the filename with path as output from the NET FILE command)
::%3 is the specified filename (as provided via commandline argument)|ret|
if /I "%2"=="%3" net file %1 /close & echo id %1 (%2) was closed|ret|
goto :eof|ret|

-- end here --

by bryan

Arrrrgh! These teeny text-edit windows are screwing me up! One last chance for me to get this right:

if "%1"=="" (echo You must specify a filename and path as the argument to this command) & goto :eof|ret|
if not "%2"=="" (echo Too many arguments) & goto :eof|ret|
net file > junk.tmp|ret|
for /f "skip=4 tokens=1,2 delims= " %%a in (junk.tmp) do call :parseit %%a %%b %1|ret|
del junk.tmp|ret|
goto :eof|ret|
|ret|
:parseit|ret|
::%1 is %%a from above (the file id)|ret|
::%2 is%%b from above (the filename with path as output from the NET FILE command)|ret|
::%3 is the specified filename (as provided via commandline argument)|ret|
if /I "%2"=="%3" net file %1 /close & echo id %1 (%2) was closed|ret|
goto :eof|ret|There we go. Now just replace each occurrence of "|ret|" with a carriage return (very important, there can be no spaces after the "goto :eof" statements!!)

-Bryan

This discussion is closed to new replies.