I need to do a bat script to process file one by one, according to a number in the filename. Here is an example of what I can get :
CDEP_0003.pdf
CIM_0001.pdf
ESCALE_0002.pdf
It's possible to loop and process each of those files in an ordered way using regex or something like this to exclude the number and use it as a loop index ? (0001, 0002, 0003 etc..)
Sign up to request clarification or add additional context in comments.
Comments
1
You could
build an array of used numbers by first iterating through the files, or
use a counting loop (for /l) from 10000 to 19999 to have the
leading zeros and take the last 4 digits..
Provided there is only one underscore preceding the number:
@Echo off
Pushd "X:\folder\to\start"
::clear array
for /f "delims==" %%A in ('Set No[ 2^>Nul') do Set "%%A="
:: fill array
for /f "tokens=2delims=_." %%A in (
'Dir /B "*_????.pdf" ^| findstr "^.*_[0-9][0-9][0-9][0-9]\.pdf$"'
) Do Set /A "No[%%A]=%%A"
:: process existing numbers
for /f "tokens=2 delims=[]" %%A in ('Set No[ 2^>Nul') do (
Rem dir "*_%%A.pdf" or whatever you want to do
)