コメント |
[[拡張子が .cgi と .pl の 改行コードを lf に変更し、属性を 755 に変更する]]
@DIV
Imports System.Net.Sockets
Imports System.IO
Imports System.Text
Public Class Form1
Public user As String = "user"
Public pass As String = "pass"
Public ftp_base As String = "uploadTest"
Public local_base As String = "C:\user\vb\2008_04\lightbox_patio"
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
@C:green(' ローカルターゲットディレクトリ以下のディレクトリ一覧)
Dim subFolders As String() = System.IO.Directory.GetDirectories( _
local_base, "*", System.IO.SearchOption.AllDirectories)
Dim files As String() = Nothing
Dim ftp As String = ""
Dim wc As System.Net.WebClient = Nothing
@C:green(' アップロード用の WEB クライアント)
wc = New System.Net.WebClient()
@C:green(' アップロード用の ユーザー・パスワード)
wc.Credentials = New System.Net.NetworkCredential(user, pass)
@C:green(' ローカルフォルダの一覧をソート)( ディレクトリ作成順序の為の整列 )
Array.Sort(subFolders)
@C:green(' ローカルフォルダを列挙)
For Each dir As String In subFolders
@C:green(' リモートディレクトリ作成用のパス作成)
ftp = "ftp://lightbox.if.land.to/" + ftp_base + (dir.Replace(local_base, "")).Replace("\", "/")
@C:green(' ディレクトリ作成)
MkDir(ftp)
@C:green(' デバッグ用の標準出力)
Console.WriteLine(ftp)
@C:green(' 各ローカルディレクトリ内のファイル一覧)
files = System.IO.Directory.GetFiles(dir, "*", IO.SearchOption.TopDirectoryOnly)
@C:green(' ファイルを列挙)
For Each uploadTarget As String In files
If (System.IO.Path.GetExtension(uploadTarget)).ToLower = ".cgi" Or _
(System.IO.Path.GetExtension(uploadTarget)).ToLower = ".pl" Then
' アップロード( テキストLF変換上書き )
@C:green(' ******************************************)
@C:green(' バイナリとして CR をローカルで取り除く)
@C:green(' ******************************************)
Dim bs As Byte() = System.IO.File.ReadAllBytes(uploadTarget)
Dim len As Integer = bs.Length
Dim cnt As Integer = 0
For I As Integer = 0 To len - 1
If bs(I) <> &HD Then
cnt += 1
End If
Next
Dim bs2 As Byte() = New Byte(cnt - 1) {}
cnt = 0
For I As Integer = 0 To len - 1
If bs(I) <> &HD Then
bs2(cnt) = bs(I)
cnt += 1
End If
Next
@C:green(' 変換したデータを一時ファイルに書き込む)
Dim fn As String = System.IO.Path.GetTempFileName()
Dim fs As New System.IO.FileStream(fn, _
System.IO.FileMode.Create, _
System.IO.FileAccess.Write)
fs.Write(bs2, 0, bs2.Length)
@C:green(' 閉じる)
fs.Flush()
fs.Close()
@C:green(' 一時ファイルをアップロード)
wc.UploadFile( _
ftp & "/" & System.IO.Path.GetFileName(uploadTarget), _
fn _
)
@C:green(' 一時ファイルは削除)
System.IO.File.Delete(fn)
@C:green(' 属性変更)
chg_mod((dir.Replace(local_base, "")).Replace("\", "/") & "/" & System.IO.Path.GetFileName(uploadTarget))
Else
@C:green(' アップロード)( バイナリ上書き )
wc.UploadFile( _
ftp & "/" & System.IO.Path.GetFileName(uploadTarget), _
uploadTarget _
)
End If
@C:green(' デバッグ用の標準出力)
Console.WriteLine(uploadTarget)
Next
Next
@C:green(' メモリ開放)
wc.Dispose()
MessageBox.Show("処理が終了しました")
End Sub
Private Sub MkDir(ByVal path As String)
Dim ftpReq As System.Net.FtpWebRequest = Nothing
Dim ftpRes As System.Net.FtpWebResponse = Nothing
Try
ftpReq = CType(System.Net.WebRequest.Create(path), System.Net.FtpWebRequest)
'ログインユーザー名とパスワードを設定
ftpReq.Credentials = New System.Net.NetworkCredential(user, pass)
' ディレクトリ作成コマンド
ftpReq.Method = System.Net.WebRequestMethods.Ftp.MakeDirectory
ftpReq.KeepAlive = False
ftpReq.UseBinary = True
ftpReq.UsePassive = True
' 実行
ftpRes = CType(ftpReq.GetResponse(), System.Net.FtpWebResponse)
' デバッグ用の標準出力
' Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription)
' 閉じる
ftpRes.Close()
Catch ex As Exception
End Try
End Sub
Private Sub chg_mod(ByVal ftp_path As String)
' TCP/IP 処理のクラスインスタンスを作成
Dim ftp As TcpClient = New TcpClient()
' 接続( エラー処理無し )
ftp.Connect("lightbox.if.land.to", 21)
' FTP にアクセスする為のハンドルを作成
Dim ftp_access As NetworkStream = ftp.GetStream()
' データ読み込み用のインターフェイスを作成
Dim reader As StreamReader = New StreamReader(ftp_access)
' データ書き込み用のインターフェイスを作成
Dim writer As StreamWriter = New StreamWriter(ftp_access)
writer.AutoFlush = True
' 接続の応答コードを表示する
If ftp.Connected Then
Console.WriteLine(reader.ReadLine())
End If
' ////////////////////////////////////////////
' コマンド処理
' ////////////////////////////////////////////
' ユーザ名を送出
writer.WriteLine("USER " + user)
' 応答コードを表示する
Console.WriteLine(reader.ReadLine())
' パスワードを送出
writer.WriteLine("PASS " + pass)
' 応答コードを表示する
Console.WriteLine(reader.ReadLine())
' ここでは必要無いが必要な場合はここで実行する
writer.WriteLine("PASV")
' 応答コードを表示する
Console.WriteLine(reader.ReadLine())
' コマンド送出
writer.WriteLine("SITE CHMOD 755 /public_html/uploadTest" + ftp_path)
' 応答コードを表示する
Console.WriteLine(reader.ReadLine())
' ////////////////////////////////////////////
' コマンド処理の終了
' ////////////////////////////////////////////
' 接続解除
ftp.Close()
End Sub
End Class
@END
|