%Net.FtpSession クラスを使用してファイルサイズを取得する方法
これはInterSystems FAQ サイトの記事です。
ファイルサイズを取得する専用メソッドはありませんが、%Net.FtpSession クラスの sendCommand() メソッドを通してFTPのSIZEコマンドを呼び出し、サイズを取得できます。
簡単な利用例をご紹介します。
まず、以下の様なクラスを定義します。
Class MyNet.FtpSession Extends %Net.FtpSession
{ Method getSize(fname As %String, ByRef size As %Integer)
{
set type=..Type // 現Typeを保存しておく
do ..Binary()
set st=..sendCommand("SIZE "_fname) //SIZE <file name>
set size=..ReturnMessage
if type="Ascii" {
do ..Ascii()
}
quit
} }
上記で作成したクラスを使用したサンプルコードです。
set ftp=##class(MyNet.FtpSession).%New()
write ftp.Connect(ftpsrv,user,pass) // FTPサイトへ接続ができると1が返ります。
do ftp.getSize(fname,.fsize) // 第2パラメータに指定したファイルのサイズが返ります
write "ファイルサイズ : ",fsize,!
write ftp.Logout() // 成功したら1が返ります
※ sendCommand() メソッドは [Internal] 属性が付与されているため将来変更される可能性があります。