I'm trying to run a Powershell command or CMD command, that count all files in 1 folder and its subfolder. I got a script that count all files, but it does not find hidden files. The problem I found was in my network share there was a folder that said 0 files, but when I go into the folder there are a lot of files in there.
Is there a way to get those files into the count?
I have tried a few Powershell command and CMD commands.
Get-ChildItem -Recurse -File | Measure-Object | %{$_.Count}
I can only get it to count files Windows can see, if a folder says 0 files but there are files in there, it does not count it.
Use the force
PS> Get-ChildItem -Path c:\test | Measure-Object | Select-Object -ExpandProperty Count
16
PS> Get-ChildItem -Path c:\test -Force | Measure-Object | Select-Object -ExpandProperty Count
17
The -Force parameter on Get-ChildItem will force inclusion of hidden files
Related
I'm recursively counting total number of objects (files, folders, etc) to check folders vs their Amazon S3 backups.
When I use windows explorer on a folder (right click --> properties), I get a smaller number of total objects than what the following powershell code generates. Why?
Amazon S3 matches the count from Windows Explorer 100% of the time. Why is powershell giving a higher total number, and what is the likely difference (system files, hidden files, etc)? The total number of objects in these folders is routinely 77,000+.
folder_name; Get-ChildItem -Recurse | Measure-Object | %{$_.count}
I was unable to replicate.
When in file explorer, right-click the folder in question -> properties
Under the General tab, there's a section called Contains.
This lists both the Files and Folders as separate numbers.
In my example I have 19,267 Files, 1,163 Folders which is a total of 20,430 objects
When I run
Get-ChildItem -Path C:\folder -Recurse | measure | % Count
it returns 20430
When I run
Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $false} | measure | % count
it returns 19267
When I run
Get-ChildItem -Path C:\folder -Recurse | ?{$_.PSiscontainer -eq $true} | measure | % count
it returns 1163
Are you certain that you're counting both the files and folders when manually viewing the properties?
The discrepancy comes from Windows Explorer counting files, and separately, folders. Powershell (version 2.0 Build 6.1) is counting everything together. It seems -File and -Directory don't work in PowerShell V2.0.
I really want to be able to get a list as a .cvs or .txt output of just the number of files (recursively) from a large number of folders. Going through windows explorer is one by one, and I don't get this as an output that I can copy/paste.
To count the number of files and folders in separate variables, you can do
# create two variables for the count
[int64]$totalFolders, [int64]$totalFiles = 0
# loop over the folders in the path
Get-ChildItem -Path 'ThePath' -Recurse -Force -ErrorAction SilentlyContinue | ForEach-Object {
if ($_.PSIsContainer) { $totalFolders++ } else { $totalFiles++ }
}
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"
The -Force switch makes sure also hidden and system files are counted.
A probably faster alternative is to use robocopy:
$roboCount = robocopy 'ThePath' 'NoDestination' /L /E /BYTES
$totalFolders = #($roboCount -match 'New Dir').Count - 1 # the rootfolder is also counted
$totalFiles = #($roboCount -match 'New File').Count
# output the results
"Folders: $totalFolders`r`nFiles: $totalFiles"
I am looking for a PS script that checks for a certain file type(.err) in a folder's sub-folders (depth -1) and if it finds at least one file with the required file type, prints only the sub-folder's name, without file patch or file name, e.g.:
[root folder]
[subfolder1]-has .err in it
[subfolder2]-doesn't have .err in it
[subfolder3]-doesn't have .err in it
[subfolder4]-has .err in it
[subfolder5]-has .err in it
Output:
[subfolder1]
[subfolder4]
[subfolder5]
I'm not good at PowerShell, so I only found how to list subfolder names which has .err files in it as many times as it has files inside.
(Get-ChildItem -Path C:\root -Depth 1 -recurse -filter *.err).DirectoryName | echo
Okay, after direction from #mklement0 my suggestion would be,
(Get-ChildItem (C:\root + "\*\*") -Filter "*.err").Directory.Name | select -Unique
If I understand the question properly, this should do what you want:
Get-ChildItem -Path 'C:\Root' -Depth 1 -Recurse -Filter *.err -File |
Group-Object -Property DirectoryName |
ForEach-Object { ($_.Name -split '\\')[-1] }
It searches 1 level deep through the subfolders and if it finds files with extension .err (no matter how many files are in that folder), it outputs the subfolder name only once.
If you are on PowerShell version below 3.0, change the top line into
Get-ChildItem -Path 'C:\Root' -Depth 1 -Recurse -Filter *.err |
Where-Object { !$_.PSIsContainer } |
Update: Karthick Ganesan's answer is the simplest approach.
Try the following:
(Get-ChildItem -Depth 1 -Filter *.err).Directory.Name | Get-Unique | Select -Skip 1
For brevity, I've omitted the -Path argument and also the -File switch that limits matching to files, as it's fair to assume that you won't have any directories named *.err.
The use of -Depth implies the use of -Recurse, so the latter needn't be specified.
.Directory.Name outputs the matching files' directory names as an array (via member-access enumeration, PSv3+).
Get-Unique weeds out duplicates, which is necessary, because a given directory will be output multiple times if it contains multiple *.err files.
Select -Skip 1 (Select is a built-in alias for Select-Object) skips the first output object, because it represents the input directory itself (depth 0).
I have a .txt with the names of over 1000 files I want to delete. My .txt file does not have file paths. The files I want to delete are spread throughout multiple folders but they are all in the same drive. Is there any way to use powershell or command prompt to search for all files within my drive with the same name as what is listed in my .txt file and delete them?
Assuming you're PowerShell prompt is currently set at the root location from which you want to start your search and the file is in the same directory:
gc .\MyListOfFilesIWantToDelete.txt | %{gci $_ -Recurse | Remove-Item -WhatIf}
Note, you'll have to remove the -whatif
Or, let's say your file is somewhere else where you have PowerShell opened (eg: ~/Documents), and you want to scan your D: drive. This should work:
gc .\MyListOfFilesIWantToDelete.txt | %{gci D:\ -Filter $_ -Recurse -ErrorAction SilentlyContinue | Remove-Item -WhatIf}
Note I put SilentlyContinue. This is because you'll see a lot of red if you don't have access to folders in your search path.
Alternatively, you can load up a variable with your list of files..
$thesefiles = gc .\mylistoffilesiwanttodelete.txt
.. and use the Remove-Item cmdlet directly..
Remove-Item -Path D:\Folder -Include $thesefiles -Recurse -WhatIf
or in one swoop without loading a variable:
Remove-Item -Path D:\Folder -Include $(gc .\mylistoffilesiwanttodelete.txt) -Recurse -WhatIf
Again, I'm using -WhatIf for testing. Also, I've noticed different behaviors in the past with get-childitem on different versions of PowerShell. I tested these with 5.1
Change directory from following powershell command
Following command will allow you to delete .txt files in specific directory
Get-ChildItem C:\*.txt -file -r | remove-item
I have a folder named LOGFILES with a subfolder named LOGFILES_OLD.
But we need to assume that I don't know the contents of the folder.
What I'm trying to develop in PowerShell is a method to show a list of all files with a .log extension.
I have failed to get it with something I have found here:
Get-Item C:\LOGFILES -Recurse | Where-Object {$_.Extension -eq ".log"} | Format-List
Why don't you use :
Get-ChildItem "C:\LOGFILES" -include *.log -Recurse
I'm going around in circles trying to run a powershell script which returns the sub-folder count of sub-folders in my root directory.
E.g. Root directory C:\temp
Subfolders are C:\temp\1 and C:\temp\2 and each have x subfolders
I want to see the count of subfolders in C:\temp\1 and C:\temp\2 and if they are under C:\temp\1 or C:\temp\2.
So far I can only get a total subfolder count.
Pretty simple to do:
Get-ChildItem C:\Temp -Directory `
| Select-Object #{n='FullName';e={$_.FullName}},#{n='SubFolderCount';e={(Get-ChildItem $_.FullName -Recurse -Directory).Count}} `
| Format-Table -Autosize;