title: Fastest way to check for the highest filename in sequence with Batch file?
body: <p>I'm attempting to use a batch file to keep all files in a particular folder numbered sequentially (0,1,2,...). To do this I first require knowing what the highest number in this sequence is so I can append the non-sequentially named files to the end of the list. For example, I may have the following files:</p>
  
  <p>0.png</p>
  
  <p>1.jpg</p>
  
  <p>2.jpg</p>
  
  <p>3.png</p>
  
  <p>file_51.jpg</p>
  
  <p>pictureabc.png</p>
  
  <p>This would return a result of 3 as that is the last filename in sequence. I would then be able to find the remaining files (not sure the best way to do this either) and number them starting at 4.</p>
  
  <p>The only solution I have come up with so far is using an infinite goto loop that stops when no filename including that number exists.</p>
  
  <pre><code>set count=0
  :loop
  if exist %count%.* set /a count+=1 &amp; goto loop
  </code></pre>
  
  <p>This solution is highly inefficient however and takes almost a minute for 100 thousand images. I'm looking for a solution that will be able to achieve this in under 10 seconds if possible.</p>