UDM - forfiles Statement - Generic Character Substitution into Variables

Overview

In order to capture the parts of a file name string specified to the forfiles statement, values represented by the * and ? generic characters can be stored in variables.

Up to 10 substitutions can be made (that is, 10 tokens stored into variables). Substitutions can be made to a variable for forfiles - $(_file.wildcardn) - where n is an integer between 0 and 9.

Wildcard Expansion Behavior

Wildcard expansion behavior is as follows:

  • All ? wildcards must be satisfied.
  • Expansion is performed left to right.
  • If a * wildcard is encountered, it will expand as far as it can without infringing on the guaranteed expansion of ? wildcards.
  • Secondary * wildcards in a wildcard string (for example, ***, *?*?*, and so on) will expand to zero length strings.

Example 1

  1. A generic file pattern of A?C.txt applied to ABC.txt will return B as the value of $(_file.wildcard0) variable.
  2. A generic file pattern of A?C*.txt applied to ABCDEF,txt will return B as the value of $(_file.wildcard0) and DEF as the value of $(_file.wildcard1).
  3. A generic file pattern of ??*.txt applied to ABCDEF,txt will return A as the value of $(_file.wildcard0), and B as the value of $(_file.wildcard1), and CDEF as the value of $(_file.wildcard1).
     

If c:\data contains the following files:

2015-CLIENTA-05DATA.xml
2015-CLIENTB-10WIDGETS.xml

The following code:

cd src=c:\data
forfiles src=2015-*-??*.xml
 echo "FILE Name = $(_file)"
 echo "WC 1 = $(_file.wildcard0)"
 echo "WC 2 = $(_file.wildcard1)"
 echo "WC 3 = $(_file.wildcard2)"
 echo "WC 4 = $(_file.wildcard3)"
end


Will result in:

FILE Name = 2015-CLIENTA-05DATA.xml
WC1 = CLIENA
WC2 = 0
WC3 = 5
WC4 = DATA
FILE Name = 2015-CLIENTB-10WIDGETS.xml
WC1 = CLIENB
WC2 = 1
WC3 = 0
WC4 = WIDGETS


Example 2

Given a file name of 0123456789abc.xml:

A pattern of *???.xml would yield:

$(_file.wildcard0) -> 0123456789
$(_file.wildcard1) -> a
$(_file.wildcard2) -> b
$(_file.wildcard3) -> c



A pattern of ???*.xml would yield:

$(_file.wildcard0) -> 0
$(_file.wildcard1) -> 1
$(_file.wildcard2) -> 2
$(_file.wildcard3) -> 3456789abc



A pattern of ?*?.xml would yield:

$(_file.wildcard0) -> 0
$(_file.wildcard1) -> 123456789ab
$(_file.wildcard2) -> c



A pattern of ?*?*?*?.xml would yield:

$(_file.wildcard0) -> 0
$(_file.wildcard1) -> 123456789
$(_file.wildcard2) -> a
$(_file.wildcard3) ->
$(_file.wildcard4) -> b
$(_file.wildcard5) ->
$(_file.wildcard6) -> c