Search code examples
stringbatch-filefile-read

Pattern match and string manipulation


I'm making a file monitor for a folder where I download subtitles. So far, it works like this:

Look for new .rar files in the folder.

If found, extract the subtitles and delete the .rar file

If a single .srt file was extracted, save the file name to a variable.


Now, I'm clueless about how to achieve the next (and final) part of the script:

I want to find a pattern based on the way subtitles are named.

Let's say, the subtitles file can be something like this:

SomeShow.1x03.stuff.srt

some_show s01e03-stuff.srt

some show 1-03 stuff.srt

etc.

I want to get something like: SomeShow 1 3 and based on that, start the video with the name that matches that pattern, which I guess would be a matter of reversing the process that was used to get the Show, season and episode based on the name of the .srt file.

Is this possible at all? It'd be really simple stuff in most languages, but I really need this to be a .bat and I'm clueless about how to approach this... so far all I've managed to do is to remove the extension from the variable.

Thanks in advance.


Solution

  • Batch files are Turing complete - you can do anything in them, but it is usually not wise to go to extremes. You might be able to package a sed or grep or your own binary alongside your .bat file for a good compromise between batchiness and function. If you can assume a suitable operating system, you will have Powershell installed and go that route.

    You should recognize that the task is not exactly defined and that the "solution" may need some tweaking and be never robust enough.

    For this reason, the richer language you can pick, the further you will get.