0

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 :

  1. CDEP_0003.pdf
  2. CIM_0001.pdf
  3. 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..)

Nathan30
  • 689
  • 2
  • 8
  • 29

2 Answers2

1

Simply loop through .*0000\.pdf$ to .*9999\.pdf$ using another answer on how to loop through files matching wildcard in batch file

Webber
  • 4,672
  • 4
  • 29
  • 38
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
)