I'm not at all an expert at VB Scripting, but since it's a requirement at one of my projects for the moment, I am trying to write a VBScript that will GET all files from a specified FTP Folder.
I manage to get a single specified file, but I can't seem to get all files in a folder. Here's the script I'm trying to use:
Dim objOutStream
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True, TristateFalse)
With objOutStream
.WriteLine "USER myuser" ' USERNAME
.WriteLine "mypass" ' Password
.WriteLine "binary"
.WriteLine "prompt n"
.WriteLine "lcd /foldertocopyfrom" ' FOLDER I'm changing into
.WriteLine "mget *" ' Get all files with today's date in it
.WriteLine "bye"
.Close
End With
Set oFTPScriptShell = CreateObject("WScript.Shell")
oFTPScriptShell.Run "%comspec% /c FTP -n -s:" & "C:\temp\temp\empty.txt" & " " & "ftp.location.com", 0, True
It doesn't give me an error or anything, it basically does nothing (and yes, I'm sure there are files in the /foldertocopy from :-))
Any ideas? Something obvious I am missing?
Thanks!
I tried your solution but had to make a couple small fixes to make it work:
Added Option Explicit (for better detection of undefined variables)
Removed the TristateFalse parameter
Removed prompt since toggle since interactive mode is already off
Changed bye to quit
Added -i parameter to the FTP command
Plus I tested your FTP command on the command line before using it in your script
Here's the modified script
Option Explicit
Const ForWriting = 2
Dim objOutStream, objFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True)
With objOutStream
.WriteLine "USER myuser" ' USERNAME
.WriteLine "mypass" ' Password
.WriteLine "binary"
.WriteLine "lcd /foldertocopyfrom" ' FOLDER I'm changing into
.WriteLine "mget *" ' Get all files with today's date in it
.WriteLine "quit"
.Close
End With
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c FTP -n -i -s:" & "C:\temp\temp\empty.txt" & " " & "ftp.location.com", 0, True
I wrote a function to do this for you. You can read and examine it's code here:
http://www.naterice.com/articles/51
Please find below code to download from ftp location.
Function FTPDownload(sSite, sUsername, sPassword, sRemotePath)
Const ForWriting = 2
Dim objOutStream, objjFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting, True)
With objOutStream
.WriteLine sUsername ' USERNAME
.WriteLine sPassword ' Password
.WriteLine "binary"
.WriteLine "cd /"& sRemotePath' FOLDER I'm changing into
.WriteLine "mget *" ' Get all files with today's date in it
.WriteLine "quit"
.Close
End With
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%Comspec% /c FTP -i -s:" & "C:\temp\temp\empty.txt" & " " & sSite
End Function
Note e.g.
sSite : 192.168.0.1
Related
When I started on this I wasn't aware that I had to be able to select each daily task manually and pick each task whenever I want, I'm trying to figure out how to convert it into a manual entry so I don't have to rework the whole thing, bear in mind I'm very new to vbscript so if there's an obvious solution I apologize. I'm still working on the later days of the week to finish this.
dtmToday = Date()
dtmDayOfWeek = DatePart("w", dtmToday)
'Select case to pickup the value of day of the week and call procedure
Select Case dtmDayOfWeek
Case 1
Call Sunday()
Case 2
Call Monday()
Case 3
Call Tuesday()
Case 4
Call Wednesday()
Case 5
Call Thursday()
Case 6
Call Friday()
Case 7
Call Saturday()
End Select
'Sunday procedure will execute from select case
sub Sunday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' Execute the command and append to the text file
WShShell.run "cmd /c ping -n 10 youtube.com >> ping.txt", hidden
wscript.quit
End sub
'Monday procedure will execute from select case
sub Monday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' to execute the command and append to the text file using >> if you want to text to be overriden use >
WShShell.run "cmd /c netstat >> netstat.txt", hidden
wscript.quit
End sub
'Tuesday procedure will execute from select case
sub Tuesday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
' to execute the command and append to the text file using >> if you want to text to be overriden use >
WShShell.run "cmd /c arp -a >> arp.txt", hidden
wscript.quit
End sub
sub Wednesday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
WShShell.run "cmd /c nbstat -n >> nbstat.txt", hidden
wscript.quit
End Sub
sub Thursday()
'defining variables
dim wshShell
dim path
dim fso
'setting up the environment to run vbscript
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
WShShell.run "cmd /c tracert -n 10 youtube.com >> nbstat.txt", hidden
wscript.quit
End Sub
You need first optimize your code to avoid heavy duplication as #Lankymart was mentioned it in his comment, by writing one function and call it when you need it, and to store all your commands into an array for easy access by their index.
So your code can be written like that :
Option Explicit
' We define our Global variables
Dim Title,ArrCommands,strcmd,dtmDayOfWeek,IndexCommand,UserInput
Title = "Run command line based on the Day Of Week"
'----------------------------------------------------------------------------------
' We define and store our commands lines into an array
ArrCommands = Array(_
"ping -n 10 youtube.com >> ping.txt",_
"netstat >> netstat.txt",_
"arp -a >> arp.txt",_
"Color 0A & Title Running nbtstat command & nbtstat -n",_
"Color 0A & Title Running Tracert command & tracert youtube.com",_
"Color 0A & Title Running Ipconfig command & Ipconfig /all",_
"Color 0A & Title Running netstat command & netstat -ano"_
)
'-------------------------------Main Program---------------------------------------
Do While Not IsDate(UserInput)
UserInput = InputBox("Type a date here example 24/06/2020",Title,"24/06/2020")
dtmDayOfWeek = MyWeekday(UserInput)
IndexCommand = dtmDayOfWeek - 1
Loop
MsgBox "Day of the Week = "& dtmDayOfWeek & vbCrlf &_
"The command will be executed is : "& ArrCommands(IndexCommand),vbInformation,Title
'Select case to pickup the value of day of the week
Select Case dtmDayOfWeek
Case 1
Call Execute(ArrCommands(IndexCommand),0)
Case 2
Call Execute(ArrCommands(IndexCommand),0)
Case 3
Call Execute(ArrCommands(IndexCommand),0)
Case 4
Call Execute(ArrCommands(IndexCommand),1)
Case 5
Call Execute(ArrCommands(IndexCommand),1)
Case 6
Call Execute(ArrCommands(IndexCommand),1)
Case 7
Call Execute(ArrCommands(IndexCommand),1)
End Select
'MsgBox "Command line is done",vbInformation,Title
'----------------------------------------------------------------------------------
Function MyWeekday(MyDate)
If MyDate = "" Then MyDate = Date()
If IsDate(MyDate) Then
MyWeekDay = Weekday(MyDate)
Exit Function
End If
End Function
'----------------------------------------------------------------------------------
Sub Execute(StrCmd,Console)
Dim ws,MyCmd
Set ws = CreateObject("wscript.Shell")
'The console = 0 means will be running in hidden mode
If Console = 0 Then
MyCmd = "CMD /C " & StrCmd & " "
ws.run MyCmd,Console,True
End If
'The console = 1 means will be running in not hidden mode
If Console = 1 Then
MyCmd = "CMD /K " & StrCmd & " "
ws.run MyCmd,Console,True
End If
End Sub
'----------------------------------------------------------------------------------
I have the followaing run.vbs script
Rexe = "R-Portable\App\R-Portable\bin\Rscript.exe"
Ropts = "--no-save --no-environ --no-init-file --no-restore --no-Rconsole "
RScriptFile = "runShinyApp.R"
Outfile = "ShinyApp.log"
startChrome = "GoogleChromePortable\App\Chrome-bin\chrome.exe --app=http://127.0.0.1:9999"
strCommand = Rexe & " " & Ropts & " " & RScriptFile & " 1> " & Outfile & " 2>&1"
intWindowStyle = 0 ' Hide the window and activate another window.'
bWaitOnReturn = False ' continue running script after launching R '
' the following is a Sub call, so no parentheses around arguments'
CreateObject("Wscript.Shell").Run strCommand, intWindowStyle, bWaitOnReturn
WScript.Sleep 1000
CreateObject("Wscript.Shell").Run startChrome, intWindowStyle, bWaitOnReturn
It works pretty well in most cases except when the user puts the run.vbs script in a folder with spaces in its name: e.g. if run.vbs is in folder "foo bar", the user gets the error : "C:\Users\[user name]\Desktop\foo" not recognized as internal command...
I don't understand why Rscript.exe looks for the absolute path before running even if it's called from its parent directory using relative path.
I heard about the double quote solution using the absolute path but it doesn't seem to work with .exe scripts (it does though with .bat and .cmd)
Thanks for any help!
Below code will help you
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
'run command'
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec("C:\Program Files\R\R-3.2.3\bin\Rscript.exe C:\subfolder\YourScript.R " & """" & var1 & """")
Set oOutput = oExec.StdOut
handle the results as they are written to and read from the StdOut object
Dim s As String
Dim sLine As String
While Not oOutput.AtEndOfStream
sLine = oOutput.ReadLine
If sLine <> "" Then s = s & sLine & vbCrLf
Wend
I am new to vbs scripting, I have been working on a script to create a directory with user input. Then the script copies a specific file into the newly created directory. I can't seem to get the file into the newly created directory. any help would be great!!
dim UserName
Do
UserName = InputBox ("Enter Client Name with no spaces IE: lastname_firstname")
If UserName = "" then
Msgbox "No Username entered"
end if
Loop Until UserName <> ""
MsgBox "Please click OK to continue"
MsgBox "Check \\server\path\share\ for NEW client folder"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "cmd /c mkdir \\server\path\share\" & UserName
sPath = "\\server\path\share\"
Set oShell = CreateObject("WScript.Shell")
oShell.Run "explorer /n," & sPath & UserName, 1, False
Set oShell = Nothing
dim filesys
set filesys=CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("\\server\path\share\some_type_file.docx") Then
filesys.CopyFile "\\server\path\share\some_type_file.docx", "\\server\path\share\" <<COPY FILE INTO NEWLY CREATED DIRECTORY "UserName">>
End If
Try to change the last line to this
filesys.CopyFile "\\server\path\share\some_type_file.docx", "\\server\path\share\" & UserName & "\"
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 ?
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.
I need to comeup with a vbscript to grab a file from an ftp site where the file name is in the form "vendor(date)(date)(random#).zip" . These files are updated daily so I need a regex or way to select the newest file on the server and download it. I know how to handle this on the local file system, but I don't know how to determine which file to get on a remote ftp server.
Funny you posted this as I just recently had to knock out a script to do almost exactly word for word what you're asking for. Basically, all you really need to do is to have your script create an FTP command file, then call it.
Use your method of choice to create a string to hold the date in whatever format you are looking for, I called it strDate and in my case it ended up being this syntax: headerinfo.13September10 Where headerinfo is a standard file header with a number attached to it.
Write out an FTP command file:
Dim objOutStream
Set objOutStream = objFSO.OpenTextFile(strCommandFile, ForWriting, True, TristateFalse)
With objOutStream
.WriteLine "USER xxxxxx" ' USERNAME
.WriteLine "xxxxxxftp" ' Password
.WriteLine "binary"
.WriteLine "prompt n"
.WriteLine "lcd " & strNetmonData ' FOLDER I'm changing into
.WriteLine "mget *." & strDate ' Get all files with today's date in it
.WriteLine "bye"
.Close
End With
Then later in your script you just call it:
WSHShell.Run "%comspec% /c FTP -n -s:" & strCommandFile & " " & strSite, 0, True
Where strSite is the IP or name of the site you are trying to connect to.
The following code will help you to get the latest file name from ftp server after which you can download it.
Const ForWriting = 2
Dim objOutStream, objjFSO, objShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutStream = objFSO.OpenTextFile("C:\temp\temp\empty.txt", ForWriting,True)
With objOutStream
.WriteLine sUsername ' USERNAME
.WriteLine sPassword ' Password
.WriteLine "cd /"& sRemotePath' FOLDER I'm changing into
.WriteLine "ls -rt tmp/listing.txt"
.WriteLine "quit"
.Close
End With
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%Comspec% /c FTP -i -s:" & "C:\temp\temp\empty.txt" & " " & sSite
wait(2)
Set strCommand = objShell.Exec ("%Comspec% /c head -1 tmp\listing.txt")
Set objStdOut = strCommand.StdOut
strFilename = objStdOut.ReadLine