?
Solved

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

Posted on 2014-11-25
10
Medium Priority
?
228 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
Need protection from advanced malware attacks?

Look no further than WatchGuard's Total Security Suite, providing defense in depth against today's most headlining attacks like Petya 2.0 and WannaCry. Keep your organization out of the news with protection from known and unknown threats.

 
LVL 85

Accepted Solution

by:
oBdA earned 668 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 25

Assisted Solution

by:NVIT
NVIT earned 668 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 664 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 25

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

800 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