How do I count the files in a specific folder and store the filenames in an array?
There are a number of problems with your code:
You need to enabledelayedexpansion if you are going to use it later.
You haven't initialised
%%~nxf should be
Your for /l command has the wrong syntax (it should be
Here is a corrected batch file (test.cmd):
ECHO ### Checking the number of files under %1 ###
for %%x in (%1\pdf*.*) do (
set /a numFiles+=1
ECHO ### Number of files found: %numFiles%
set /a index=%numFiles%-1
for /L %%i in (0,1,%index%) do (
Example usage and output:
> dir *.pdf
Volume in drive F is Expansion
Volume Serial Number is 3656-BB63
Directory of F:\test
21/02/2017 22:53 0 pdf01.pdf
21/02/2017 22:53 0 pdf02.pdf
21/02/2017 22:53 0 pdf03.pdf
3 File(s) 0 bytes
0 Dir(s) 1,701,266,092,032 bytes free
> test .
### Checking the number of files under . ###
### Number of files found: 3
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- enabledelayedexpansion - Delayed Expansion will cause variables to be expanded at execution time rather than at parse time.
- for - Conditionally perform a command on several files.
- for /l - Conditionally perform a command for a range of numbers.
- parameters - A command line argument (or parameter) is any value passed into a batch script.