Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 0
12346 C:\MyProgram\0002.lock 0
12347 C:\MyProgram\0003.lock 0
12348 C:\MyProgram\0004.lock 0
12349 C:\MyProgram\0005.lock 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

You must log in to answer this question.

Browse other questions tagged .