Solved

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

Posted on 2014-11-25
10
224 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 84

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 84

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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