Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

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?
A0n1us

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
martin

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.
A0n1us

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"