powershell - How to count files in FTP directory -


i have script. i'm trying count how many file in.

clear $ftp_uri = "ftp://ftp.domain.net:" $user = "username" $pass = "password" $subfolder = "/test/out/" $ftp_urix = $ftp_uri + $subfolder $uri=[system.uri] $ftp_urix  $ftp=[system.net.ftpwebrequest]::create($uri)  $ftp.credentials=new-object system.net.networkcredential($user,$pass)  #get list of files in current directory. $ftp.method=[system.net.webrequestmethods+ftp]::listdirectorydetails $ftp.usebinary = $true $ftp.keepalive = $false $ftp.enablessl = $true $ftp.timeout = 30000 $ftp.usepassive=$true  try {  $ftpresponse=$ftp.getresponse()  $strm=$ftpresponse.getresponsestream()  $ftpreader=new-object system.io.streamreader($strm,'utf-8')  $list=$ftpreader.readtoend()   $lines=$list.split("`n")   $lines  $lines.count   $ftpreader.close()   $ftpresponse.close() } catch{  $_|fl * -force  $ftpreader.close()   $ftpresponse.close() } 

in directory have 3 files $lines.count return 4. $lines have 4 rows, 3 files , empty line. can explain me mystery?

the $list contains:

file1`nfile2`nfile3`n 

if split string "`n", (correctly) 4 parts, last 1 being empty.

you can use overload of string.split takes stringsplitoptions , use removeemptyentries:

$list.split("`n", [system.stringsplitoptions]::removeemptyentries) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -