Link to home
Start Free TrialLog in
Avatar of Daniel Forrester
Daniel Forrester

asked on

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

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.
Avatar of shaydie
shaydie

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
----------------------------------------------------------------------------
Avatar of Daniel Forrester

ASKER

Will be either server 2003 or 2000.
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
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?
Both.

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.
Don't know if i'm being thick but im struggling to get this working!!!
How far have you got ?
I'll try and help but you'll need to give me more to go on....
Is this going to end up being a batch file that runs?
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
@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?
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 ?
Using explorer or the command line?
using the command line.  We need to prove the basic commands work before they can be added to the script.
Ok I have a connection.
it shows;
ftp>

What next?
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.
The folder I want on the ftp server is called test how do i access it.
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
Just add "cd test"

So...

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

Can you send me over a dummy batch file and text file?

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
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
ASKER CERTIFIED SOLUTION
Avatar of rotech_IT
rotech_IT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kent Keller
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.