How can I get a file count in a specific folder?

100questions
100questions used Ask the Experts™
on
I would like a script that I can run which will produce an output file which numbers the file count in a specific folder.

For instance at this path:  C:\data\, I would like to run a script, not to be kept within the data folder, but externally, which would produce a text file which lists the amount of files found in the data folder.

If there are 2 files, I would like the output file to be produced, called files.txt, and within the files.txt file it should list the following:  2 files found, and the time stamp as well.

The files.txt should be created at C:\filecount\ folder.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Barry MolenwijkTechnical Support Specialist II

Commented:
$Filecount = (Get-Childitem C:\data -Recurse).Count
$Timestamp = Get-Date -Format "dd-MM-yyyy H:mm:ss"
$Message = "$($Timestamp) | $($Filecount) files found."

Does that work for you?
Barry MolenwijkTechnical Support Specialist II

Commented:
That would be PowerShell btw.
Barry MolenwijkTechnical Support Specialist II

Commented:
D'oh. Add one more line.

$Message | Out-File C:\filecount\files.txt -Append
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Plain batch. This script does not count recursively; if you want to count files in subfolders as well, simply add /s after the "dir" command. You can pass the path to the folder as first argument, by default it will use C:\Data.
@echo off
setlocal
set LogFile=C:\filecount\files.txt
if "%~1"=="" (set Folder=C:\Data) else (set Folder=%~1)
for /f %%a in ('dir /b /a:-d "%Folder%" 2^>NUL ^| find /c /v ""') do set FileCount=%%a
echo [%Date%][%Time%] %FileCount% files found in '%Folder%'.
>>"%LogFile%" echo [%Date%][%Time%] %FileCount% files found in '%Folder%'.

Open in new window

NVITEnd-user support
Commented:
Another batch example:
@echo off
for /f %%a in ('dir /a:-d c:\data ^| find /c /v ""') do (
  set ct=%%a
)
echo %ct% %date% %time% > C:\filecount\files.txt

Open in new window

Commented:
or from batch

for /f %%s in ('dir /a-d /a c:\data\*.* ^| find "File(s)"') do echo %%s files found at %date% %time%> D:\files.txt


Steve
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
NewVillageIT,
this will count the additional lines of the dir output (volume information, summary etc) as well, not only the files.

Steve Knight,
this will only work on English versions of Windows; the "dir" output is localized (and it will not count hidden files).

Commented:
oBdA ... pedantic on :-)  Yes can soon add /a to the line to get files including hidden.... and change the word File(s) to the relevant one for your lingo!
NVITEnd-user support

Commented:
oBdA,
Oops... This one.

@echo off
for /f %%a in ('dir /b /a:-d c:\data ^| find /c /v ""') do (
  echo %%a files found %date% %time% >> C:\filecount\files.txt
)

Open in new window

Author

Commented:
Thanks for all the assistance.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial