Powershell 如何计算FTP目录中的文件数

Powershell 如何计算FTP目录中的文件数,powershell,ftp,ftpwebrequest,string-split,Powershell,Ftp,Ftpwebrequest,String Split,我有这个剧本。我想数一数有多少文件在里面 清除 $ftp_uri=”ftp://ftp.domain.net:" $user=“用户名” $pass=“密码” $subfolder=“/test/out/” $ftp\u urix=$ftp\u uri+$子文件夹 $uri=[system.uri]$ftp\u urix $ftp=[system.net.ftpwebrequest]::创建($uri) $ftp.Credentials=新对象系统.Net.NetworkCredential($

我有这个剧本。我想数一数有多少文件在里面

清除
$ftp_uri=”ftp://ftp.domain.net:"
$user=“用户名”
$pass=“密码”
$subfolder=“/test/out/”
$ftp\u urix=$ftp\u uri+$子文件夹
$uri=[system.uri]$ftp\u urix
$ftp=[system.net.ftpwebrequest]::创建($uri)
$ftp.Credentials=新对象系统.Net.NetworkCredential($user,$pass)
#获取当前目录中的文件列表。
$ftp.Method=[system.net.WebRequestMethods+ftp]::ListDirectorydetails
$ftp.UseBinary=$true
$ftp.KeepAlive=$false
$ftp.EnableSsl=$true
$ftp.Timeout=30000
$ftp.useAmposive=$true
尝试
{
$ftpresponse=$ftp.GetResponse()
$strm=$ftpresponse.GetResponseStream()
$ftpreader=新对象系统.IO.StreamReader($strm,'UTF-8'))
$list=$ftpreader.ReadToEnd()
$lines=$list.Split(“'n”)
$line
$line.Count
$ftpReader.Close()
$ftpresponse.Close()
}
抓住{
$| fl*-力
$ftpReader.Close()
$ftpresponse.Close()
}

在目录中,我有三个文件,但
$line.count
返回4<代码>$line有4行、三个文件和一个空行。有人能给我解释这个谜吗?

这个
$list
包含:

file1`nfile2`nfile3`n
如果用“`n”分割字符串,则(正确地)得到四个部分,最后一个部分为空

您可以使用接受并使用
删除mptyentries

$list.Split('n',[System.StringSplitOptions]::removeMptyEntries)