Solved

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

Posted on 2014-11-25
10
214 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
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Expert Comment

by:Barry Molenwijk
Comment Utility
$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
Comment Utility
That would be PowerShell btw.
0
 
LVL 3

Expert Comment

by:Barry Molenwijk
Comment Utility
D'oh. Add one more line.

$Message | Out-File C:\filecount\files.txt -Append
0
 
LVL 82

Accepted Solution

by:
oBdA earned 167 total points
Comment Utility
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 23

Assisted Solution

by:NVIT
NVIT earned 167 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 166 total points
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
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
Comment Utility
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 23

Expert Comment

by:NVIT
Comment Utility
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
Comment Utility
Thanks for all the assistance.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now