0

I need help in deleting files with specific patterns in a folder.

For example in C:/program Files/ I have files like: test_1.txt, test_2.txt, test_3.txt, test_4.txt, test_5.txt.

I would like to delete the files from test_2.txt to test_5.txt. Thank you!

1

3 Answers 3

1
FOR /l %%i in (2,1,6 ) DO (
FOR /f "tokens=*" %%a in ('dir /b  ^| findstr test_%%i') DO DEL %%a
)

This worked fine for me.

Sign up to request clarification or add additional context in comments.

Comments

0

try this batch script

FOR /f "tokens=*" %%a in ('dir /b | findstr ^.*test_.[0-9]{1}\.txt') DO rd %a

1 Comment

Hi @Rolwin C the above solution is not working .However using this FOR /f "tokens=*" %%a in ('dir /b ^| findstr test_') DO DEL %%a i am able to delete all the files with test_ as prefix but not able to delete selected files
0

You could use,

for i in `seq 2 5`; do rm -f test_$i.txt; done

That assumes you delete files from test_2.txt to test_5.txt. You should change the numbers if you're going to delete files in a different range.

1 Comment

This will only work in *NIX-based environments. The question requires a Windows answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.