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.

Background

Delete a set of files, scattered across different directories.

Problem

The following code does not work (the unescaped | causes issues);

for %i in (dir /s/b | find "lock") do echo del %i

Question

Without writing a batch file, how would you delete all files named "lock" (i.e., found using the find command) within the current directory and all subdirectories (including hidden directories)?

Thank you!

share|improve this question
    
The reason the code does not work is because it is malformed. See: technet.microsoft.com/en-us/library/bb490909.aspx Correct line of code is as follows: for "usebackq" %i in (`dir /s/b ^| find "lock"`) do echo del %i –  David Ruhmann Dec 17 '12 at 21:48
    
Ignore my comment above and see my answer below. –  David Ruhmann Dec 17 '12 at 22:04
add comment

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
Thank you, David. –  Dave Jarvis Dec 17 '12 at 22:07
    
@DaveJarvis Your welcome. –  David Ruhmann Dec 17 '12 at 22:18
    
@DaveJarvis You may also want to add the /l option to the dir command to cause a pure lowercase comparison, or add the /i option to the find command to cause a case-insensitive comparison. –  David Ruhmann Dec 17 '12 at 22:24
add comment

I've dealt with this problem before and I used xargs and exec. Hopefully this link should be helpful. http://danielmiessler.com/blog/linux-xargs-vs-exec

share|improve this answer
1  
For Windows, not Linux. ;-) –  Dave Jarvis Dec 17 '12 at 22:06
add comment

For a given drive and/or path (looks in subfolders also):

del /f /s /q c:\windows\lock
share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.