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!

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
  • 2
    Possible duplicate of [Batch Script to delete files based on findstr regex](http://stackoverflow.com/questions/18386164/batch-script-to-delete-files-based-on-findstr-regex) – Rolwin Crasta Oct 15 '15 at 06:49

3 Answers3

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.

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
0

try this batch script

FOR /f "tokens=*" %%a in ('dir /b | findstr ^.*test_.[0-9]{1}\.txt') DO rd %a
Rolwin Crasta
  • 4,219
  • 3
  • 35
  • 45
  • 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 – Rongali Ramunaidu Oct 15 '15 at 11:58
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.

Ryan
  • 1