Solved

# File Counts - Multiple Directories

Posted on 2011-05-13
Medium Priority
390 Views
I need a script that would return the file count of directories listed in a sepeate text file.  I would like this Exported to a CSV file with the directory name and count in the CSV file.

For Example:

DirectoryName,Count,
C:\Temp,11,
C:\,34,
0
• 8
• 7

LVL 13

Expert Comment

ID: 35754778
Here you are:
$rootpath = "c:\ee" dir$rootpath | ?{$_.psiscontainer} |%{$i=0
dir $_.fullname -Recurse -ErrorAction silentlycontinue -Force | ?{!$_.psiscontainer} | %{$i++} New-Object -TypeName PSObject -Property @{ DirectoryName =$_.fullname
Count = $i } } | Select-Object DirectoryName, Count | Export-Csv c:\yourfile.csv -NoTypeInformation  0 Author Comment ID: 35754866 soostibi: That works for every directory in the$rootPath, but I need it to be able to use a DirList.txt file and check the directories listed in their.  Please keep in mind that these directories could be directories on different servers.
0

LVL 13

Expert Comment

ID: 35754970
OK, one more question, let's say, that you have this directory structure:

MainDir                     10 files
SubDir1                  5 files
Subdir2                  2 files

What number would you like to see at MainDir? 10 or 17? So would you like to have a sum of the files in the directory and its subdirectories or only the number of the direct members?
0

Author Comment

ID: 35755033
I don't need it that far in the weeds.  I have like 10 directories that I need to compare the counts with at the root level, not any of their sub-directories.  So, to answer your question - I would lke to see the "10".  Thanks for asking.
0

LVL 13

Accepted Solution

soostibi earned 2000 total points
ID: 35755185
Then:
$rootpaths = get-content c:\DirList.txt$rootpaths | % {
dir $_ -ErrorAction silentlycontinue -Force | ?{!$_.psiscontainer} | % {$i=0} {$i++}
New-Object -TypeName PSObject -Property @{
DirectoryName = $_ Count =$i
}
} | Select-Object DirectoryName, Count | Export-Csv c:\yourfile.csv -NoTypeInformation

0

Author Closing Comment

ID: 35755436
0

Author Comment

ID: 35769707
Would it be possible to add a filter to only count the file with today's date?
0

LVL 13

Expert Comment

ID: 35769886
function count-files ($where = (get-content c:\dirlist.txt),$todayonly = $true){$where | % {
dir $_ -ErrorAction silentlycontinue -Force | ?{!$_.psiscontainer -and (!$todayonly -or ((get-date) -$_.lastwritetime).totaldays -le 1)} | % {$i=0} {$i++}
New-Object -TypeName PSObject -Property @{
DirectoryName = $_ Count =$i
}
} | Select-Object DirectoryName, Count | Export-Csv c:\yourfile.csv -NoTypeInformation
}


After you define (run) it, you can use it by calling it:
count-files -where c:\ee -todayonly $false # gives the statistics of c:\ee count-files -todayonly$false # gives the statistics of the folders listed in c:\dirlist.txt

count-files # gives only the statistics of the fieles modified today in the folders listed in c:\dirlist.txt

0

Author Comment

ID: 35771333
I implemented what you provided and in the csv it returns the actual csv name and number of files.  I need it to return the directory listed in the dirlist.txt and number o ffiles with todays date.

I am willing to open another question and reward you points for your time if you want.
0

LVL 13

Expert Comment

ID: 35772000
Well, it shoud give what you requested. Could you copy here exactly how you called the function and the resulting file?
0

Author Comment

ID: 35772070
I got it to work.  I actually had to read what you were trying to tell me.  It was user error. I posted another question on EE if you are interested.  Thanks for all of your help.
0

Author Comment

ID: 35902328
Hey Soostibi -

Can you assist me with modifying this script to count the number of files created over a hour ago in the directories listed?  Basically only files with todays date and older than one hour.  I will open another ticket if you want to credit you some points.
0

LVL 13

Expert Comment

ID: 35909846
So you mean another switch for the function, that filters not files of today but files of last hour?
0

Author Comment

ID: 35912224
I need files for today, but also are older than one hour.
0

LVL 13

Expert Comment

ID: 35914366
I hope this is what you need, I added a new column with the number of files modified within the last hour.
function count-files ($where = (get-content c:\dirlist.txt),$todayonly = $true){$where | % {
dir $_ -ErrorAction silentlycontinue -Force | ?{!$_.psiscontainer -and (!$todayonly -or ((get-date) -$_.lastwritetime).totaldays -le 1)} |
ForEach-Object {$i=$lh=0} {
$i++ if(((get-date) -$_.lastwritetime).totalhours -le 1){$lh++} } New-Object -TypeName PSObject -Property @{ DirectoryName =$_
Count = $i LastHourCount =$lh
}
} | Select-Object DirectoryName, Count, LastHourCount | Export-Csv c:\yourfile.csv -NoTypeInformation
}

0

## Featured Post

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).
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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…
Loops Section Overview
###### Suggested Courses
Course of the Month15 days, 16 hours left to enroll