Your code needs a few touchups. The pipe operator needs to be escaped by the batch escape character ^ and when using quotations within the parentheses for a command, the usebackq option must be specified.
for /?
Batch Format:
for /f "usebackq" %%i in (`dir /s /b ^| find "lock"`) do echo %%i
Command Line Format:
for /f "usebackq" %i in (`dir /s /b ^| find "lock"`) do echo %i
Replace echo with del and any of its options when you want it to actually delete the files. Note the double percent signs are needed when used within a bat file, single when used directly on the command line.
Another method is to use the forfiles command. forfiles /?
forfiles /m *lock* /s /c "cmd /c echo @file"
Note, both of these methods will also delete any folders that contain the search term lock. Additional steps would be needed to be taken to prevent this.
for "usebackq" %i in (`dir /s/b ^| find "lock"`) do echo del %i– David Ruhmann Dec 17 '12 at 21:48