Solved

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

Posted on 2010-11-24
25
458 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
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
----------------------------------------------------------------------------
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
ID: 34206571
Will be either server 2003 or 2000.
0
 
LVL 6

Expert Comment

by:ipajones
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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:shaydie
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

by:dannyfccs
ID: 34207029
Both.
0
 
LVL 6

Expert Comment

by:ipajones
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 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
ID: 34207291
Don't know if i'm being thick but im struggling to get this working!!!
0
 
LVL 6

Expert Comment

by:ipajones
ID: 34207295
How far have you got ?
0
 
LVL 6

Expert Comment

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

Author Comment

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

Expert Comment

by:ipajones
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 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
ID: 34207407
@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
 
LVL 6

Expert Comment

by:ipajones
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

by:dannyfccs
ID: 34207471
Using explorer or the command line?
0
 
LVL 6

Expert Comment

by:ipajones
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

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

What next?
0
 
LVL 6

Expert Comment

by:ipajones
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 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
ID: 34207618
The folder I want on the ftp server is called test how do i access it.
0
 
LVL 7

Expert Comment

by:shaydie
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
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
ID: 34207646
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
ID: 34207812
Can you send me over a dummy batch file and text file?
0
 
LVL 6

Expert Comment

by:ipajones
ID: 34208010

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

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

by:justadad
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
online backup for win98se 30 60
SCCM 2012 14 39
Send data from a cell phone to a pc 5 54
XP driver for Dell Color MFP H625cdw printer needed 7 188
Hallo! I guess almost every Windows Administrator must have got stumped with this question "Where does WINDOWS store a users cached credentials? Every user who had once logged onto a Server/Desktop while it was connected to the domain could sti…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
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…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

685 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