• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

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.
0
Daniel Forrester
Asked:
Daniel Forrester
  • 10
  • 9
  • 4
  • +2
1 Solution
 
shaydieCommented:
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
 
Daniel ForresterDirectorAuthor Commented:
Will be either server 2003 or 2000.
0
 
ipajonesCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
shaydieCommented:
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
 
Daniel ForresterDirectorAuthor Commented:
Both.
0
 
ipajonesCommented:

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

What next?
0
 
ipajonesCommented:
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
 
Daniel ForresterDirectorAuthor Commented:
The folder I want on the ftp server is called test how do i access it.
0
 
shaydieCommented:
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
 
ipajonesCommented:
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
 
Daniel ForresterDirectorAuthor Commented:
Can you send me over a dummy batch file and text file?
0
 
ipajonesCommented:

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
 
shaydieCommented:
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
 
rotech_ITCommented:
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
 
justadadCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 10
  • 9
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now