Solved

FTP scripting - Automate download of files and move files to an archive directory

Posted on 2012-03-30
6
1,767 Views
Last Modified: 2012-06-27
One of my clients has a connection to an Unix environment where he starts some applications wich he uses in a Windows environment.

There is also a directory wich we can access by use a NFSclient, but after 2 days we have to reboot the server, to get the connection up and running again.

There is an other option, wich is downloading the files to a local directory by using FTP.

Now i am looking for a way to download the files and i have the following:

open <IP-address>
<username>
<password>
cd ..
cd ..
cd ..
cd ..
cd ..
cd var
cd appl
cd <application name>
prompt
lcd N:\<localdirectory>
mget *.aaa
mget *.AAA
mget *.bbb
mget *.BBB
bye

Open in new window


This works when i run it, but the files are still on the remote FTP location.

What i would like to do is putting these files in the Remote FTP directory ARCHIVE for an example.

I have found a sample code on a unix / linux form
#!/bin/sh
clear
# change local directory
cd [local-directory]

#collect file names
ftp -ni ftp.abccompany.com <<EOF
user [user] [password]
cd /OUT
mls W945*.* list.txt 
quit
EOF

# create ftp action list
echo >>todo.lst user [user] [password]
while read N
do
    echo >>todo.lst cd /OUT
    echo >>todo.lst get $N
    echo >>todo.lst rename $N ARCHIVE/$N
done <list.txt

echo >>todo.lst quit

# ftp transfer process
ftp -nv ftp.abccompany.com <todo.lst

# cleanup
rm todo.lst

Open in new window


But these commands doesn't work.

Can someone help me to get this working?
0
Comment
Question by:RemcoVi
6 Comments
 
LVL 13

Expert Comment

by:Sandy
ID: 37793229
Please use pure-ftp having upload and run after scripts.
0
 
LVL 20

Accepted Solution

by:
carlmd earned 500 total points
ID: 37795727
The following script works... use it all the time.

#!/bin/ksh
#
    HOST='remotehostname'
    USERN='anonymous'
    PASS='mm'
    cd /usr/sys/inst.images/update
    echo `ftp -inv ${HOST} >/home/ftp.como <<EndFTP
    user "${USERN}" "${PASS}"
    cd /remote/path
    binary
    mget *
    quit
    EndFTP`

NOTE that the marks before the "ftp" and after"EndFTP" are back tics. You MUST use these. I suggest you cut and past this script into vi or other editor on unix and edit it there.

remotehostname -> the path/url to the remote site you want to get files from
USERN=  -> the login name you use at the remote site
PASS=  -> the password you use at the remote site
cd /usr/....  -> this is a "cd" on you local system to where you want to put the downloaded files
echo   -> this creates a file in the path /home/ftp.como (change as you wish) that contains all that goes by in the download,
                  this is valuable if you have any problems or just want to see what happens
user   -> this logs you in on the remote system (don't change this)
cd /remote/path  -> change this to be the path on the remote site where you want to get the files from
binary -> this indicates the download is a binary file, if files are ascii you can omit this line, but it does not hurt anyway
mget * -> get all the files in the directory you cd'ed to. If you wanted all files that started with "text" then make this mget text*

Save this script with your changes and make sure it is executable. (chmod a+x script).

You can run this script without using a terminal session by using the "at" command
For example:
        at now today /path/myscript             will run the script in the background now
        at 8 pm today /path/myscript           will run the script at 8PM today
for other option see man pages for at
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 37797289
To get the files and then move them to another directory, try this:
#!/bin/ksh
#
SRCHOST=remoteHost
USER=MySelf
PASS=WhoCares
SRCDIR=/remote/file/dir
ARCDIR=/remote/file/archive
TGTDIR=/local/file/path

cd $TGTDIR

#get list of file names to transfer
ftpcmd=`
ftp -ni $SRCHOST <<EOF | grep -v ^ftp | xargs -i? echo "get "?"\nrename "?" $ARCDIR/"?
user $USER pass $PASS
cd $SRCDIR
ls -1  *.aaa
ls -1  *.AAA
ls -1  *.bbb
ls -1  *.BBB
bye
EOF`

# Execute the file transfer
ftp -ni $SRCHOST <<EOF
user $USER pass $PASS
cd $SRCDIR
$ftpcmd
bye
EOF

Open in new window

:p
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:RemcoVi
ID: 37799775
Problem is that these scripts are for running on a unix environment, but i am connecting to a Unix environment, not running one.

I need to start the command on a Windows 2008 server, wich then connects to the ftp running on a Unix environment.

I tried the above anyway, but i can't get it to work.
0
 
LVL 20

Assisted Solution

by:carlmd
carlmd earned 500 total points
ID: 37800129
Sorry, the fact that you wanted to run this from Windows was not clear.

Take a look at:

http://support.microsoft.com/kb/96269

and

http://support.microsoft.com/kb/555976

and

http://www.howtogeek.com/howto/windows/how-to-automate-ftp-uploads-from-the-windows-command-line/

one of these should answer your question
0
 

Author Closing Comment

by:RemcoVi
ID: 37979291
I just bought a software license for a specific product wich makes scripting easier on FTP and it also has the option to do the things i want.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now