DATA Step, Macro, Functions and more

How to copy a file from FTP server to Local machine

Reply
Contributor
Posts: 22

How to copy a file from FTP server to Local machine

Hi,

 

I want to copy a file from FTP server (Unix) to local machine (Windows) using SAS code.

 

There are many files loaded in FTP server daily. So, I want just to copy the latest file from FTP to local machine.

 

Currently, I have to manually copy from Unix server to Windows using FTP (Tectia).

 

Thanks

Nikunj

Super User
Posts: 17,823

Re: How to copy a file from FTP server to Local machine

If you can create a script on Windows or Unix side to do the job, its relatively easy to execute the script from SAS. 

 

That's probably the ideal solution. You can also pass unix commands directly to the server or windows so if you can utilize x command you can pass the unix command to download the data as well.

 

In SAS 9.4+ there's FCOPY which will move files.

http://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#n10dz22b5ix...

 

 

Contributor
Posts: 22

Re: How to copy a file from FTP server to Local machine

Hi Reeza,

 

Thank You.

 

If possible, could you provide me unix code as i am not good with Unix.

So that I can make changes accordingly on that code and try it.

 

Regards,

Nikunj

Super User
Posts: 17,823

Re: How to copy a file from FTP server to Local machine

Sorry, I'm not great with Unix. It's also dependent on your application. 

 

I'm sure if you searched on StackOverflow you'd find examples of what the commands should look like. 

 

Would FCOPY work for you? Does your Base SAS see your server and your desktop at the same time? Or do you not have SAS 9.4?

Super User
Posts: 6,936

Re: How to copy a file from FTP server to Local machine

Windows ftp can be scripted with the -s:scriptfile commandline option.

Since you will only use the Windows client, no UNIX knowledge apart from file/pathnames is required.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 17,823

Re: How to copy a file from FTP server to Local machine

If you have EG you can use this add in:

 

http://blogs.sas.com/content/sasdummy/2012/12/06/copy-files-in-sas-eg/

 

PROC DOWNLOAD if you have the license for SAS/CONNECT 

http://www2.sas.com/proceedings/sugi30/023-30.pdf (top of page 6)

 

Google search results (untested):

http://snipplr.com/view/42667/sas-macro-to-ftp-files-between-pc-and-unix-or-among-unix-server-locati...

 

 

Super User
Posts: 5,256

Re: How to copy a file from FTP server to Local machine

And there's also the filename ftp engine.
Data never sleeps
Super User
Super User
Posts: 7,401

Re: How to copy a file from FTP server to Local machine

Hi,

 

Why would you need to do it in SAS?  You have an FTP program, just script it within that, then schedule that script to run as and when you need.  Including another application (SAS) in the process just means another step which needs documenting, creating, and validating.  

Contributor
Posts: 22

Re: How to copy a file from FTP server to Local machine

I agree with you RW9.

 

Even I tried the following code and create a batch of it for testing.

 

scpg3 host_server_name:/home/abcd/test.txt C:\abcd\Desktop

 

But, the issue is there are lot of files in the unix folder. I want the latest uploaded file only and with particular prefix.

 

Do you have any code for the same.

Super User
Posts: 17,823

Re: How to copy a file from FTP server to Local machine

How do you know which is the latest file? Does your prefix/naming convention include the date? 

Contributor
Posts: 22

Re: How to copy a file from FTP server to Local machine

Hi Reez,

 

Yes prefix has date stamp.

 

Also, if possible, we can get the modification time of the file. If it is of today then we will copy it.

Contributor
Posts: 22

Re: How to copy a file from FTP server to Local machine

Thanks all for your help and quick responses.

I have prepared a scp command line code. Also, files which i wanted to download has date prefix.. so that will also help me to copy the desired files only.

THANKS ALL again for their valuable advice.

Super User
Super User
Posts: 7,401

Re: How to copy a file from FTP server to Local machine

Its quite dependant on what you are using as the FTP software.  I did it many years back in Reflections - VB scripting, so thats not going to be much help.  

Google throws up a lot of results, this one for instance:

http://www.dreamincode.net/forums/topic/350239-download-last-modified-file-from-ftp-server/

Or WinSCP:

https://winscp.net/eng/docs/script_download_most_recent_file

 

Ask a Question
Discussion stats
  • 12 replies
  • 922 views
  • 5 likes
  • 5 in conversation