11
3
I tried to make a simple thing with PowerShell, find files which take most space on the drive. I used ls
+ sort
and... it took forever for me.
Sometimes I use far manager and compared with PowerShell it looks much faster and stable.
Ok, it is based on the .NET, but .NET is not so slow. I expect to see something lightweight and fast! It is console!
Another thing, I would like to have something like IEnumerable
in PowerShell to see results immediately. Is it possible to achieve? It might help a bit while expecting results cause sometimes I think it just hang out.
EDIT
I'm doing something like that
ls -Recurse -ErrorAction SilentlyContinue | sort -Property Size | select -First 10
And I guess it might take DAYS.
EDIT
Just to compare.
C# code took for me about 2 min. For sure it is not ideal and didn't process all files, but it processed at least >95%.
void Main()
{
GetFilesSize(@"C:\").OrderByDescending(x => x).Take(10).ToList();
}
public IEnumerable<long> GetFilesSize(string directory)
{
var accessDenied = false;
var dirList = new string[0];
try
{
dirList = Directory.GetDirectories(directory);
}
catch{
accessDenied = true;
}
if(accessDenied) yield break;
foreach (var dir in dirList)
{
foreach (var size in GetFilesSize(dir))
{
yield return size;
}
}
foreach (var fileName in Directory.GetFiles(directory))
{
if(fileName.Length>=260) continue;
yield return new FileInfo(fileName).Length;
}
}
5"Another thing..." No, please, no! Do not ask a second question, which will generate additional/different/unrelated responses, in the same question. Just create a new SuperUser question. – TOOGAM – 2016-05-13T05:41:35.037
1would be very useful to see your code which is "so slow" because maybe it's not PowerShell that's slow, rather your code! – SimonS – 2016-05-13T08:21:25.510
2Welcome to [su]! Please try and ask 1 question at a time (otherwise your question will be closed as too broad). – DavidPostill – 2016-05-13T13:16:08.660
@Ramhound If you read my comments careful, you can see that I complain about powershell performance, not .net. .NET is mentioned as base of powershell which works faster. Not sure about total amount of files, I'm trying to scan the whole drive. So I guess there thousands files. – Neir0 – 2016-05-13T22:57:51.317
@Ramhound I calculated: 556458 – Neir0 – 2016-05-13T23:03:20.647