Link to home
Start Free TrialLog in
Avatar of NewsInternationalLtd
NewsInternationalLtd

asked on

Close open files on remote machine

I would like to be able to close an open file on a remote server. I know the name of the file and would like to close it programmatically in either VBS or VB6. I have been trying using the code below but this doesn't seem to work. Has anyone got any ideas?

Set objConnection = GetObject("WinNT://TESTSERVER/LanmanServer")
Set colResources = objConnection.Resources
On Error Resume Next
For Each objResource in colResources
If Instr(1,objResource.Path,"Authorise.exe") > 0 Then colResources.Remove(objResource.Name)
Next
Avatar of Lester_Clayton
Lester_Clayton

Avatar of NewsInternationalLtd

ASKER

This is probably the kind of thing I'm after but having a little difficulty finding a good working example of the VB6 code.
You should SEVERE the connection to the Object or CLOSE it then set the Object to NOTHING to release it from memory.
I'm 99% certain I need to use NetFileEnum and NetFileClose APIs. However API programming is far from my strong point.
I found the following code regarding NetFileEnum which works perfectly in VB6:
http://vbnet.mvps.org/index.html?code/network/netfileenum.htm
I want to be able to add a line:
    If GetPointerToByteStringW(fi3.fi3_pathname)="blah, blah" Then NetFileClose blah, blah
However to do this I need to be able to obtain the file ID (presumably from NetFileEnum) but I'm not sure how to do this. Ideally I need a working example of both API's together in VB6.
I have upped the points to 400 as I realise this is a little trickier than I first envisioned!
OK, have spotted the fileID in the above example, feel a bit silly for missing that (fi3.fi3_id).
Just need to figure out how to use NetFileClose with it if anyone can help.
When you want you use an API you will need your project explorer, should be on right side.
If not visible Control R will bring it up.  Now Right Click and Select Add->Module.
Give it what ever name you want like (API)ToolBox.BAS

There are hundreds of different APIs' that you can use.  When you pick the one you want you will see a list of reserved words and their value.

You have to have them in the BAS File.

The way the API talks to an object us usually by a handle that you must pass to it so It knows what to take control over.  You can do amazing impressive things with API s' and also a great deal of damage so be careful.

There are librarys' available that have the API s' and required reserve words so that you can just copy and paste into your BAS file.

You call an API just like you call a function or procedure, you give the name and populate the required paramaters, and you will get a reply through the paramaters.

Sometimes you will use more than one API, One to get the handle, and one or more to take care of your task.

I will look through my code archive for my library to give you more info.  May take a while.  If you go to Borders or a computer book store you can buy one of the API books 5 or so inches thick and you will have the keys to the kingdom so to speak.
 
Thanks
Roger
Thanks but to award the points I would like a specific example, perhaps based on the link I gave but ideally as simple as possible, capturing the file Id and closing the file. I will keep this question open for  another week or two after which I will delete if there are no answers. I have managed to work around the problem but would still like to know the solution to this.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologies for the delay is responding. This only happens occasionally so had to wait for it to happen again before replying. Although I was looking for a solution that didn't rely on any 3rd party utilities, the solution above does work with a little tinkering (didn't want the prompt, but this was easily fixed). It isn't too much hassle just to stick psexec on a share somewhere. Thanks for your effort.
Have awarded B for the reason it relied on PSExec. Thanks for your efforts though and I am happy to use this.
No problem.  That's fine.  PSExec is not necessarily a third party tool anymore, as Microsoft bought it from SysInternals.  The Windows Script Host goes have the ability to execute scripts remotely through WMI, but it is often problematic.  PSExec is the simplest to use, and a *much* better alternative to RunAs.

Regards,

Rob.
How do you remove that prompt asking which ID you want to close? I just want it to close without asking.
Thanks.
Sorry I am new VBScripting, can you please advise how to run this script without prompt which session to close?