Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Delete Lines From Plain Text File If End Of Line Matches Set Values  (Read 55201 times)

0 Members and 1 Guest are viewing this topic.

Thaum2u

    Topic Starter


    Rookie
    • No You!
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 11
To preface, I have a folder named MOVIES that I store all of my movie files in. Within that folder are subfolders for each letter of the alphabet, the # sign (for movies that begin with a number or other non-alphabetic character), and a folder named MUSIC RELATED (for music related movies.)

I made the batchfile to by default, output a folder label, which is the letter of the alphabet that is the folder name, surrounded by dashes to make it very easy to see the next letter in the output file.  I can also provide the argument   /nolabels   to output without the folder labels, but that's beside the point.

OUTPUT EXAMPLE:

- - - - - - - -  - MUSIC RELATED  - - - - - - - -
VAN HALEN LIVE SHOWS    <--- This is a sub-folder that contains what it is named. Only the folder name is output, not its contents)
AC-DC - Let There Be Rock [1980].mp4
Mayor Of The Sunset Strip [2003].mp4
Mayor Of The Sunset Strip [2003].srt
Searching For Sugar Man [2012].idx
Searching For Sugar Man [2012].mp4
Searching For Sugar Man [2012].sub

- - - - - - - -  #  - - - - - - - -
2001 - A Space Odyssey [1968].mp4
2001 - A Space Odyssey [1968].srt
The 40 Year Old Virgin (Unrated ) [2005].mp4
The 40 Year Old Virgin (Unrated ) [2005].srt

- - - - - - - -  A  - - - - - - - -
A Street Cat Named Bob [2016].idx
A Street Cat Named Bob [2016].mp4
A Street Cat Named Bob [2016].sub
Airplane! [1980].mkv
Airplane! [1980].srt

... Etc ...

I have a batchfile that adds the names of movies to a plain text file, of all the folders and movie files within the folder that the batchfile is run from.
To make it clear, I have a folder full of movies with names such as the following:

EXAMPLE OUTPUT FROM EXISTING BATCHFILE:

                 
- - - - - - - -  - MUSIC RELATED  - - - - - - - -
VAN HALEN LIVE SHOWS
AC-DC - Let There Be Rock [1980].mp4
Mayor Of The Sunset Strip [2003].mp4
Mayor Of The Sunset Strip [2003].srt
Searching For Sugar Man [2012].idx
Searching For Sugar Man [2012].mp4
Searching For Sugar Man [2012].sub


The batchfile works perfectly, except that ideally, I would like for the .srt, .idx, and .sub (files that contain the subtitles for each movie) to NOT be included in the resulting text file output.

EXAMPLE OUTPUT OF THE DESIRED MODIFIED NO SUBTITLES BATCHFILE:

- - - - - - - -  - MUSIC RELATED  - - - - - - - -
VAN HALEN LIVE SHOWS
AC-DC - Let There Be Rock [1980].mp4
Mayor Of The Sunset Strip [2003].mp4
Searching For Sugar Man [2012].mp4


Ideally, I would like to have the same batchfile do this, but if need be, I could run my existing batchfile, then run a different batchfile to process the output text file from the first to achieve my desired result.

I am not against using an external program, such as the awesome Notepad++, from within the batchfile to do some processing if need be. But if possible, it would be great to not use anything but the batchfile itself, or themselves, to accomplish everything.

I don't know how to check each output line for a certain ending, and to not output that line if it finds a match to any of the following endings:

.idx
.sub
.srt

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PART 2

I would also like to add another argument named   /noext   that would output everything the same, but strip off the file extension part of each movie.

EXAMPLE OUTPUT WITH THE  /noext  ARGUMENT SPECIFIED:

- - - - - - - -  - MUSIC RELATED  - - - - - - - -
VAN HALEN LIVE SHOWS
AC-DC - Let There Be Rock [1980]
Mayor Of The Sunset Strip [2003]
Searching For Sugar Man [2012]


I think I know how to do everything except how to strip off the file extensions for this part.

Attached is my batchfile as-is.

Any help is greatly appreciated?

- Thaum

carlos1001



    Beginner

    Thanked: 3
    • Computer: Specs
    • Experience: Beginner
    • OS: Linux variant
    Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
    « Reply #1 on: August 06, 2021, 04:58:54 PM »
    Try using findstr; eg.

    Code: [Select]
    dir "%%a"/OGN /B | findstr /v /i "\.srt$" | findstr /v /i "\.idx$" | findstr /v /i "\.sub$"

    Edit: formatting; looks like markdown doesn't work on here?

    Thaum2u

      Topic Starter


      Rookie
      • No You!
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 11
    Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
    « Reply #2 on: August 10, 2021, 03:50:02 AM »
    I did not know about FINDSTR.  But even with your code, I am uncertain how to use it to omit outputting to the text file?

    carlos1001



      Beginner

      Thanked: 3
      • Computer: Specs
      • Experience: Beginner
      • OS: Linux variant
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #3 on: August 10, 2021, 09:37:26 AM »
      The idea is to pipe the output from the dir command to findstr. By piping I mean direct the output of a command into another command by using the | symbol. Findstr can be used in multiple ways; it can exclude a text pattern, or it can show text based on the pattern. What I did there was exclude any lines that end with those extensions by chaining multiple calls to findstr, with each one excluding another extension. In order to use it, replace the dir command in the for loops you have in the batch file with something similar to what I posted.

      Hope this helps.

      Thaum2u

        Topic Starter


        Rookie
        • No You!
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 11
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #4 on: August 11, 2021, 01:23:08 AM »
      Thanks so much for taking the time to help.

      Here is what I have to start with:


      (
          echo - -  LIST GENERATED: %DATE%  - -
          FOR /F "delims=" %%a in ('dir /A:D /OGN /B') DO (
            echo.
            echo - - - - - - - -  %%a  - - - - - - - -
            dir "%%a"/OGN /B
          )
      )> MOVIE_LIST.TXT


      I've tried multiple ways of replacing your code into mine, but can't get it to work.

      Would you mind modifying what I have above with your example, since I can't seem to grasp it?

      Thaum2u

        Topic Starter


        Rookie
        • No You!
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 11
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #5 on: August 11, 2021, 02:37:09 AM »
      Oh stupid me. I got it now and it works exactly as I wanted. THANKS!!!

      Is there a way to edit and/or delete posts we make on here?


      Thaum2u

        Topic Starter


        Rookie
        • No You!
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 11
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #6 on: August 11, 2021, 02:42:47 AM »
      Now for part 2:

      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      PART 2

      I would also like to add another argument named   /noext   that would output everything the same, but strip off the file extension part of each movie.

      EXAMPLE OUTPUT WITH THE  /noext  ARGUMENT SPECIFIED:


      - - - - - - - -  - MUSIC RELATED  - - - - - - - -
      VAN HALEN LIVE SHOWS
      AC-DC - Let There Be Rock [1980]
      Mayor Of The Sunset Strip [2003]
      Searching For Sugar Man [2012]


      I think I know how to do everything except how to strip off the file extensions for this part.

      Attached is my newest updated version of the complete batchfile.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #7 on: August 27, 2021, 09:01:23 AM »
      Try using findstr; eg.

      Code: [Select]
      dir "%%a"/OGN /B | findstr /v /i "\.srt$" | findstr /v /i "\.idx$" | findstr /v /i "\.sub$"

      Edit: formatting; looks like markdown doesn't work on here?
      This can be made more efficient by combining the three FINDSTR commands into one.

      Code: [Select]
      dir "%%a"/OGN /B | findstr /RIVC:"\.srt$" /RIVC:"\.idx$" /RIVC:"\.sub$"

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #8 on: August 27, 2021, 09:17:07 AM »

      I would also like to add another argument named   /noext   that would output everything the same, but strip off the file extension part of each movie.


      Code: [Select]
      FOR /F "delims=" %%G IN ('dir "%%a"/OGN /B ^| findstr /RIVC:"\.srt$" /RIVC:"\.idx$" /RIVC:"\.sub$" /RIVC:"\.jpg$" /RIVC:"\.png$"') DO ECHO %%~nG

      Thaum2u

        Topic Starter


        Rookie
        • No You!
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 11
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #9 on: September 01, 2021, 03:31:58 AM »
      This can be made more efficient by combining the three FINDSTR commands into one.

      Code: [Select]
      dir "%%a"/OGN /B | findstr /RIVC:"\.srt$" /RIVC:"\.idx$" /RIVC:"\.sub$"

      Thanks!

      Thaum2u

        Topic Starter


        Rookie
        • No You!
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 11
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #10 on: September 01, 2021, 03:36:56 AM »
      Code: [Select]
      FOR /F "delims=" %%G IN ('dir "%%a"/OGN /B ^| findstr /RIVC:"\.srt$" /RIVC:"\.idx$" /RIVC:"\.sub$" /RIVC:"\.jpg$" /RIVC:"\.png$"') DO ECHO %%~nG

      Thanks Squashman!  I am not sure how to implement the code you suggested for me.  The way I did it results in a blank file.

      Here is my default routine (now updated with your more efficient code suggestions):


      :DEFAULT
      (
          echo - -  LIST GENERATED: %DATE%  - -
          FOR /F "delims=" %%a in ('dir /A:D /OGN /B') DO (
            echo.
            echo - - - - - - - -  %%a  - - - - - - - -
            dir "%%a"/OGN /B | findstr /RIVC:"\.srt$" /RIVC:"\.idx$" /RIVC:"\.sub$" /RIVC:"\.jpg$" /RIVC:"\.png$" /RIVC:"\.bat$"
          )
      )> %NAME%.TXT
      start notepad %NAME%.TXT


      How would you modify that to omit file extensions in the resulting text file?

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #11 on: September 01, 2021, 02:46:07 PM »
      My code:
      Code: [Select]
      FOR /F "delims=" %%G IN ('dir "%%a"/OGN /B ^| findstr /RIVC:"\.srt$" /RIVC:"\.idx$" /RIVC:"\.sub$" /RIVC:"\.jpg$" /RIVC:"\.png$"') DO ECHO %%~nG
      replaces this code:
      Code: [Select]
      dir "%%a"/OGN /B | findstr /RIVC:"\.srt$" /RIVC:"\.idx$" /RIVC:"\.sub$" /RIVC:"\.jpg$" /RIVC:"\.png$" /RIVC:"\.bat$"
      Could you please learn how to use CODE tags.  It would be much appreciated.


      Thaum2u

        Topic Starter


        Rookie
        • No You!
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 11
      Re: Delete Lines From Plain Text File If End Of Line Matches Set Values
      « Reply #12 on: September 01, 2021, 09:47:01 PM »
      Could you please learn how to use CODE tags.  It would be much appreciated.

      You are awesome!  Thank you so much!  And now I know how to use the code tag as well.