?
Solved

batch file to upload to ftp

Posted on 2011-10-22
16
Medium Priority
?
992 Views
Last Modified: 2012-05-12
hi All,

i am looking for a good script which i can use to send multiple files to ftp site, i want to schedule the batch file so that it run every night and send files to ftp.

Let me know if i can get it online or if there is someone who have written already
0
Comment
Question by:ammartahir1978
  • 7
  • 6
  • 3
16 Comments
 
LVL 60

Expert Comment

by:Bill Prew
ID: 37012515
Take a look at this question, it has some examples of this. Post back any questions you have from that.

~bp
0
 
LVL 60

Expert Comment

by:Bill Prew
ID: 37012516
0
 

Author Comment

by:ammartahir1978
ID: 37012598
hi billprew thank you for your reply, but i couldnt understand i tried the first few line and try to connect to ftp server but a window is opening and then close!!!!!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ammartahir1978
ID: 37012603
i am trying this VBS but this doesnt work

anything i am missing in this?
strFTPServer = "IPADDRESS"
strUsername = "USERNAME"
strPassword = "PASSWORD"
strFileToUpload = "C:\Users\Administrator\Desktop\photo3.jpg"
Set objNetwork = CreateObject("WScript.Network")
strFolderToCreate = Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & "_" & objNetwork.ComputerName

Set objFSO = CreateObject("Scripting.FileSystemObject")
strFileToUpload = objFSO.GetFile(strFileToUpload).ShortPath
strFTPScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FTPCommands.txt"
Set objFTP = objFSO.CreateTextFile(strFTPScript, True)
objFTP.WriteLine "open " & strFTPServer
objFTP.WriteLine "user " & strUsername
objFTP.WriteLine "Password " & strPassword
objFTP.WriteLine "cd street"
objFTP.WriteLine "cd Windows"
objFTP.WriteLine "md " & strFolderToCreate
objFTP.WriteLine "cd " & strFolderToCreate
objFTP.WriteLine "put " & strFileToUpload
objFTP.WriteLine "cd.."
objFTP.WriteLine "cd.."
objFTP.WriteLine "close"
objFTP.WriteLine "bye"
objFTP.Close
strFTPScript = objFSO.GetFile(strFTPScript).ShortPath

Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /k ftp -s:" & strFTPScript, 1, True

objFSO.DeleteFile strFTPScript, True

Open in new window

0
 
LVL 60

Expert Comment

by:Bill Prew
ID: 37012877
Try running the FTP command from a DOS command prompt. Change the vbs script to not delete the temporary file, and then do the following from a command line:

ftp -s:c:\foo\bar.txt

changing c:\foo\bar.txt to your temp file name.  See what errors it displays.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37013437

You should be ok with what is in the other q.  my link there describes the other methods i use, though i prefer the one i pasted into the other questions, i.e.


http://scripts.dragon-it.co.uk/links/batch-ftp-scripting

do
cmd.exe
ftp.exe

sort out commands to send to ftp server having logged on, or give us the file / dir names and we can help.

E.g.
Type in at ftp prompt:

open x.x.x.x
type in the username and password
lcd c:\dir on your system
cd /remote directory name
bin
put yourfile.txt  (copies yourfile.txt from dir on your sys to remote dir)
quit

When you say multiple, are we talking for or five named files, a directory worth, a directory with sub directories etc?

Steve
0
 

Author Comment

by:ammartahir1978
ID: 37013910
i have tried but it doesnt work?

can someone create the file and i can replace the contents accordingly?

Thank you
0
 

Author Comment

by:ammartahir1978
ID: 37014041
this is what i get when i use this VBS

ftp> open IPADDRESS
Connected to IPADDRESS
220 (vsFTPd 2.2.2)
User (IPADDRESS:(none)):
331 Please specify the password.

530 Login incorrect.
Login failed.
ftp> cd street
530 Please login with USER and PASS.
ftp> cd Windows
530 Please login with USER and PASS.
ftp> md 20111023_SERVER2008
Ambiguous command.
ftp> cd 20111023_SERVER2008
530 Please login with USER and PASS.
ftp> put C:\Users\ADMINI~1\Desktop\photo3.jpg
530 Please login with USER and PASS.
530 Please login with USER and PASS.
ftp> cd..
Invalid command.
ftp> cd..
Invalid command.
ftp> close
221 Goodbye.

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014050
Not sure why you are going to VBS route when it is a few lines in batch file, i.e. Copy / paste the code into notepad.  Save as something.cmd (not something.vbs.txt or ftp.cmd for instance as it causes all sorts of issues).

Anyway I think the hint in your output may be this "530 Login incorrect." and "530 Please login with USER and PASS."

objFTP.WriteLine "user " & strUsername
objFTP.WriteLine "Password " & strPassword

May or not need the user and/or password keyword for your server, I would normally not expect it to need Password, so:

objFTP.WriteLine "user " & strUsername
objFTP.WriteLine strPassword

or

objFTP.WriteLine strUsername
objFTP.WriteLine strPassword



Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014053
Oh and the cd.. commands would have to be cd .. but you don't really need them anyway as you are quitting just after.

Also try to add "-n -i" on the commandline for ftp.exe to stop it doing auto-login, and to stop prompting on some transfers.

i.e.
objShell.Run "cmd /k ftp -n -i -s:" & strFTPScript, 1, True

Steve
0
 

Author Comment

by:ammartahir1978
ID: 37014059
hi dragon-it

thank you so much, yes i would l;ove to use simple scrpt but i tried your provided script and it didnt worked
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 37014097
Ok, it will work if given a chance .... it is the same as you are making a file and then running exactly the same ftp.exe command line.

Either please try suggestions above or feedback on what doesn't work in batch script.  This is what you would need based on your vbscript code in batch IMO:

@echo off
set FTPServer=IPADDRESS
set Username=USERNAME
set Password=PASSWORD
set LocalDir=C:\Users\Administrator\Desktop
set RemoteDir=street/Windows

REM Get date in form yyyymmdd_computername
echo wscript.echo Year(Date) ^& Right("0" ^& Month(Date), 2) ^& Right("0" ^& Day(Date), 2) ^& "_%computername%" > "%temp%\getdate.vbs
for /f "tokens=*" %%a in ('cscript //nologo "%temp%\getdate.vbs"') do set yyyymmdd=%%a

(echo open %FTPServer%
echo user %UserName%
echo %Password%
echo lcd %LocalDir%
echo cd %RemoteDir%
md %yyyymmdd%
cd %yyyymmdd%
echo put "photo3.jpg"
echo quit
) | ftp -n -i
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014098
This line

echo user %UserName%

may need to be

echo %UserName%

depending upon your server.
0
 

Author Comment

by:ammartahir1978
ID: 37014101
thank you dragon it , it is working now

thank you so much for your help
0
 

Author Closing Comment

by:ammartahir1978
ID: 37014103
Excellent
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37014146
No problem, it was probably just that password line.

Steve
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

615 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