Sufiyan Ghori
Sufiyan Ghori

Reputation: 18763

VBscript and CMD writing into a text file

I am writing a script that executes and write everything to the file

here is example,

I stored the complete command in the variable 'Command' ,

Command = "ftp ftp.xyz.com 21 " & vbCRLF

and then executing it in command prompt,

shell.Run "%comspec% /c FTP " & Command & " > " & E:/abc.txt, 0, TRUE

but when this program execute it won't write anything to the text file because this is an incomplete command, this command on execution prompt user to input username and password of FTP,

how can i do this , that my programm automatically input username and password when prompt and then write everything to file ?

Upvotes: 0

Views: 3118

Answers (1)

Nilpo
Nilpo

Reputation: 4816

You need to run FTP using an unattended script. (Try ftp /? and look at the -s switch.)

It looks like this:

Const HOSTNAME = "ftp.myserver.com"
Const USERNAME = "Login"
Const PASSWORD = "password"

Set WshShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.CreateTextFile("session.txt")

With objFile
    .WriteLine "USER username"
    .WriteLine "password"
    .WriteLine "cd /public_html/"  ' continue adding commands like this
    .Close
End With

strOutput = "C:\somefilepath\output.txt"
strCommand = "%systemroot%\System32\ftp.exe -s:session.txt > " & strOutput
strCommand = WshShell.ExpandEnvironmentStrings(strCommand)
WshShell.Run strCommand, 0, vbTrue

objFso.DeleteFile "session.txt", vbTrue

You can read more in my article Using FTP in WSH on ASP Free. I also answered a related question here.

Upvotes: 1

Related Questions