Count and list total files per folder at remote site

Advertisement

A0n1us
Joined:
Posts:
2

Count and list total files per folder at remote site

I know this has been asked before and I have searched online and read some of the related articles here on the forum for counting files. I have tried most of them and end with some error or the other. The most common error is
Cannot Find an overload for EnumerateRemoteFiles and the argument count: "2"
I saw another article where this error was brought up and Martin addressed it, but I could not quite make out what he was asking to implement and the person who asked the question, did not comment on that final help provided :(

I tried different iterations of the script and this one gets me a file count, but not in the format that I want. The output comes out like this – and count is correct.
Number of files in /Test/ is 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I would like to get an output in this format:
Test/ABC 2
Test/XVZ 6
and so on. (if that is not possible, then the total count of all files is enough)
Thank you for your input.

Here is the script that I tried, which gave the above output:
#Load WinSCP assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Session settings
$sessionOptions = New-Object WinSCP.SessionOptions -property{
    # all the usual stuff
}
# Create session and connect
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
 
# Remote directory to count files in
$remoteDirectory = "/Test/"
 
# Enumerate files and count them recursively
$fileCount =
    $session.EnumerateRemoteFiles(
        $remoteDirectory, "*.*", [WinSCP.EnumerationOptions]::AllDirectories).Count
 
# Close session
$session.Dispose()
 
# Output the count
Write-Host "Number of files in $remoteDirectory is $fileCount"

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,541
Location:
Prague, Czechia

Re: Count and list total files per folder at remote site

It's not that trivial to do. And I'm no PowerShell expert.
Do you have a link to the post where "this has been asked before"? So we do not have to re-invent the wheel.

Reply with quote

A0n1us
Joined:
Posts:
2

Re: Count and list total files per folder at remote site

Martin,
Here is the post that I was talking about
How to count folders recursively

Towards the end, the same error is mentioned. Hoping that I get some input from you, and I can try and implement it with my powershell script

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,541
Location:
Prague, Czechia

Re: Count and list total files per folder at remote site

Sorry, I do not see how that post is relevant to your question.
And it's about C#. In C#, I can write this easily.

So, let's start from the scratch. Do you want to list all folders recursively, showing number of files (and subfolders? or without subfolders?) along with each of them?

Reply with quote

Advertisement

You can post new topics in this forum