Batch File To Copy Text File To FTP Site - ftp

I have a text file on one computer that I want to send to a folder on a ftp site.
Can someone please show me a batch file code that will login to the FTP site with the username and password, and copy the text file.
Thanks for your help.

If you mean batch as in Windows batch, you can do that with the following script tst.cmd:
#ftp -n -stst.ftp myTargetMachine.com
(replacing myTargetMachine.com with the name of your actual FTP server) and the following FTP command file tst.ftp:
user myUser myPassword
dir
bye
Obviously, you should replace the myUser and myPassword with your actual username and password, and also dir command with whatever you really want to do, such as:
put localfile.txt /fullpath/remotefile.txt
If you're talking about a UNIX-like environment, the script would be:
#!/bin/bash
ftp -n myTargetMachine.com <<EOF
user myUser myPassword
dir
bye
EOF
Same deal with the FTP server name, user ID and password, of course.
And, in response to your comment:
Yes, I am talking about from a Windows environment. So for arguments sake, let's say the text file is C:\textfile.txt and I need to copy it to the FTP site in the folder called BACKUPS.
You would use a script like the following transfer.cmd:
#setlocal enableextensions enabledelayedexpansion
#echo off
c:
cd \
ftp -n -stransfer.ftp myTargetMachine.com
endlocal
and transfer.ftp:
user myUser myPassword
put textfile.txt /backups/textfile.txt
bye
If you wanted the Windows version as a single file, you could use something like:
#echo off
echo user myUser myPassword>tst.ftp
echo dir>>tst.ftp
echo bye>>tst.ftp
ftp -n -s:tst.ftp myTargetMachine.com
del /q tst.ftp
which temporarily creates tst.ftp and then deletes it when it's finished.

Command:
ftp -n -sScriptName HostName
And in the Script:
[User_id]
[ftp_password]
ascii
put myfilehere.html /remotedir/remotename.txt
quit

Related

FTP files in different folders using shell script

I'm trying for an ftp script, that sends files in different folders within the same connection, but no luck with below script.
#!/bin/bash
HOST_NAME=host.server
username= user_name
passwd= password
remote = /path_to_remote/folder
local = /path_to_local/folder
folder=$1
pwd
ftp -in <<EOF
open $HOST_NAME
user $username $passwd
cd local/
lcd remote/
put a_filename_<timestamp>.txt
mkdir $remote/$folder
cd $remote/$folder
lcd $local/$folder
put b_filename.txt
close
bye
Adding to this, at run-time, is it possible to send only the latest files created in the last 10 minutes?
Try this:
#!/bin/bash
HOST_NAME=host.server
username=user_name
passwd=password
remote=/path_to_remote/folder
local=/path_to_local/folder
folder="$1"
pwd
ftp -in <<EOF
open "$HOST_NAME"
user "$username" "$passwd"
cd local/
lcd remote/
put a_filename_<timestamp>.txt
mkdir "$remote/$folder"
cd "$remote/$folder"
lcd "$local/$folder"
put b_filename.txt
close
bye
EOF
Notes:
The shell doesn't allow spaces next to =.
Quote variables.
OP code lacked closing EOF.
I've left the <timestamp> alone, that'd be a different Q.

UNIX - FTP Using Parameterized Values

I have been stuck in this problem in a few days now and I really need help. My goal is to FTP a certain file into a bridge server. But before I can FTP, I need to enter some login credentials first. I want the login part to be automated that's why I created a separated parameter file. That parameter file has the login details.
So when I run the script, first it will create a txt file. Then the text file will be passed into the bridge server. Now, the script will also pass the login details from the parameter file to access the bridge server and finally a successful FTP. Any way to do this?
FTPFILE="File to be ftped"
Lets say the parameterised file has the details in the format
HostName username password.
Read the file contents using a loop statement like or however you like
I am using a while loop here
while read hostname username password
do
HOST=${hostname}
LOGIN=${username}
PWD=${password}
done
write the details - hostname,login, password to the $HOME/.netrc file
echo "machine ${HOST} login ${LOGIN} password ${PWD}" > /$HOME/.netrc
echo "macdef init" >> /$HOME/.netrc
echo "put ${FTPFILE} " >> /$HOME/.netrc
echo "bye" >> /$HOME/.netrc
echo >> /$HOME/.netrc
Ftp statement (Ftp first looks for .netrc file in $HOME directory to initiate the login process. If the file is not found then the username and password will be prompted)
ftp -i $HOST
This code will do the job:
#!/bin/sh
FTP_USERNAME=username
FTP_PASSWORD=password
FTP_SERVER=server_domaine
touch /directory/textfile.txt
run_access_server()
{
lftp <<STOP
#automatically access the server
open -u $FTP_USERNAME,$FTP_PASSWORD $FTP_SERVER
#changing directory
cd /directory/on/server
lcd /from/where/you/fetch/
#upload the file using get
mget textfile.txt
bye
STOP
}
run_access_server
Tell me how it works out with you.Regards

Windows batch script: ftp command: wait for the user to log in

I´d like to make Windows .cmd script file. One of its commands makes use of the ftp command. When I execute the batch script, the commmand prompt does not wait for the user to type his user/password, so the next commands are not properly executed. How can I make it to pause?
Thanks.
Edited:
My question was more related to the interactive use of the command (-i option) than the automatic login (-n option). I want to wait for the user to enter their credentials.
Also, I have seen that by typing the command:
ftp -n -i -s:myFtpCommands.txt 192.168.0.20
and that myFtpCommands.txt contains:
use myUser
mget *
bye
There is no need to type the password to get files. Where is the associated security problem?
You could ask the user for their details before hand, then build the command file based on their input.
#echo off
set /p un=Enter your FTP username:
set /p pw=Enter your FTP password:
echo open 192.168.0.20 >ftpscript.txt
echo %un% >>ftpscript.txt
echo %pw% >>ftpscript.txt
mget * >>ftpscript.txt
bye >>ftpscript.txt
ftp -i -s:ftpscript.txt
With regards to the auto login, this is a feature of FTP, it's an anonymous login, that is setup on FTP servers to allow this sort of access.
Whoever setup the FTP server would be able to control the accounts that are allowed to be used to access the server, and the permissions on the files that they don't want everyone to have access to.

FTP Command Copy all text files then delete

Hi I have created a ftp cmd script that would copy the text files from local source to ftp folder destination. copying was succesful. but i want it to be more complex that after successfull copying, copy to another local folder (will serve as backup copy of text files, increment file name by 1 when exists) then it would delete the copied text files from the source.
My script goes like:
#ftp -i -s:"%~f0"&GOTO:EOF
open [172.16.xx.xx]
oracle
mypassword
cd /ftp_destination/
mput D:/local_source_folder
quit
I know it is to much to ask and it is a bit complex, but helping a newbie would be a great help. Thanks for advance help.
#ftp -i -s:"%~f0"&GOTO:EOF
open [172.16.xx.xx]
oracle
mypassword
cd /ftp_destination/
mput D:/local_source_folder
!del /q D:/local_source_folder
quit
! is the shell command for FTP, and anything after it will be executed in a "dos" shell.
Alternitively, you can just do !ENTER and FTP will open a "dos" shell for you to type in. EXIT will return you to ftp. Like this:
#ftp -i -s:"%~f0"&GOTO:EOF
open [172.16.xx.xx]
oracle
mypassword
cd /ftp_destination/
mput D:/local_source_folder
!
del /q D:/local_source_folder
exit
quit

Access properties of a file at FTP server

I want to access properties of a file named AH_store_20120117_00432.csv at ftp server with url ftp.mysite.com. Properties such as last modified date/time,size, etc.
I am accessing ftp server from batch file as follows:
#echo off
>ftp2.txt Echo open _ftp.mysite.com
>>ftp2.txt Echo username
>>ftp2.txt Echo password
>>ftp2.txt Echo cd dir1\dir2
>>ftp2.txt Echo quit
ftp -s:ftp2.txt
The file is present in dir2.
FTP site can be accessed from above code, but not the file properties.
Kindly help.
Thanks
command dir gets some info about files
For your batch file:
#echo off
>ftp2.txt Echo open _ftp.mysite.com
>>ftp2.txt Echo username
>>ftp2.txt Echo password
>>ftp2.txt Echo cd dir1\dir2
>>ftp2.txt Echo dir
>>ftp2.txt Echo quit
ftp -s:ftp2.txt
You will get a list of the records included date of file. Format of the records may vary widely from system to system (RFC 959 - File Transfer Protocol).
For example Windows FTP server may response:
12-04-11 08:00PM 209665 04a64ad1-33c6-48ec-9931-abcc6445c491
...
Linux may response:
-rwxrwxrwx 1 owner group 156 Jan 27 2010 warning.log
...

Resources