Solved

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

Posted on 2014-11-25
10
227 Views
Last Modified: 2014-11-27
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.
0
Comment
Question by:100questions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:Barry Molenwijk
ID: 40465016
$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?
0
 
LVL 3

Expert Comment

by:Barry Molenwijk
ID: 40465018
That would be PowerShell btw.
0
 
LVL 3

Expert Comment

by:Barry Molenwijk
ID: 40465027
D'oh. Add one more line.

$Message | Out-File C:\filecount\files.txt -Append
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 85

Accepted Solution

by:
oBdA earned 167 total points
ID: 40465115
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

0
 
LVL 24

Assisted Solution

by:NVIT
NVIT earned 167 total points
ID: 40465136
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

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 166 total points
ID: 40465137
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
0
 
LVL 85

Expert Comment

by:oBdA
ID: 40465169
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).
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40465203
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!
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40465569
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

0
 

Author Closing Comment

by:100questions
ID: 40468913
Thanks for all the assistance.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question