How to unlock files using handle.exe and process name?

65

Solution 1

You can use handle.exe once to list the PIDs and handles, then again multiple times to close each one.

Use the for /f command to loop through the result of the first command. I don't have a Windows machine handy to test on, but it should look something like this:

From a batch file:

for /F "tokens=3,6 delims=: " %%I IN ('handle.exe -accepteula TestPro.log') DO handle.exe -c %%J -y -p %%I

From the command-line:

for /F "tokens=3,6 delims=: " %I IN ('handle.exe -accepteula TestPro.log') DO handle.exe -c %J -y -p %I

Solution 2

https://technet.microsoft.com/en-us/sysinternals/handle.aspx

TEST AND DISPLAY (DRY-RUN):

for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do @echo %i %j                                                                    

EXECUTE (CLOSE HANDLES):

for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do handle.exe -c %j -y -p %i  

EXECUTE (CLOSE HANDLES) AND THEN TASKKILL:

for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do handle.exe -c %j -y -p %i & taskkill /t /f /PID %i                                                       
Share:
65

Related videos on Youtube

Radek
Author by

Radek

Updated on September 18, 2022

Comments

  • Radek
    Radek almost 2 years

    I am trying to build the following layout with bootstrap 4, I would like 2 bigger columns on the left and then 4 grouped together on the right but their height togetehr cannot exceed the heigth of the first 2.

    Except I just cant get it to work.

    This is what I want:

    enter image description here

    This is basically what I got:

    https://codepen.io/anon/pen/ZrBpze

    <section id="mu-featured">
            <div class="container">
                <div class="row">
    
                    <div class="col-md-4 col-sm-6">
                        <img src="img/picture.jpg" alt="" style="height: 235px">
                        <h1>Header</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                            Illo suscipit facilis ipsum ullam reiciendis odio error
                            iste neque ratione libero rem accusamus voluptatibus, nihil
                            unde maiores sunt nisi. Assumenda, consectetur.</p>
                        <a href="#" class="">Call to action!</a>
                    </div>
    
                    <div class="col-md-4 col-sm-6">
                        <img src="img/picture.jpg" alt="" style="height: 235px">
                        <h1>Header</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                            Illo suscipit facilis ipsum ullam reiciendis odio error
                            iste neque ratione libero rem accusamus voluptatibus, nihil
                            unde maiores sunt nisi. Assumenda, consectetur.</p>
                        <a href="#" class="">Call to action!</a>
                    </div>
    
                    <div class="col-md-4 col-sm-6">
                        <img src="img/picture.jpg" alt="" style="height: 85px">
                        <h1>Header</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                            Illo suscipit facilis ipsum ullam reiciendis odio error
                            iste neque ratione libero rem accusamus voluptatibus, nihil
                            unde maiores sunt nisi. Assumenda, consectetur.  <a href="#" class="">Call to action!</a></p>
                    </div>
    
                    <div class="col-md-4 col-sm-6">
                        <img src="img/picture.jpg" alt="" style="height: 85px">
                        <h1>Header</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                            Illo suscipit facilis ipsum ullam reiciendis odio error
                            iste neque ratione libero rem accusamus voluptatibus, nihil
                            unde maiores sunt nisi. Assumenda, consectetur.  <a href="#" class="">Call to action!</a></p>
                    </div>
    
                    <div class="col-md-4 col-sm-6">
                        <img src="img/picture.jpg" alt="" style="height: 85px">
                        <h1>Header</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                            Illo suscipit facilis ipsum ullam reiciendis odio error
                            iste neque ratione libero rem accusamus voluptatibus, nihil
                            unde maiores sunt nisi. Assumenda, consectetur.  <a href="#" class="">Call to action!</a></p>
                    </div>
    
                    <div class="col-md-4 col-sm-6">
                        <img src="img/picture.jpg" alt="" style="height: 85px">
                        <h1>Header</h1>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.
                            Illo suscipit facilis ipsum ullam reiciendis odio error
                            iste neque ratione libero rem accusamus voluptatibus, nihil
                            unde maiores sunt nisi. Assumenda, consectetur.  <a href="#" class="">Call to action!</a></p>
    
                    </div>
    
                </div>
            </div>
        </section>
    
    • Mihai T
      Mihai T over 6 years
      and what have you tried to solve this problem ? i see that all cols have the same classes
  • Radek
    Radek over 11 years
    I cannot make it work. Your code gives me ('handle.exe was unexpected at this time. I think there is missing in before ('handle.exe was unexpected at this time. But then %%J contains :` so it's not valid argument for handle.exe
  • Stephen Jennings
    Stephen Jennings over 11 years
    Yeah, I remember having trouble getting the command exactly right, but I can't remember what I did off the top of my head. I'm trying to get access to my batch file again so I can update my answer with the right syntax.
  • Stephen Jennings
    Stephen Jennings over 11 years
    @Radek I updated my answer with the actual code I used. Looks like I got %%I and %%J mixed up, and we need the colon as a delimiter. Try this and see how it goes.
  • Radek
    Radek over 11 years
    Works nicely (both batch file & command line versions) if cmd started as administrator. Any idea how I can make it work as a part of my overnight automation testing? runas doesn't work for me in this case.
  • Radek
    Radek over 11 years
    Got it working. The trick is that the command to run under runas is in fact cmd.exe and the argument for cmd is your code. I was doing it without cmd.
  • johntrepreneur
    johntrepreneur almost 10 years
    @StephenJennings - any chance you could whip up this same syntax in powershell?
  • bertieb
    bertieb over 7 years
    Can you expand on this and explain how it improves on the (four and a half year old) accepted answer?
  • I say Reinstate Monica
    I say Reinstate Monica over 7 years
    I see your commands are different than the accepted answer, so its a legitimate answer. However, as bertieb indicated, your answer needs an explanation as to what your commands will do and how they address the OPs question. Thanks for contributing to SuperUser.
  • WebDevBooster
    WebDevBooster over 6 years
    In the second code snippet I just added, the small images and their text content are separated in 2 different columns and <div class="w-100"></div> is used as a separator.
  • Admin
    Admin over 6 years
    Thanks this did exactly what I wanted! I edited it a little bit to what I wanted with the pictures, except one thing I dont understand is how I can match the width of images to the width of the column its in. jsfiddle.net/ggqt15zm/4 This was my idea for the smaller screens by the way but I wanted to focus on desktop first and then later work on the smaller screens. No idea if thats the right way to do it. imgur.com/a/SbFb1