batch-filedelete-file

Delete files in current folder that has specific text in filename


I am trying to use a batch script to delete files that has a ( in its name. For example, the file that I would like to delete has the name "Test1 - Copy (5).txt".

I tried this command but it does not work:

for /f "delims=" %%a in (' 
    findstr /l /i /m /c:"(" "C:\Users\Desktop\New folder\*.*"
') do echo del "%%a"

Can you assist me in getting the correct code to delete the files that has a ( in its name? Thanks!


Solution

  • Usually when someone online gives you code that could be destructive (like code to delete a bunch of files), they'll preface the delete command with an echo so that you can see what commands would be run. In order to actually run the command, simply remove the echo:

    for /f "delims=" %%a in ('findstr /l /i /m /c:"(" "C:\Users\Desktop\New folder\*.*"') do del "%%~a"
    

    However, this is a lot of typing (and it's doing a case-insensitive search for an open parentheses for some reason), so you can simply use wildcards to delete any files whose name contain the string you're looking for:

    del "%USERPROFILE%\Desktop\New folder\*(*"