Batch file to FTP a file from Unix server to Windows

raksrules

Well-Known Member
Elite
Aug 30, 2007
9,906
1,746
378
Mumbai
I have a requirement of creating a batch file in windows. This is what it should do.

1. Connect to Unix server through say FTP using supplied hardcoded credentials.
2. Navigate to a predefined Unix directory (mostly /usr/tmp/XXXX/).
3. GET the file which is there. Assume only one file will be there in this directory at any point of time.
4. Delete the file from that Unix directory after GETTING it. (I guess this through FTP is possible or not, not sure)
5. Close the connection.
6. File should now be available to the user in the same directory where the batch file was executed.

How can i achieve this ???
 

nukeu666

Well-Known Member
Adept
Dec 19, 2006
2,156
45
112
36
try this script
dont forget to +x it
Code:
#!/bin/bash -vx
ftp -in 10.0.0.0<<END_SCRIPT
quote USER abc
quote PASS xxxx
bin
prompt off
cd /blah/-- this folder contains files...
lcd /blahblah/ -- this location is local directory for file replace...
mget *
rm *
bye
END_SCRIPT
 

6pack

Well-Known Member
ex-Mod
Sep 19, 2005
7,525
1,536
302
Using FTP Batch Scripts

will this help?

Common FTP Commands

? to request help or information about the FTP commands

ascii to set the mode of file transfer to ASCII (this is the default and transmits seven bits per character)

binary to set the mode of file transfer to binary (the binary mode transmits all eight bits per byte and thus provides less chance of a transmission error and must be used to transmit files other than ASCII files)

bye to exit the FTP environment (same as quit)

cd to change directory on the remote machine

close to terminate a connection with another computer

close brubeck closes the current FTP connection with brubeck,

but still leaves you within the FTP environment.

delete to delete (remove) a file in the current remote directory (same as rm in UNIX)

get to copy one file from the remote machine to the local machine

get ABC DEF copies file ABC in the current remote directory to (or on top of) a file named DEF in your current local directory.

get ABC copies file ABC in the current remote directory to (or on top of) a file with the same name, ABC, in your current local directory.

help to request a list of all available FTP commands

lcd to change directory on your local machine (same as UNIX cd)

ls to list the names of the files in the current remote directory

mkdir to make a new directory within the current remote directory

mget to copy multiple files from the remote machine to the local machine;

you are prompted for a y/n answer before transferring each file

mget * copies all the files in the current remote directory to your current local directory, using the same filenames. Notice the use of the wild card character, *.

mput to copy multiple files from the local machine to the remote machine;

you are prompted for a y/n answer before transferring each file

open to open a connection with another computer

open brubeck opens a new FTP connection with brubeck;

you must enter a username and password for a brubeck account

(unless it is to be an anonymous connection).

put to copy one file from the local machine to the remote machine

pwd to find out the pathname of the current directory on the remote machine

quit to exit the FTP environment (same as bye)

rmdir to to remove (delete) a directory in the current remote directory
Source: Basic FTP Commands

SOme more here :Microsoft Windows Command-Line FTP Command List
 

kekerode

Well-Known Member
Adept
Jul 29, 2009
570
7
82
Bangalore
Is perl on windows is option or any 3rd party executable? Or you just want to do it with whatever comes with windows only?
 

raksrules

Well-Known Member
Elite
Aug 30, 2007
9,906
1,746
378
Mumbai
kekerode said:
Is perl on windows is option or any 3rd party executable? Or you just want to do it with whatever comes with windows only?
I only have option of creating the script in windows. No third party tool or application is possible.
 

kekerode

Well-Known Member
Adept
Jul 29, 2009
570
7
82
Bangalore
Here u go ... should work

create a batch file ... let say with name ftp_script.bat

and here is content of ftp_script.bat

Code:
@echo off
@echo ftp_user>ftp_test.scr
@echo ftp_password>>ftp_test.scr
@echo ftp_password>>ftp_test.scr
@echo cd /user/tmp>>ftp_test.scr
@echo get deleteme.txt>>ftp_test.scr
@echo quit>>ftp_test.scr
@ftp -s:ftp_test.scr ftp_server
@del ftp_test.scr
change ftp_user, ftp_password,ftp_server according to credentials and other things too

note that there is no space before > or >>

just run batch file
 

raksrules

Well-Known Member
Elite
Aug 30, 2007
9,906
1,746
378
Mumbai
^^ This thing works as in it connects ftp, switches directory and all but for some reason the get command does not work. It works when the filename is exact but not when i give "abc*.pdf" or even "get *" does not work.

Mget is a posisbility but i dont want any prompt or user input, it has to ftp (get) all files which match the pattern that i give (here abc*)
 

kekerode

Well-Known Member
Adept
Jul 29, 2009
570
7
82
Bangalore
raksrules said:
^^ This thing works as in it connects ftp, switches directory and all but for some reason the get command does not work. It works when the filename is exact but not when i give "abc*.pdf" or even "get *" does not work.
Mget is a posisbility but i dont want any prompt or user input, it has to ftp (get) all files which match the pattern that i give (here abc*)
change this line

@ftp -s:ftp_test.scr ftp_server

to this

@ftp -i -s:ftp_test.scr ftp_server

this will turn off interactive mode while doing mget