in reply to How can I use printf FORMAT strings for (Win32) shell globs?

Many thanks for the really useful suggestions, folks.. I *knew* there had to be a better way... and, as the question involved regex, I expected *lots* of options ;)

I guess I should've explained context a bit better.. and sure, example runs & data always help.. but I simply thought the question was one of 'translation' and 'substitution', so I didn't include test data and such this time around.

By way of background... I'm basically putting an 'intelligent wrapper' around 'ffmpeg', the video manipulation tool... and one of the functions I'm dealing with is the creation/usage of a sequence of frames from a video. The normal syntax for extracting frame images uses an argument like 'Img%04d.png' so that the filename is a consistent length and ffmpeg can output/input a sequence of images in-order - this is why the spec. is zero-filled - and the wrapper determines the width ('4' in this example) after having counted all the frames in the video. This also explains why I don't need to consider negative numbers, etc... and the 'fixed-width, zero-filled' format is a requirement for some subsequent processing after this wrapper creates the image files.

With Win32's CMD.exe (or 4NT.exe or TCC.exe, which I'm generally using), the shell gets confused with that '%' (even when escaped, it seems), so the string that's used on the command line is `Img%04d.png` (with the backprimes)... and that seems to work with Linux and Win32 CLI shells Ok...

The wrapper does some checks with the string before it calls ffmpeg to see if there are any pre-existing, conflicting files... but I admit I'm slack in that I only considered using glob(), as I thought (unjustifiably?) it might be the simplest method and it might work better than using something like File::Find, particularly when checking for 10s of 1000s of (frame image) files per video... and I *did* forget that File::Find IS core in Perl these days... Ooop!

Anyway, the original code was purposely 'ordinary' as I'm *still* not so flash with regex and I wanted to be sure I properly understood what each part of the code was doing.

Certainly, there are a lot of *most helpful* postings here now, Fanx!... so I can try some more elegant methods.. and see which one best suits what I'm trying to do.

Fanx! again, everyone...

  • Comment on Re: How can I use printf FORMAT strings for (Win32) shell globs?

Replies are listed 'Best First'.
Re^2: How can I use printf FORMAT strings for (Win32) shell globs?
by soonix (Chancellor) on Jul 18, 2017 at 09:34 UTC
    With Win32's CMD.exe (or 4NT.exe or TCC.exe, which I'm generally using), the shell gets confused with that '%' (even when escaped, it seems)
    Did you take into account that CMD escapes % differently from other characters? See e.g. on SS64 under "Escaping Percents"

      Oh, yes... ...as does printf() (when using ' or ")... and different versions of 4NT/TCC/TCC-LE/etc, which also use different multi-command separators which may also include '%' characters...

      Anyway, with all this escaping, the (originally) simple approach is getting more and more complex to read and understand... particularly when I come back to this in a year or sumfin'...(!) ...and I want to try and avoid any (somehow) avoidable complexity for something as simple as 'globbing' a list of files(!)

      ...but point well-taken... Fanx!

Re^2: How can I use printf FORMAT strings for (Win32) shell globs?
by haukex (Archbishop) on Jul 19, 2017 at 06:37 UTC
    ffmpeg

    Ah, well that narrows it down. From the documentation:

    The syntax foo-%03d.jpeg specifies to use a decimal number composed of three digits padded with zeroes to express the sequence number. It is the same syntax supported by the C printf function, but only formats accepting a normal integer are suitable.

    When importing an image sequence, -i also supports expanding shell-like wildcard patterns (globbing) internally, by selecting the image2-specific -pattern_type glob option.

    And the "image2" documentation goes into a little more detail:

    A sequence pattern may contain the string "%d" or "%0Nd", which specifies the position of the characters representing a sequential number in each filename matched by the pattern. If the form "%d0Nd" is used, the string representing the number in each filename is 0-padded and N is the total number of 0-padded digits representing the number. The literal character ’%’ can be specified in the pattern with the string "%%".

    And skimming the rest of the documentation, it seems these are pretty much the only two patterns you'll have to worry about, which makes the conversion much simpler - they can all be replaced by regexes of the form \d+ or \d{N}.

Re^2: How can I use printf FORMAT strings for (Win32) shell globs?
by RonW (Parson) on Jul 20, 2017 at 20:33 UTC

    Have you looked at FFmpeg on CPAN? There are other ffmpeg releated modules on CPAN, as well.

    (Note that the module name is case sensitive: FFmpeg is a module. ffmpeg is a program written in Perl.)