Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using Windows command prompt, I want to unlock and delete several files in a single command if possible. So far I have managed to come up with:

net file | findstr /R [0-9]*lock

This results in:

12345 C:\MyProgram\0001.lock user.name 0
12346 C:\MyProgram\0002.lock user.name 0
12347 C:\MyProgram\0003.lock user.name 0
12348 C:\MyProgram\0004.lock user.name 0
12349 C:\MyProgram\0005.lock user.name 0

The first part of the string (ID) would be used as follows:

net file 12345 /close

The second part of the string (path) would be used as follows:

del C:\MyProgram\0001.lock

I would like to put this into a batch script, which would cycle through each line ouputted by findstr, grab the 1st and 2nd parts and execute the commands accordingly. Any help would be greatly appreciated.

share|improve this question
This sort of thing is much easier with a Unix shell and tools like sed on Windows. Is that an option for you or do you need this done using only the stuff that ships with Windows? –  Nicole Hamilton Sep 21 '12 at 17:00
Check out the help for the for command. The default space and tab delims will split the strings, and tokens can be used to retrieve only the first two portions that you're after. –  Karan Sep 21 '12 at 17:08
Thanks Karan, this worked best for me. If you could post this as an answer, I'll give you the points. :) –  Reado Oct 9 '12 at 8:45
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.