There is a directory with a set of files. Files have the same structure. 1 row heading is always different. All subsequent lines (one or more) contain information divided by the symbol ^ .

An example of one of the files:

Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ 1^4^63350^тСкст^тСкст^11.2016^7^0^6^2^^201^132^2004^SC^MS^^ 1^1^63650^тСкст^тСкст^16.2056^7^0^6^2^^201^132^2004^SC^MS^^ 1^2^65350^тСкст^тСкст^15.2046^7^0^6^2^^201^132^2004^SC^MS^^ 1^8^63650^тСкст^тСкст^16.2056^7^0^6^2^^201^132^2004^SC^MS^^ 1^2^65350^тСкст^тСкст^15.2046^7^0^6^2^^201^132^2004^SC^MS^^ 

Task: leave in the folder only those files in which starting from the second line, at least in one of the lines, after the first character ^ and before the second character ^ there is a digit 2 . Other files to delete.

    1 answer 1

    1st option
    If you only need files that contain lines like:

    1 ^ 2 ^ 65350 ^ text ^ text ^ 15.2046 ^ 7 ^ 0 ^ 6 ^ 2 ^^ 201 ^ 132 ^ 2004 ^ SC ^ MS ^^

    but not

    1 ^ 123 ^ 65350 ^ text ^ text ^ 15.2046 ^ 7 ^ 0 ^ 6 ^ 2 ^^ 201 ^ 132 ^ 2004 ^ SC ^ MS ^^

    (save code as .cmd):

     @echo off setlocal enabledelayedexpansion :: здСсь ΡƒΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Π°ΠΌ ΠΈ маску для поиска set files=D:\3\*.txt for %%f in (%files%) do ( set found= for /f "skip=1 tokens=2 delims=^^" %%l in (%%f) do if "%%l" equ "2" set found=1 if "!found!" equ "" echo del /q %%f ) 

    2nd option
    If you also need files in which there are lines like:

    1 ^ 123 ^ 65350 ^ text ^ text ^ 15.2046 ^ 7 ^ 0 ^ 6 ^ 2 ^^ 201 ^ 132 ^ 2004 ^ SC ^ MS ^^

    those. after the first character ^ there is any number containing a two, then:

     @echo off setlocal enabledelayedexpansion :: здСсь ΡƒΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Π°ΠΌ ΠΈ маску для поиска set files=D:\3\*.txt for %%f in (%files%) do ( set found= for /f "skip=1 tokens=2 delims=^^" %%l in (%%f) do ( echo %%l | find "2" > nul if "!errorlevel!" equ "0" set found=1 ) if "!found!" equ "" echo del /q %%f ) 

    PS: If everything works as it should, do not forget to delete echo before del for real deletion of files.


    By the way, if the files are large, then you can use such a script - it should work faster (only for the first option):

     @echo off :: здСсь ΡƒΠΊΠ°ΠΆΠΈΡ‚Π΅ ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Π°ΠΌ ΠΈ маску для поиска set files=D:\3\*.txt for %%f in (%files%) do call :check %%f goto :eof :check for /f "skip=1 tokens=2 delims=^^" %%l in (%1) do if "%%l" equ "2" goto :eof echo del /q %1 
    • 1 option! Thank you for what you need! - Andrey
    • one
      Added more smart (in theory) option. - Alekcvp