{"id":8103,"date":"2021-11-25T23:19:19","date_gmt":"2021-11-25T23:19:19","guid":{"rendered":"https:\/\/lgildv5i97.onrocket.site\/answers\/?post_type=question&#038;p=8103"},"modified":"2021-11-25T23:19:45","modified_gmt":"2021-11-25T23:19:45","slug":"solved-batch-file-to-find-the-last-occurrence-of-a-string","status":"publish","type":"question","link":"https:\/\/computing.net\/answers\/programming\/batch-file-to-find-the-last-occurrence-of-a-string\/30513.html","title":{"rendered":"Solved Batch File To Find The Last Occurrence Of a String?"},"content":{"rendered":"<p>I&#8217;m trying to set a variable and write to a file the value of the last occurrence of &#8216;time=&#8217;, 00:24:53.16, in this example file.<\/p>\n<p>&#8212;&#8212;&#8211; file: input.txt &#8212;&#8212;&#8211;<br \/>\nInput #0, mp3, from &#8216;test.mp3&#8217;:<br \/>\nMetadata:<br \/>\nencoder : Lavf57.51.102<br \/>\nDuration: 00:23:41.31, start: 0.025057, bitrate: 128 kb\/s<br \/>\nStream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb\/s<br \/>\nMetadata:<br \/>\nencoder : Lavc57.60<br \/>\nOutput #0, null, to &#8216;pipe:&#8217;:<br \/>\nMetadata:<br \/>\nencoder : Lavf57.51.102<br \/>\nStream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb\/s<br \/>\nMetadata:<br \/>\nencoder : Lavc57.60.101 pcm_s16le<br \/>\nStream mapping:<br \/>\nStream #0:0 -&gt; #0:0 (mp3 (native) -&gt; pcm_s16le (native))<br \/>\nPress [q] to stop, [?] for help<br \/>\nsize=N\/A time=00:03:29.65 bitrate=N\/A speed= 419x<br \/>\nsize=N\/A time=00:07:56.65 bitrate=N\/A speed= 477x<br \/>\nsize=N\/A time=00:12:25.43 bitrate=N\/A speed= 497x<br \/>\nsize=N\/A time=00:16:56.45 bitrate=N\/A speed= 508x<br \/>\nsize=N\/A time=00:21:22.56 bitrate=N\/A speed= 513x<br \/>\nsize=N\/A time=00:24:53.16 bitrate=N\/A speed= 515x<br \/>\nvideo:0kB audio:25720kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown<br \/>\n&#8212;&#8212;&#8211; end of file &#8212;&#8212;&#8212;&#8212;<\/p>\n<p>The trouble I&#8217;m having comes from the fact that all the &#8216;lines&#8217; starting with &#8216;size=&#8217; (except the last one) end with &lt;sp&gt;&lt;sp&gt;&lt;sp&gt;&lt;sp&gt;&lt;cr&gt;, no &lt;lf&gt;, so they are really part of a single line as far a parse-ability is concerned. The last &#8216;size=&#8217; line has a &lt;cr&gt;&lt;lf&gt;.<\/p>\n<p>So, the problem really boils down to extracting the last of several occurrences of a string in a line of unknown length, e.g.:<\/p>\n<p>size=N\/A time=00:03:29.65 bitrate=N\/A speed= 419x size=N\/A time=00:07:56.65 bitrate=N\/A speed= 477x size=N\/A time=00:12:25.43 bitrate=N\/A speed= 497x size=N\/A time=00:16:56.45 bitrate=N\/A speed= 508x size=N\/A time=00:21:22.56 bitrate=N\/A speed= 513x size=N\/A time=00:24:53.16 bitrate=N\/A speed= 515x<\/p>\n<p>In case it makes the solution any easier, the desired &#8216;time=&#8217; value will always be the last one in the file, and will always be the &#8216;largest&#8217;.<\/p>\n<p>The following batch file almost works, but it finds and processes the first &#8216;time=&#8217; value, not the last. It converts the value to seconds, strips the echo-added &lt;cr&gt;&lt;lf&gt;, sets an environment variable, and sends the result to a file. It works except on the wrong data. Is there some way to accomplish this in a pure Win batch file?<\/p>\n<p>@echo off<br \/>\nfor \/F &#8220;tokens=1-7 delims==:. &#8221; %%1 in (input.txt) do (if &#8220;%%1&#8243;==&#8221;size&#8221; call :findtime %%4 %%5 %%6 %%7)<br \/>\n:findtime<br \/>\necho:<br \/>\nset \/A s=%3 &amp; set \/A s=s+%2*60 &amp; set \/A s=s+%1*60*60<br \/>\necho:%1:%2:%3.%4 = %s%.%4 seconds<br \/>\nset dur=%s%.%4<br \/>\necho:%dur% = includes a CRLF<br \/>\n&lt;nul (set\/p z=%dur%)<br \/>\necho: = stripped CRLF, method1<br \/>\n(echo:|set \/p =&#8221;%dur%&#8221;)&gt;duration.txt %= strip CRLF then send to file for later use =%<br \/>\ntype duration.txt<br \/>\necho: = stripped CRLF, method2<br \/>\necho:<br \/>\npause<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"iawp_total_views":17},"question-category":[55],"question_tags":[],"class_list":["post-8103","question","type-question","status-publish","hentry","question-category-programming"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question\/8103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question"}],"about":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/types\/question"}],"author":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/comments?post=8103"}],"wp:attachment":[{"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/media?parent=8103"}],"wp:term":[{"taxonomy":"question-category","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question-category?post=8103"},{"taxonomy":"question_tags","embeddable":true,"href":"https:\/\/computing.net\/answers\/wp-json\/wp\/v2\/question_tags?post=8103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}