2012-02-17 6 views
1

私はこのように混乱しています。私はVBSスクリプトを使ってFTPにデータをアップロードしようとしていますが、ループ内にスクリプトを追加すると、1つのファイルファイルでうまく動作します。FTPにリモートアップロードする際に一時ファイルを生成する必要があるのはなぜですか?

また、なぜ我々は

Dim fso, folder, files, strPath 
Set fso = CreateObject("Scripting.FileSystemObject") 

strPath = "E:/Test" 
Set folder = fso.GetFolder(strPath) 
Set files = folder.Files 

Const hostname = "ftp.domain.com" 
Const port = 21 
Const username = "username" 
Const password = "password" 
Const remoteDir = "/" 
Const useDefaultsExclusively = True 
Const skipConfirmation = True 


For each item In files 

    If InStr(1, item.Name, "txt") <> 0 Then 
    defaultFile = item.Name 
    localFile = fso.getFileName(defaultFile) 
    localDir = fso.getParentFolderName(defaultFile) 
    Set shell = CreateObject("WScript.Shell") 

    tempDir = shell.ExpandEnvironmentStrings("%TEMP%") 
    ' temporary script file supplied to Windows FTP client 
    scriptFile = tempDir & "\" & fso.GetTempName 
    ' temporary file to store standard output from Windows FTP client 
    outputFile = tempDir & "\" & fso.GetTempName 

    'input script 
    script = script & "lcd " & """" & localDir & """" & vbCRLF 
    script = script & "open " & hostname & " " & port & vbCRLF 
    script = script & "user " & username & vbCRLF 
    script = script & password & vbCRLF 
    script = script & "cd " & """" & remoteDir & """" & vbCRLF 
    script = script & "binary" & vbCRLF 
    script = script & "prompt n" & vbCRLF 
    script = script & "put " & """" & localFile & """" & vbCRLF 
    script = script & "quit" & vbCRLF 


    Set textFile = fso.CreateTextFile(scriptFile, True) 
    textFile.WriteLine(script) 



    ' bWaitOnReturn set to TRUE - indicating script should wait for the program 
    ' to finish executing before continuing to the next statement 
    shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, TRUE 
    Wscript.Sleep 10 
    ' open standard output temp file read only, failing if not present 
    Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2) 
    results = textFile.ReadAll 
    textFile.Close 

End If 

Next 

fso.DeleteFile(scriptFile) 
fso.DeleteFile(outputFile) 

、これは私が使用しているスクリプトです

、私はこれを言うことを意味

、ときFTPへのリモートアップロード一時ファイルを生成する必要がありますこのコードで一時ファイルを作成する理由は次のとおりです。S

Set shell = CreateObject("WScript.Shell") 

    tempDir = shell.ExpandEnvironmentStrings("%TEMP%") 
    ' temporary script file supplied to Windows FTP client 
    scriptFile = tempDir & "\" & fso.GetTempName 
    ' temporary file to store standard output from Windows FTP client 
    outputFile = tempDir & "\" & fso.GetTempName 


Set textFile = fso.CreateTextFile(scriptFile, True) 
     textFile.WriteLine(script) 



     ' bWaitOnReturn set to TRUE - indicating script should wait for the program 
     ' to finish executing before continuing to the next statement 
     shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, TRUE 
     Wscript.Sleep 10 
     ' open standard output temp file read only, failing if not present 
     Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2) 
     results = textFile.ReadAll 
     textFile.Close 

私はループしていますがスクリプトを介して、それは現在のディレクトリ内のすべてのtxtファイルをアップロードする必要がありますが、それは最初のものだけをアップロードします、なぜそうですか?

私は、現在のファイルすなわち

MsgBox defaultfile 

を印刷するとき、それは現在のフォルダのみが、アップロードの最初のファイル内の各txtファイルの名前を表示します。

答えて

5

FTPのライブ自動化のネイティブメソッドはありません。これは実際にはスクリプトの回避策です。あなたのスクリプトはFTP命令でテキストファイルを作成しています。次に、-sスイッチを使用してコマンドラインからFTPを起動します。 -sスイッチを使うと、閉じる前に実行するFTP.exeのセッションコマンドを含むテキストファイルを提供できます。スクリプトが実際にFTPプログラムを直接自動化するわけではありませんが、FTPを「無人モード」で実行することで自動化をシミュレートします。コマンドプロンプトを開き、ftp /?と入力すると、より理解を深めることができます。

[編集] 私はお詫び申し上げます、私はあなたの2番目の質問を逃した。スクリプトは、CreateTextFileメソッドをループするため、最初のファイルのみをアップロードします。ファイルがすでに存在するため、このメソッドは最初の試行の後で失敗しています。あなたが本当にしなければならないことは、一時ファイルにファイルを追加してからFTPを一度だけ実行することです。あなたのWebサーバーも休憩に感謝します。

Dim fso, folder, files, strPath 
Set fso = CreateObject("Scripting.FileSystemObject") 

strPath = "E:/Test" 
Set folder = fso.GetFolder(strPath) 
Set files = folder.Files 

Const hostname = "ftp.domain.com" 
Const port = 21 
Const username = "username" 
Const password = "password" 
Const remoteDir = "/" 
Const useDefaultsExclusively = True 
Const skipConfirmation = True 

tempDir = shell.ExpandEnvironmentStrings("%TEMP%") 
' temporary script file supplied to Windows FTP client 
scriptFile = tempDir & "\" & fso.GetTempName 
' temporary file to store standard output from Windows FTP client 
outputFile = tempDir & "\" & fso.GetTempName 

Set textFile = fso.CreateTextFile(scriptFile, True) 

'input script 
textFile.WriteLine("open " & hostname & " " & port) 
textFile.WriteLine("user " & username) 
textFile.WriteLine(password) 
textFile.WriteLine("cd " & Chr(34) & remoteDir & Chr(34)) 
textFile.WriteLine("binary") 
textFile.WriteLine("prompt n") 

For Each item In files 
    If InStr(1, item.Name, "txt") <> 0 Then 
     textFile.WriteLine("put " & Chr(34) & item.Path & "\" & item.Name & Chr(34)) 
    End If 
Next 

textFile.WriteLine("quit") 
textFile.Close 

Set shell = CreateObject("WScript.Shell") 
' bWaitOnReturn set to TRUE - indicating script should wait for the program 
' to finish executing before continuing to the next statement 
shell.Run "%comspec% /c FTP -n -s:" & scriptFile & " > " & outputFile, 0, True 
WScript.Sleep 10 
' open standard output temp file read only, failing if not present 
Set textFile = fso.OpenTextFile(outputFile, 1, 0, -2) 
results = textFile.ReadAll 
textFile.Close 

fso.DeleteFile(scriptFile) 
fso.DeleteFile(outputFile) 
関連する問題