Solved

Setup Scheduled task through Windows that copies a file up to an FTP server.

Posted on 2010-11-24
25
452 Views
Last Modified: 2012-06-27
Basically I need to copy a csv file up to an FTP server that a customer has access to. I want this to be done as a scheduled task through Windows, however if this is not possible I'm open to suggestions. Then part of the script is to delete the old csv files and keep the current one live.
0
Comment
Question by:dannyfccs
  • 10
  • 9
  • 4
  • +2
25 Comments
 
LVL 7

Expert Comment

by:shaydie
Comment Utility
Yes.. this is possible. What is your OS?
 I do something like this to download files on a scheduled task using the standard win xp ftp. Use a batch file to start the ftp program and a script to specify the ftp actions. You can also use the batch file to delete the csv files after ftp'ing. Setup a scheduled tas to run the batch file.. This is the format.. you will need to edit it for your settings.


The batch file consists of this line, which uses a script and outputs to a log file.
----------------------------------------------------------------------------
ftp -s:C:\path\to\ftpscript.txt 192.168.1.12 >  C:\path\to\logfile.txt
----------------------------------------------------------------------------

The script is a text file in this format
----------------------------------------------------------------------------
username
password
cd /path/on/ftpserver
lcd C:\path\on\local
bin
prompt
mget filetodownload
mget filetodownload2
mput filetoupload
mput filetoupload2
bye
----------------------------------------------------------------------------
0
 

Author Comment

by:dannyfccs
Comment Utility
Will be either server 2003 or 2000.
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
You could also do this just by using 1 batch script with the FTP commands embedded:

rem Put all FTP commands between the parenthesis prefixed with "echo"
( echo open <ftp_server_name>
  echo user <ftp_user> <ftp_password>
  echo mdelete *.csv
  echo mput whatever.csv
) | ftp -n -i >logfile.log

Open in new window

You can then schedule this batch file to run using Windows Task Scheduler.

--IJ
0
 
LVL 7

Expert Comment

by:shaydie
Comment Utility
I believe both 2k and 2003 have the builtin ftp client so you are good there.

When you say 'part of the script is to delete the old csv files and keep the current one live' is that delete files from the FTP server, the local disk or both?
0
 

Author Comment

by:dannyfccs
Comment Utility
Both.
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility

You can easily add a "delete" command to delete files on the local drive as well

rem Put all FTP commands between the parenthesis prefixed with "echo"
( echo open <ftp_server_name>
  echo user <ftp_user> <ftp_password>
  echo mdelete *.csv
  echo mput whatever.csv
) | ftp -n -i >logfile.log
del /q /f *.csv

Open in new window

You'll obviously need to substitute your own filename and directory details.
0
 

Author Comment

by:dannyfccs
Comment Utility
Don't know if i'm being thick but im struggling to get this working!!!
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
How far have you got ?
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
I'll try and help but you'll need to give me more to go on....
0
 

Author Comment

by:dannyfccs
Comment Utility
Is this going to end up being a batch file that runs?
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
yep.  So you would put the following in a file and save it as say "ftpupload.cmd"

@echo off
rem Put all FTP commands between the parenthesis prefixed with "echo"
( echo open <ftp_server_name>
  echo user <ftp_user> <ftp_password>
  echo mdelete *.csv
  echo mput whatever.csv
) | ftp -n -i >logfile.log
del /q /f *.csv

Open in new window


Obviously you need to substitute in your own folder and filename details.  I would also suggest creating some temporary files (e.g. temp1.tmp, temp2.tmp) so you can play with and test the script.  Once you're happy you can modify to use the "real" filenames.

When testing you can run the script interactively and then worry about the scheduled task bit once you've got the script working.

Why don't you post what you've got so far and we can take it from there.

--IJ
0
 

Author Comment

by:dannyfccs
Comment Utility
@echo off
rem Put all FTP commands between the parenthesis prefixed with "echo"
( echo open <ftp.domain.com>
  echo user <username> <password>
  echo mdelete *.csv
  echo mput c:\folder\book1.csv
) | ftp -n -i >logfile.log
del /q /f *.csv
 
This is pasted from my.cmd file where am I going wrong?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:ipajones
Comment Utility
I take it you're leaving out the "< >" ?

Let's start from the beginning:

can you do "ftp ftp.domain.com" and successfully login to the FTP server ?

Also - what error's are you getting ?
0
 

Author Comment

by:dannyfccs
Comment Utility
Using explorer or the command line?
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
using the command line.  We need to prove the basic commands work before they can be added to the script.
0
 

Author Comment

by:dannyfccs
Comment Utility
Ok I have a connection.
it shows;
ftp>

What next?
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
OK.  So now quit the FTP session and put the FTP server name and user details into the following script:

@echo off
( echo open ftp.domain.com
  echo user username password
  echo ls
) | ftp -n -i

Open in new window


Save this as say ftptest.cmd and run it - you should see a dir of the files currently saved to the root folder on the FTP server.
0
 

Author Comment

by:dannyfccs
Comment Utility
The folder I want on the ftp server is called test how do i access it.
0
 
LVL 7

Expert Comment

by:shaydie
Comment Utility
Once you've made sure you can connect... Here's another idea.. this would work if you may not know the file name but just want to upload the most recent csv file.. it's still using the script and batch file but I'm sure you could convert it to the one batch file also.

This will look in your specified directory and copy the most recent csv file to a tmp folder.. delete all csv files in the folder, move your most recent file back.. connect to the ftp server, delete all csv files, and upload your most current csv.

change in batch file the lines

set source=c:\Temp\TestA (Change this to your folder where csv file is)
set destdir=c:\Temp\TestA\tmp (Change this to your tmp folder)
ftp -s:C:\path\to\ftpscript.txt 192.168.1.12 >  C:\path\to\logfile.txt (Change the paths to your script and log file, and the IP address to the actual FTP server address)

Change in the script
username (ftp username)
password (ftp password)
lcd c:\Temp\TestA (your local folder where the csv file resides)

Save the batch file and script.. make sure the batch file is referencing the actual path to the script where it says: C:\path\to\ftpscript.txt

Run the batch file

Batch:

set source=c:\Temp\TestA
set destdir=c:\Temp\TestA\tmp
md %destdir%
pushd "%source%"
for /f "tokens=*" %%a in ('dir %source%\*.csv /b /a-d /o:d 2^>NUL') do (set lfile=%%a)
copy /y "%source%\%lfile%" "%destdir%\%lfile%"
del "%source%\*.csv"
move "%destdir%\%lfile%" "%source%\%lfile%"
ftp -s:C:\path\to\ftpscript.txt 192.168.1.12 >  C:\path\to\logfile.txt

Script: (ftpscript.txt)

username
password
lcd c:\Temp\TestA
prompt
mdelete *.csv
mput *.csv
bye
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility
Just add "cd test"

So...

@echo off
( echo open ftp.domain.com
  echo user username password
  echo cd test
  echo ls
) | ftp -n -i

0
 

Author Comment

by:dannyfccs
Comment Utility
Can you send me over a dummy batch file and text file?
0
 
LVL 6

Expert Comment

by:ipajones
Comment Utility

Does the folloowing not work then ?

@echo off
( echo open ftp.domain.com
  echo user username password
  echo cd test
  echo ls
) | ftp -n -i

Open in new window


Once you've proven the basic FTP scripted commands you can then add the appropriate get, put and delete commands. e.g.

@echo off
( echo open ftp.domain.com
  echo user username password
  echo cd test
  echo mdelete *.csv
  echo mput c:\folder\book1.csv
) | ftp -n -i

Open in new window


Once you're happy the basic script is working you can look at adding a command to delete local copies of the CSV files and running the batch script as a scheduled task.

--IJ
0
 
LVL 7

Expert Comment

by:shaydie
Comment Utility
I can upload you an example of what I was saying... I attached a zip with a batch file and script file.

I don't know if you want to use the delete local csv files portion?? If not just delete all but the last line of the batch file.

You will need to put these files in c:\folder and have your csv there also
Note: As is it will delete all csv files in this folder except the newest one.. so you may want to change the folder or back it up first if you have other csv files you don't want deleted.

You will want to rename the UploadFTP.txt to UploadFTP.bat

In the UploadFTP file you will want to change the IP Address on the last line to your FTP server
and in the script change the username and password to your FTP username and password.
FtpExample.zip
0
 
LVL 5

Accepted Solution

by:
rotech_IT earned 500 total points
Comment Utility
This can be done easily with watchdirectory as an alternative.  $129 for the professional edition, which is what you'd want.  It's well worth the investment.  It has a 30 day free trial as well.

http://www.watchdirectory.net/
0
 
LVL 7

Expert Comment

by:justadad
Comment Utility
Another option is to use WinSCP. I use it in a batch file every day to get files from a Linux box and sycronise with a local folder for backup purposes. Some of the commands are in the batch file, the rest are in a file that WinSCP processes.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

762 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

6 Experts available now in Live!

Get 1:1 Help Now