I am not very familiar with Windows CMD scripts and I need to write one that will check the number of files in a specific folder and store the filenames found in variables (maybe an array). Here is what I have (%1 is the folder I am getting as a parameter):

ECHO ### Checking the number of files under %1 ###
for %%x in (%1\pdf*.*) do (
 set file[!numFiles!]=%%~nxf
 set /a numFiles+=1
ECHO ### Number of files found: %numFiles%

for /L %%i in (0,1,2,3,4) do (
   echo !file[%%i]!

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:

  1. You need to enabledelayedexpansion if you are going to use it later.

  2. You haven't initialised numFiles.

  3. %%~nxf should be %%~nfx.

  4. Your for /l command has the wrong syntax (it should be start,step,end).

Here is a corrected batch file (test.cmd):

@echo off
setlocal enabledelayedexpansion
ECHO ### Checking the number of files under %1 ###
set numFiles=0
for %%x in (%1\pdf*.*) do (
  set file[!numFiles!]=%%~nfx
  set /a numFiles+=1
ECHO ### Number of files found: %numFiles%

set /a index=%numFiles%-1
for /L %%i in (0,1,%index%) do (
  echo !file[%%i]!

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

Further Reading

  • 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.
  • Thanks! Almost perfect. I just forgot mention that I need to grab only the filename (without the path)... – RonaldoMaia Feb 22 '17 at 14:06
  • @RonaldoMaia Replace %%~nfx with %%~nxx – DavidPostill Feb 22 '17 at 14:09
  • @RonaldoMaia Do you need more help? If this answered your question, please don't forget to accept the answer. – DavidPostill Feb 22 '17 at 14:10

To get a list of filenames:

dir /b

To get number of files:

dir /b /a-d | find /v /c "?"

To store list of files in a variable 'files' (comma delimited CSV):

set files=""&for /f "delims=" %i in ('dir /b') do set files=!files!,"%i"
set files=!files:~3!

The above commands are for current directory. For different directory, suffix the 'dir' command with the directory name, eg. ".." or "c:\" etc.

Tested on Win 10

  • Could you please explain what exactly your find part is looking for and what it does? – PeterCo Nov 18 '19 at 13:06
  • It looks for files without invalid filenames (ie. files with valid filenames), and displays the count; resulting in number of files being displayed. – Zimba Nov 23 '19 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.