jeudi 26 février 2015

Batch file - Deleting files with a name matching this regex OR that regex but NOT that one

I try to write a Windows batch file to delete all files in a directory matching ((regex1 or regex2) and not regex3).


I'm able to delete files matching a regular expression using something like:



for /f "tokens=*" %%a in (
'dir /a-d /b *.* ^| findstr /r /c:"^test[0-9]*.jar$"'
) do del "%%a"


But I have no idea how I can say :



  1. this regex or this one.

  2. but not this one


~~~


UPDATE


A better example of what I'm trying to do :


The .bat file would be automatically ran at the end of the extraction of a 7-zip's SFX (self-extracting) executable. This executable will update some versioned jars inside a folder structure.


Let's say the executable will extract a test-1.0.2.jar file inside a lib folder. If this exact file (same version) already exists, then it is overwritten, this already works well since it is managed by the SFX executable itself. But if there already are other versions of the same file in the lib folder, I want the .bat file to delete them! For example :



  • test-1.0.1.jar

  • test-1.0.3.jar

  • test-1.0.jar

  • test-1.jar

  • test.jar


...would have to be deleted.


But those must not be deleted :



  • test-1.0.2.jar (the new file itself)

  • test-test2-1.0.0.jar (not a version of the same file)


I'm currently not able to express that using only one regex in the .bat , because findstr's regex capabilities are pretty limited. But if I was able to say :


Anything that is :



  • ^test.jar$ or

  • ^test-[0-9]+.jar$ or

  • ^test-[0-9]+.[0-9]+.jar$ or

  • ^test-[0-9]+.[0-9]+.[0-9]+.jar$


... must be deleted except :



  • test-1.0.2.jar


Then it would work.


Any idea?


Aucun commentaire:

Enregistrer un commentaire