Solved

# batch file to upload to ftp

Posted on 2011-10-22
833 Views
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
Question by:ammartahir1978

LVL 51

Expert Comment

Take a look at this question, it has some examples of this. Post back any questions you have from that.

~bp
0

LVL 51

Expert Comment

0

Author Comment

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

Author Comment

i am trying this VBS but this doesnt work

anything i am missing in this?
strFTPServer = "IPADDRESS"
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")
strFTPScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "FTPCommands.txt"
Set objFTP = objFSO.CreateTextFile(strFTPScript, True)
objFTP.WriteLine "open " & strFTPServer
objFTP.WriteLine "user " & strUsername
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

0

LVL 51

Expert Comment

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

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.

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
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

i have tried but it doesnt work?

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

Thank you
0

Author Comment

this is what i get when i use this VBS

ftp> open IPADDRESS
220 (vsFTPd 2.2.2)

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.
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.

0

LVL 43

Expert Comment

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

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

or

Steve
0

LVL 43

Expert Comment

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

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

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 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 lcd %LocalDir%
echo cd %RemoteDir%
md %yyyymmdd%
cd %yyyymmdd%
echo put "photo3.jpg"
echo quit
) | ftp -n -i
0

LVL 43

Expert Comment

This line

may need to be

depending upon your server.
0

Author Comment

thank you dragon it , it is working now

thank you so much for your help
0

Author Closing Comment

Excellent
0

LVL 43

Expert Comment

No problem, it was probably just that password line.

Steve
0

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!