how to check if a specific file extension exists in a folder using powershell?
Solution 1
Try this:
function Get-ExtensionCount {
param(
$Root = "C:\Root\",
$FileType = @(".sln", ".designer.vb"),
$Outfile = "C:\Root\rootext.txt"
)
$output = @()
Foreach ($type in $FileType) {
$files = Get-ChildItem $Root -Filter *$type -Recurse | ? { !$_.PSIsContainer }
$output += "$type ---> $($files.Count) files"
foreach ($file in $files) {
$output += $file.FullName
}
}
$output | Set-Content $Outfile
}
I turned it into a function with your values as default parameter-values. Call it by using
Get-ExtensionCount #for default values
Or
Get-ExtensionCount -Root "d:\test" -FileType ".txt", ".bmp" -Outfile "D:\output.txt"
Output saved to the file ex:
.txt ---> 3 files
D:\Test\as.txt
D:\Test\ddddd.txt
D:\Test\sss.txt
.bmp ---> 2 files
D:\Test\dsadsa.bmp
D:\Test\New Bitmap Image.bmp
To get the all the filecounts at the start, try:
function Get-ExtensionCount {
param(
$Root = "C:\Root\",
$FileType = @(".sln", ".designer.vb"),
$Outfile = "C:\Root\rootext.txt"
)
#Filecount per type
$header = @()
#All the filepaths
$filelist = @()
Foreach ($type in $FileType) {
$files = Get-ChildItem $Root -Filter *$type -Recurse | ? { !$_.PSIsContainer }
$header += "$type ---> $($files.Count) files"
foreach ($file in $files) {
$filelist += $file.FullName
}
}
#Collect to single output
$output = @($header, $filelist)
$output | Set-Content $Outfile
}
Solution 2
Here's a one-liner to determine if at least one file with extension .txt or .ps1 exists in the directory $OutputPath:
(Get-ChildItem -Path $OutputPath -force | Where-Object Extension -in ('.txt','.ps1') | Measure-Object).Count
Explanation: the command tells you the number of files in the specified directory matching any of the listed extensions. You can append -ne 0
to the end, which returns true or false to be used in an if block.
ashish g
Updated on July 02, 2022Comments
-
ashish g almost 2 years
I have a root directory that consists of many folders and sub folders. I need to check whether a particular file like *.sln or *.designer.vb exists in the folders or subfolders and output the result in a text file.
For Eg:
$root = "C:\Root\" $FileType = ".sln",".designer.vb"
the text file will have result somewhat like below:
.sln ---> 2 files .sln files path ----> c:\Root\Application1\subfolder1\Test.sln c:\Root\Application2\subfolder1\Test2.sln
Any help will be highly appreciated!
Regards, Ashish
-
Frode F. over 11 yearsFilter doesn't support array input(multiple fileext.)
-
Lance U. Matthews over 11 yearsYes, just realizing that now.
-
Frode F. over 11 yearsYou could however modify it to use
if ($FileType -contains $group.Name) { ...
in you foreach loop instead of the Filter -
ashish g over 11 yearsHi Graimer, Thanks for your response. After I call this function through the command prompt I get: cmdlet set-content at command pipeline pos 1. supply values for the parameters.. and the parameters are endless :(
-
Frode F. over 11 yearsThis happends when you call exactly
Get-ExtensionCount
? Did you copy the function 100%? Especially the last line.$output | Set-Content $Outfile
. I think you forgot to include$Outfile
at the end. Script works on PS2.0 and PS3.0. You could also try to switch the line with$output | Set-Content -Path $Outfile
but it shouldn't matter. -
Lance U. Matthews over 11 yearsThat would work, but then
Group-Object
would end up processing files and creating groups that might ultimately end up being ignored. Instead, I changed the call toGet-ChildItem
to retrieve all files and the filtering is now performed inside ofWhere-Object
. I made a slight change to the format of$FileType
to facilitate this. -
ashish g over 11 yearsYes.. I had missed that :-) But in the output I get .sln--->44 files , .suo ---> files.. but none show the files and file paths.
-
Frode F. over 11 yearsoh, sorry. forgot about one PS3.0 feature. see updated answer
-
ashish g over 11 yearsis there a way I can print the output in the manner where all the types of files and their counts come in first and all the file paths come in the last.. .sln--->34 files .suo--->120 files then comes the filepaths
-
Frode F. over 11 yearsUpdated answer. See the last codesample (untested, don't have PS here). :-)
-
Ste over 3 yearsBest answer for me on here +1. In case anyone may want the regex version, here's that.
(Get-ChildItem -Path $OutputPath -force | Where-Object Extension -match ('\.jpe?g') | Measure-Object).Count
Which will match either jpeg or jpg.