6

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]!
)
DavidPostill
  • 153,128
  • 77
  • 353
  • 394
RonaldoMaia
  • 61
  • 1
  • 1
  • 2
  • Why not use a couple of `dir` commands? One normal to parse out the count from the summary lines, and one with `/b` to get the file list. – AFH Feb 21 '17 at 23:03
  • :: backslash-n refers to pressing the Enter key \n echo ### Number of files found: \n dir %1 | FIND /C "/" \n :: This does not store stuff in variables, so it is only a partial answer. Really you're basically asking for two things, so this could be two questions. – TOOGAM Feb 22 '17 at 02:09
  • [How can I check the size of a folder from the Windows command line?](https://superuser.com/q/837016/241386), [Count files in a folder and subfolders from the command line](https://superuser.com/q/942108/241386), [Total number of files on an NTFS volume?](https://superuser.com/q/273122/241386) – phuclv Mar 13 '19 at 02:23

2 Answers2

2

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]!
  )
endlocal

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
F:\test\pdf01.pdf
F:\test\pdf02.pdf
F:\test\pdf03.pdf

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.
DavidPostill
  • 153,128
  • 77
  • 353
  • 394
0

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

Zimba
  • 1,051
  • 11
  • 15
  • 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