File Counts - Multiple Directories

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,
bnorrisatnortechsolutionsAsked:
Who is Participating?
 
soostibiCommented:
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

Open in new window

0
 
soostibiCommented:
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

Open in new window

0
 
bnorrisatnortechsolutionsAuthor Commented:
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
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
soostibiCommented:
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
 
bnorrisatnortechsolutionsAuthor Commented:
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
 
bnorrisatnortechsolutionsAuthor Commented:
Thank you for your assistance!!  You have saved me many headaches!!
0
 
bnorrisatnortechsolutionsAuthor Commented:
Would it be possible to add a filter to only count the file with today's date?
0
 
soostibiCommented:
I made a funtion:
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
}

Open in new window


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

Open in new window

0
 
bnorrisatnortechsolutionsAuthor Commented:
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
 
soostibiCommented:
Well, it shoud give what you requested. Could you copy here exactly how you called the function and the resulting file?
0
 
bnorrisatnortechsolutionsAuthor Commented:
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
 
bnorrisatnortechsolutionsAuthor Commented:
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
 
soostibiCommented:
So you mean another switch for the function, that filters not files of today but files of last hour?
0
 
bnorrisatnortechsolutionsAuthor Commented:
I need files for today, but also are older than one hour.
0
 
soostibiCommented:
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  
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.