Using Access to download files using FTP (1 Viewer)

prasadgov

Member
Local time
Today, 09:14
Joined
Oct 12, 2021
Messages
50
Hi,

Currently, I am trying to use .ftp and .bat files to download files via FTP, but it doesn't execute.

while my ftp file looks like

ftpfile:

open 00.123.4.56
asap
234!@
cd /ftphome
cd /filefolder

mget "RRRRR.R01" "X\destinationfolder\*.txt"
quit

and my batch file looks like

ftp -d -n -s:c:\folder\ftpfile.ftp

Also, I am checking if i can automate ftp using Access command action button which would call the batch file.

TIA
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:14
Joined
Feb 28, 2001
Messages
27,646
Currently, I am trying to use .ftp and .bat files to download files via FTP, but it doesn't execute.

How do you launch the job? Normally, you could trigger this with a SHELL function to launch a file. Is that what you are doing or do you use some other method?

Also, I am checking if i can automate ftp using Access command action button which would call the batch file.

The event following a button-click should be able to issue the SHELL function.

If you are not familiar with this function, here is a link to a description.

 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:14
Joined
Feb 19, 2002
Messages
43,948
Depending on your FTP client, you can map the FTP location as a drive. If you can do that, you operate exactly the way you would if you were getting data from your C: drive.
 

prasadgov

Member
Local time
Today, 09:14
Joined
Oct 12, 2021
Messages
50
Hi,

Currently, I am trying to use .ftp and .bat files to download files via FTP, but it doesn't execute.

while my ftp file looks like

ftpfile:

open 00.123.4.56
asap
234!@
cd /ftphome
cd /filefolder

mget "RRRRR.R01" "X\destinationfolder\*.txt"
quit

and my batch file looks like

ftp -d -n -s:c:\folder\ftpfile.ftp

Also, I am checking if i can automate ftp using Access command action button which would call the batch file.

TIA
Is my ftpfile correct? I am not sure if the cd should be followed by forward slash or not. Similarly, are the double quotes around files correct?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:14
Joined
Sep 12, 2006
Messages
15,798
Don't you need a username and password for the FTP site login.

You have two cd lines. One needs to be LCD, doesn't it? Or are you trying to navigate to one folder, then a subfolder of that folder. Is the FTP path correct?

I create FTP scripts as you are doing and then run them by calling FTP in VBA.

I'll post what I use when I get a moment.
 

prasadgov

Member
Local time
Today, 09:14
Joined
Oct 12, 2021
Messages
50
Don't you need a username and password for the login.

You have two cd lines. One needs to be LCD, doesn't it? Is the FTP path correct?

I create FTP scripts as you are doing and then run them by calling FTP in VBA.

I'll post what I use when I get a moment.
The FTP path is correct since I am able to open it via WinSCP
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:14
Joined
Sep 12, 2006
Messages
15,798
I use this command to send/retrieve a file.

Call ExecCommand("C:\Windows\System32\ftp.exe -ns:" & Chr(34) & scriptfolder & ScriptFile & Chr(34), vbMinimizedFocus)

scriptfolder and scriptfile give the path to the .bat file

This is ExecCommand. I haven't kept a note of where I found it. I won't post all the referenced calls. Let me know if you do need them.

Code:
Public Sub ExecCommand(cmdline$, screenmode&)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim retval As Long

Dim smode As Long

On Error Resume Next
smode = Nz(DLookup("ftpfocusmode", "tblconstants"), 0)
If smode = 0 Then smode = 1

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
'params
'1 - appname
'2 =command line
'3 = processattributes
'4 = threadattributes
'5 = inherithandles
'6= creationflags
'7 = lpenvironment
'8 = currentdirectory
'9 = startupinfo
'10 = processinfo


On Error GoTo fail
retval = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)


' Wait for the shelled application to finish:
retval = WaitForSingleObject(proc.hProcess, INFINITE)
retval = CloseHandle(proc.hProcess)


exithere:
    Exit Sub
   
fail:
    Call MsgBox("Error: " & err & "     Desc: " & err.Description)
    Resume exithere
   
End Sub
 

prasadgov

Member
Local time
Today, 09:14
Joined
Oct 12, 2021
Messages
50
I use this command to send/retrieve a file.

Call ExecCommand("C:\Windows\System32\ftp.exe -ns:" & Chr(34) & scriptfolder & ScriptFile & Chr(34), vbMinimizedFocus)

scriptfolder and scriptfile give the path to the .bat file

This is ExecCommand. I haven't kept a note of where I found it. I won't post all the referenced calls. Let me know if you do need them.

Code:
Public Sub ExecCommand(cmdline$, screenmode&)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim retval As Long

Dim smode As Long

On Error Resume Next
smode = Nz(DLookup("ftpfocusmode", "tblconstants"), 0)
If smode = 0 Then smode = 1

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
'params
'1 - appname
'2 =command line
'3 = processattributes
'4 = threadattributes
'5 = inherithandles
'6= creationflags
'7 = lpenvironment
'8 = currentdirectory
'9 = startupinfo
'10 = processinfo


On Error GoTo fail
retval = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)


' Wait for the shelled application to finish:
retval = WaitForSingleObject(proc.hProcess, INFINITE)
retval = CloseHandle(proc.hProcess)


exithere:
    Exit Sub
  
fail:
    Call MsgBox("Error: " & err & "     Desc: " & err.Description)
    Resume exithere
  
End Sub
What is ftpfocusmode & tblconstants.
ftpfocusmode references which string or field? Do i need to create a table, tblconstants with a field, ftpfocusmode?
Also, can I call this (Call ExecCommand("C:\Windows\System32\ftp.exe -ns:" & Chr(34) & scriptfolder & ScriptFile & Chr(34), vbMinimizedFocus)) from a command action button?

TIA
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:14
Joined
Sep 12, 2006
Messages
15,798
Here is the full module which includes the public types referred to, and the API process calls.
Note that this is for 32bit Access.

You can comment out the reference to smode and tblconstants. When I run this the black ftp screen opens and echo's the commands. I was trying to suppress that display, but couldn't get it to work. The smode value is hard coded in the execcommand call as vbminimizedfocus, and is passed in as argument screenmode, (a number, either 0 or 1, but isn't actually used). Clearly I didn't tidy it up completely when I got it working.

Code:
Option Compare Database
Option Explicit

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Declare Function WaitForSingleObject Lib _
"kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds _
As Long) As Long

Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Function ShellCommand(s As String) As Long
Dim result As Long

'I tried this version but I imagine it didn't work, and I had to find the longer version.
'I've left it here in case you want to try it.

    's = "C:\windows\system32\ftp.exe -ns:" & Chr(34) & CurrentProject.Path & "\FTPCollect.txt" & Chr(34)
    'MsgBox s

    result = Shell(s, 1)
    ShellCommand = result

'    ExecCommand "C:\windows\system32\ftp.exe -ns:e\databases\edifast\scripts\ftpcollect.txt", 1
'    MsgBox result

End Function



Public Sub ExecCommand(cmdline$, screenmode&)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim retval As Long

'this was a test to show the ftp command line, and then exit
'MsgBox cmdline
'Exit Sub

Dim smode As Long

On Error Resume Next
smode = Nz(DLookup("ftpfocusmode", "tblconstants"), 0)
If smode = 0 Then smode = 1


' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
'params
'1 - appname
'2 =command line
'3 = processattributes
'4 = threadattributes
'5 = inherithandles
'6= creationflags
'7 = lpenvironment
'8 = currentdirectory
'9 = startupinfo
'10 = processinfo

On Error GoTo fail
retval = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' Wait for the shelled application to finish:
retval = WaitForSingleObject(proc.hProcess, INFINITE)
retval = CloseHandle(proc.hProcess)

exithere:
    Exit Sub
   
fail:
    Call MsgBox("Error: " & err & "     Desc: " & err.Description)
    Resume exithere
   
End Sub
 

prasadgov

Member
Local time
Today, 09:14
Joined
Oct 12, 2021
Messages
50
Here is the full module which includes the public types referred to, and the API process calls.
Note that this is for 32bit Access.

You can comment out the reference to smode and tblconstants. When I run this the black ftp screen opens and echo's the commands. I was trying to suppress that display, but couldn't get it to work. The smode value is hard coded in the execcommand call as vbminimizedfocus, and is passed in as argument screenmode, (a number, either 0 or 1, but isn't actually used). Clearly I didn't tidy it up completely when I got it working.

Code:
Option Compare Database
Option Explicit

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessId As Long
    dwThreadId As Long
End Type

Private Declare Function WaitForSingleObject Lib _
"kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds _
As Long) As Long

Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long

Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Function ShellCommand(s As String) As Long
Dim result As Long

'I tried this version but I imagine it didn't work, and I had to find the longer version.
'I've left it here in case you want to try it.

    's = "C:\windows\system32\ftp.exe -ns:" & Chr(34) & CurrentProject.Path & "\FTPCollect.txt" & Chr(34)
    'MsgBox s

    result = Shell(s, 1)
    ShellCommand = result

'    ExecCommand "C:\windows\system32\ftp.exe -ns:e\databases\edifast\scripts\ftpcollect.txt", 1
'    MsgBox result

End Function



Public Sub ExecCommand(cmdline$, screenmode&)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim retval As Long

'this was a test to show the ftp command line, and then exit
'MsgBox cmdline
'Exit Sub

Dim smode As Long

On Error Resume Next
smode = Nz(DLookup("ftpfocusmode", "tblconstants"), 0)
If smode = 0 Then smode = 1


' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
'params
'1 - appname
'2 =command line
'3 = processattributes
'4 = threadattributes
'5 = inherithandles
'6= creationflags
'7 = lpenvironment
'8 = currentdirectory
'9 = startupinfo
'10 = processinfo

On Error GoTo fail
retval = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' Wait for the shelled application to finish:
retval = WaitForSingleObject(proc.hProcess, INFINITE)
retval = CloseHandle(proc.hProcess)

exithere:
    Exit Sub
  
fail:
    Call MsgBox("Error: " & err & "     Desc: " & err.Description)
    Resume exithere
  
End Sub
Since I do not have ftp and I use winscp, can I use the command as
ExecCommand "C:\software\winscp.exe -ns:c\Daily\inputs\ftpdaily.bat", 1

also. how can I call this from a command button?
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:14
Joined
Sep 21, 2011
Messages
14,718
Open a command window and type ftp and press the enter key. What happens?
They might have disabled it for some strange reason, but at least try it out.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:14
Joined
Sep 12, 2006
Messages
15,798
Since I do not have ftp and I use winscp, can I use the command as
ExecCommand "C:\software\winscp.exe -ns:c\Daily\inputs\ftpdaily.bat", 1

also. how can I call this from a command button?
I don't see how you can be asking a question like that. If you can't programme in VBA you will need some professional development support.

Manipulating ftp for instance is quite advanced. You can't expect volunteers to solve everything for you.

We had some trouble with FTP because of firewall settings. That problem was resolved, but in the meantime I got it working with winscp, so it's doable.

The process options for winscp will be different to ftp. So you are going to need something different to -ns for winscp.
 

prasadgov

Member
Local time
Today, 09:14
Joined
Oct 12, 2021
Messages
50
Open a command window and type ftp and press the enter key. What happens?
They might have disabled it for some strange reason, but at least try it out.
I did go the cmd prompt way.

I was able to ftp and get to "/" is current directory
ftp> ls
It got as far as "200 PORT command successful.
150 Opening ASCII mode data connection.

It stalls after this. Probably, the OS is stopping this.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:14
Joined
Sep 21, 2011
Messages
14,718
No, that just opens the ftp client.
You would normally add an ip address to that command, then you would be prompted for username and password.
 

prasadgov

Member
Local time
Today, 09:14
Joined
Oct 12, 2021
Messages
50
No, that just opens the ftp client.
You would normally add an ip address to that command, then you would be prompted for username and password.
I did those steps (ip, user, password) and got to the step which I listed above and it stalls
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:14
Joined
Sep 21, 2011
Messages
14,718
Well I would certainly be testing manually before even thinking of automating it.
Does your winscp work fine manually?
 

Users who are viewing this thread

Top Bottom