Solved

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

Posted on 2010-11-24
461 Views
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
Question by:dannyfccs
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 10
• 9
• 4
• +2

LVL 7

Expert Comment

ID: 34206459
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
----------------------------------------------------------------------------
cd /path/on/ftpserver
lcd C:\path\on\local
bin
prompt
bye
----------------------------------------------------------------------------
0

Author Comment

ID: 34206571
Will be either server 2003 or 2000.
0

LVL 6

Expert Comment

ID: 34206824
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 mdelete *.csv
echo mput whatever.csv
) | ftp -n -i >logfile.log

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

--IJ
0

LVL 7

Expert Comment

ID: 34207014
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

ID: 34207029
Both.
0

LVL 6

Expert Comment

ID: 34207091

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 mdelete *.csv
echo mput whatever.csv
) | ftp -n -i >logfile.log
del /q /f *.csv

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

Author Comment

ID: 34207291
Don't know if i'm being thick but im struggling to get this working!!!
0

LVL 6

Expert Comment

ID: 34207295
How far have you got ?
0

LVL 6

Expert Comment

ID: 34207306
I'll try and help but you'll need to give me more to go on....
0

Author Comment

ID: 34207311
Is this going to end up being a batch file that runs?
0

LVL 6

Expert Comment

ID: 34207353
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 mdelete *.csv
echo mput whatever.csv
) | ftp -n -i >logfile.log
del /q /f *.csv


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

ID: 34207407
@echo off
rem Put all FTP commands between the parenthesis prefixed with "echo"
( echo open <ftp.domain.com>
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

LVL 6

Expert Comment

ID: 34207430
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

ID: 34207471
Using explorer or the command line?
0

LVL 6

Expert Comment

ID: 34207484
using the command line.  We need to prove the basic commands work before they can be added to the script.
0

Author Comment

ID: 34207540
Ok I have a connection.
it shows;
ftp>

What next?
0

LVL 6

Expert Comment

ID: 34207583
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 ls
) | ftp -n -i


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

ID: 34207618
The folder I want on the ftp server is called test how do i access it.
0

LVL 7

Expert Comment

ID: 34207644
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
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)

lcd c:\Temp\TestA
prompt
mdelete *.csv
mput *.csv
bye
0

LVL 6

Expert Comment

ID: 34207646

So...

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

0

Author Comment

ID: 34207812
Can you send me over a dummy batch file and text file?
0

LVL 6

Expert Comment

ID: 34208010

Does the folloowing not work then ?

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


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 cd test
echo mdelete *.csv
echo mput c:\folder\book1.csv
) | ftp -n -i


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

ID: 34208163
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.

In the UploadFTP file you will want to change the IP Address on the last line to your FTP server
FtpExample.zip
0

LVL 5

Accepted Solution

rotech_IT earned 500 total points
ID: 34209124
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

ID: 34229432
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

Question has a verified solution.

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

Introduction: I have always been a big fan of Windows but my liking towards it is slowly being eroded by the variety of other Applications that I encounter, when I browse the Web. Most of the software available is free and maybe Open Source too. …
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
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 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
###### Suggested Courses
Course of the Month9 days, 7 hours left to enroll