Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a batch script that uses a for loop to iterate through files in the current directory, then convert them to mp4 with ffmpeg into the parent directory. Basically I just copy all the files I want to convert into a directory called "avi" (the sub directory) and run the .bat script. The thing is, I've got quite a few files in the parent directory. When I create new avi files, they're named "capture-#.avi" where # is a number starting at 1. The converted files use the same naming scheme, but it's sequential. Every time I create a new set of avi files, the # starts back over at 1, so when I copy them into the avi directory and run the batch script it tries to overwrite the existing mp4 files in the parent directory. Is there a way to have the batch script get the filename of the most recent (highest capture number) from the parent directory and use that to dynamically name the output files, so that it doesn't matter what the name of the avi file is, when it's converted it will come out as "capture-#.mp4", with the numbers continuing from what exists in the parent directory?

For example, in the parent dir, I have 30 files, the most recent of which is obviously, "capture-30.mp4". I copy two avi files into the avi directory, named "capture-1.avi" and "capture-2.avi", can I make it so that the batch script looks in the parent directory and sees the capture-30.mp4 file, and renames the avi files (as they're converted) to capture-31.mp4 and capture-32.mp4 automatically?

I tried doing this:

for %I in (../*.mp4) do SET _file=%I

But the _file variable got set to capture_9.mp4, presumably because the sorting system isn't a natural sorting, so it considers 9 to be higher than 30 (because 9 > 3). I tried wrapping the whole for loop in parentheses and adding | sort at the end, but it didn't change anything.

share|improve this question

I would use some cache file which holds last converted number. When running script reads this number and uses it as index. In loop you should also check if filename in parent directory exists. While it is so - try to add 1 to the counter. After finishing the loop save the index to the file for later use.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .