Solved

# File Counts - Multiple Directories

Posted on 2011-05-13
374 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

LVL 13

Expert Comment

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 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

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

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

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

0

Author Comment

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

LVL 13

Expert Comment

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

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

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

Author Comment

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

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

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

Author Comment

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

LVL 13

Expert Comment

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

A commonly asked question on the site recently has been regarding how do I share the GAL between different forests? I want a list of Contacts representing all the users in a remote domain, How do i get them? It's a problem I was posed with a litt…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…