4

I have script that runs on a Windows server to perform operations on certain files. The script often fails because various users (even myself) have open file shares on the server that they've forgotten to close, so the folders are locked.

I've added code to check for open files with net file, and close them if they match certain patterns. The limitation there is that net file summarizes the path of the open files, e.g.

ID         Path                                    User name            # Locks
-------------------------------------------------------------------------------
2          C:\Program Files\...\cocoon-2.1.11\     huttarl               0

when the full path of the open file is

C:\Program Files\Apache Software Foundation\cocoon-2.1.11

So when my script checks that the file path matches a certain pattern, such as "Apache", it sometimes misses files that have ... in the middle of their net file listing.

So my question is, how can I get the full path of an open file, in a script?

Currently, I'm using cygwin/bash. So if I have to switch to WSH, there will be some porting to do. But if that's the only option, I'll go for it.

If it matters, the system information panel says my Windows edition is Windows Server Standard, (c) 2007, SP2.

3 Answers 3

4

I have found the following useful:

openfiles /query /v | findstr "string what You want"

3

You should use PsFile

About it

The "net file" command shows you a list of the files that other computers have opened on the system upon which you execute the command, however it truncates long path names and doesn't let you see that information for remote systems. PsFile is a command-line utility that shows a list of files on a system that are opened remotely, and it also allows you to close opened files either by name or by a file identifier.

1
  • Sounds good. Unless somebody tells me a way to do it using tools that are already installed with Windows, I will accept this answer.
    – LarsH
    May 16, 2013 at 19:13
1

Ah ... looks like I had actually found this a year or more ago.

Turns out that if you give net file the ID of an open file, it will output the full path.

So for my example in the question, if I typed

C:\>net file 2

I would get

File ID         2
User name       huttarl
Locks           0
Path            C:\Program Files\Apache Software Foundation\cocoon-2.1.11\
Permissions     (none)
The command completed successfully.

Using this feature would mean

  • parsing out the ID numbers from the output of the initial net file command
  • looping through them and calling net file again with each ID
  • testing the second output
  • and closing the files whose paths match the pattern.

Whereas with PSFile, I could do all this with one simple command. But with net use, I don't have to install 3rd-party software when we migrate the script to a new server. Decisions, decisions...

You must log in to answer this question.

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