Link to home
Start Free TrialLog in
Avatar of tabush
tabush

asked on

Command line to close open files in share - (ET)

I am trying to write a script that will close all open file inside a certain share folder. Anyone know if this can be accomplished via command line?

Something that would accomplish the same as manually going to Computer Management, looking for a certain folder, then right clicking all the files and choosing 'close open file.'
Avatar of Vick Vega
Vick Vega
Flag of Canada image

Yes, through Powershell, as usual.
Close-SmbOpenFile
Avatar of tabush
tabush

ASKER

Thanks.
Then i just put the file path at the end?
Scroll down to the examples section
Avatar of Shaun Vermaak
From that link
Get-SmbOpenFile | Where-Object -Property ShareRelativePath -Match ".DOCX" | Close-SmbOpenFile -Force

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany 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
Avatar of tabush

ASKER

Thank you. I will try this out.
Sorry i didnt notice your original answer had that link.
Avatar of tabush

ASKER

@Qlemo - that doesnt seem to work
@Shaun - Not sure how i plug in the path. I tried replacing ShareRelativePath with the local path and removing -match because i want all files but it didnt work.
If my suggestion does not work, you need to look at the path literal used in -like. This needs to be the full path to the folder you want to check, plus an trailing asterisk to match everything following the path name (subfolders and files).

ShareRelativePath is not suitable, as it contains only the part starting from the share location, without any information about the share itself.
Avatar of tabush

ASKER

So i tried this command to see if it will even report the open files and its not
Get-SmbOpenFile | Where-Object Path -eq "D:\FTP Share\TEST DROP FOLDER\" | Format-List

Open in new window


I am seeing them in computer manager (see screenshot)
User generated image
Path contains the complete path, including the file name. So with your code you should get all open connections for the folder only - and that works for me on my test machine.
You should use the like as I have shown, to catch all files and folders opened below that path, and not just the folder itself.

What do you get if you do not apply any filter?
Avatar of tabush

ASKER

thank you. I ended up finding an alternate solution to what we were trying to solve.
In this scenario we can just delete the files so that was easier to accomplish via script.
The comment in #a42440374 is approved and works, if used correctly. It is simple enough to be used correctly.

The suggested author comment is not acceptable (does not contain the necessary details to consider it as such).