Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with FTP script 1

Status
Not open for further replies.

staffordwarren

Technical User
Jul 21, 2000
2
US
I want to create a script that gets a file from a Unix server and moves it to my Sun box each night.<br>I currently have a script that looks like this:<br><br>ftp -n -v &lt;&lt; EndFTP<br>open &lt;system name&gt;<br>user &lt;user/pwd&gt;<br>ascii<br>cd sales_reports<br>get Report-20000706-052148.csv<br>close<br>EndFTP<br><br>Notice that in the file name, there is a date-timestamp string.&nbsp;&nbsp;What I need to do is to pick up files (and there may be multiples) that match an inputted date string.&nbsp;&nbsp;What I'd like to do is use a wildcard character in the file-spec, but that doesn't seem to work.&nbsp;&nbsp;As a second approach, can I list the files (with ls) that exist, capture the response in an array and then get the names from the array?<br><br>Any help is much appreciated!<br>Stafford Warren
 
Hi Stafford,<br><br>Here's a short perl script that does the job, would have written in ksh or sh but don't have that at home :-( so if you have perl this will do fine - post back if you don't and I'm sure someone will post a ksh version.<br><br>The script generates an ftp script for you. You may need to modify the first line so that it matches where the perl executable lives on your system. You can find that out by typing 'whence perl' or 'which perl' on the unix command line. If you haven't got perl or it's not in your $PATH the whence and which commands won't display anything.<br><br>Copy the script below into a file (mkftpscript.pl for example), make it executable (chmod +x mkftpscript.pl) and run it like this:<br><FONT FACE=monospace><br>./mkftpscript.pl &gt; ftpscript<br>chmod +x ftpscript<br>./ftpscript<br><br><b><br>#!/usr/bin/perl<br># go to the correct directory<br>chdir &quot;c:\\work&quot;;<br><br># get the current date (and other things I don't care about)<br>my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);<br><br># .. because Perl years start from 1900 - ho hum<br>$year+=1900;<br># .. because Perl months count from zero<br>$mon++;<br><br># stick a zero in front of month and day if I need to<br>$mon = &quot;0$mon&quot; if length($mon)==1;<br>$day = &quot;0$day&quot; if length($day)==1;<br><br># print the ftp script on STDOUT<br>print &quot;ftp -v -n &lt;&lt;EndFtp<br>open &lt;system name&gt;<br>user &lt;user/pwd&gt;<br>ascii<br>cd sales_reports<br>&quot;;<br><br># loop thru (sic) the files for today<br>while(&lt;Report-$year$mon$day*.csv&gt;){<br> print &quot;get $_\n&quot;;<br>}<br><br># and finish off the ftp script<br>print &quot;close<br>EndFtp<br>&quot;;<br></font></b><br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
If you wanted to go down the &quot;listing of files and grabbing a name&quot; route, Expect might do the job for you.&nbsp;&nbsp;Details at <A HREF=" TARGET="_new"> caveat - you will also need TCL available, so it might be more effort than it's worth. <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
Actually, I read up a little over the weekend on ftp, and I think I can get what I need by using the mget command.<br><br>thanks for your suggestions.
 
Yes -- mget is what you want.&nbsp;&nbsp;Use:<br><br>ftp -n -v &lt;&lt; EndFTP<br>open &lt;system name&gt;<br>user &lt;user/pwd&gt;<br>ascii<br>cd sales_reports<br>prompt<br>mget Report-20000706*.csv<br>close<br>EndFTP<br>
 
You can automate the inputting of the date using the <b>date</b> command... thus:<br><FONT FACE=monospace><br>TheDate=`date +&quot;%Y%m%d&quot;`<br>ftp -n -v &lt;&lt; *EOI<br>open &lt;system name&gt;<br>user &lt;user/pwd&gt;<br>ascii<br>cd sales_reports<br>prompt<br>mget Report-$TheDate*.csv<br>close<br>*EOI<br></font><br><br>I hope it works...
 
Elgis - that is cool. That one goes into my tool-box. <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top