Solved

DOS batch command help needed with ftp

Posted on 2010-09-01
13
1,332 Views
Last Modified: 2013-12-09
what I am trying to do is
1 .connect to ftp
2. copy all files in a folder called "source"
3. place a copy of the files in a "Picked" folder
4. finally delete original files in "source"

I do this by using a batch file that calls a script file called GetFiles.txt
The calling batch file has following commands
@Echo off
cd C:\FTPTest
ftp -v -w:2000000 -s:getfiles.txt

Open in new window


GetFiles.txt has following commands
prompt
open <ftp address>
username
password
cd Source
binary
lcd C:\Destination
mget *.*
lcd C:\Destination
cd ..
cd Picked
mput *.*
for /f %%a IN ('dir /b C:\Destination\*.*') do delete %%a
bye

Open in new window


My script works for all 3 steps above. The step 4 fails.  ( Line 13 ) - it does not do anything
I dont want to use mdelete *.*    since I need to delete only the files I copied ( just in case some files did not get copied I want to keep them in source)
so I have implemented the following line for step 4 above ( but it fails )
for /f %%a IN ('dir /b C:\Destination\*.*') do delete %%a

Open in new window

0
Comment
Question by:indikad
[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
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33576891
No easy way to do this with the built in windows ftp client.  You would have to essentially do two ftp commands, the first to xfer the files, then use a for loop in the bat script to build a new control file for the second ftp run, with a delete line for each file in the destination folder, and then run ftp with that delete control file.

You could make this a LOT simpler by using NCFTPGET (link below) with the -DD option, it will transfer the files to your pc, and then delete them from the server if successful, all automatically.  And no control file needed, all options and parms are passed tight in on the command line.  Great tool, and free.

http://www.ncftp.com/ncftp/

~bp
0
 
LVL 3

Expert Comment

by:ren20atom
ID: 33578534
Hi Indikad,

Please try the following,
On the 13 line,  put the following command
dir /b C:\Destination >>c:\Destination\list.txt

on the line 14, put the following command
for /F %%A in (c:\destination\list.txt) do (del c:\destination\%%A)

on the line 15, put the command : bye
Please test and let me know if it worked.
0
 

Author Comment

by:indikad
ID: 33582730
Hi  ren20atom,

Thanks for your attention.

what is list.txt ?
also del does not work in thisn instance since  - I am deleting files in a ftp folder.

Hi , billprew - at this moment I am bit reluctant to have another 3rdPart FTP client on the server.  But thanks anyway for the tip.
0
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
LVL 54

Expert Comment

by:Bill Prew
ID: 33582786
Does the c:\destination folder get cleaned out after each xfer, or will it build up with more and more files over time?  If the latter then doing a DEL in ftp for each of those will grow beyond the files transferred in the most recent batch and become inefficient.

If the files do get moved out of there after each xfer then I have an approach that will work, but it will result in a good bit more bat code.

~bp
0
 

Author Comment

by:indikad
ID: 33582822
Yes. Destination folder ,  will get flushed to a seperate folder within the local server. I just have not implemented that yet.

Thanks.
0
 

Author Comment

by:indikad
ID: 33583140
billprew: , do you have the solution please ?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33583161
Working it...

~bp
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33583187
Okay, I think should give you the right idea.  Let me know what questions you have.

~bp
@echo off
cd C:\FTPTest
ftp -v -w:2000000 -s:getfiles.txt
copy /Y delfiles.cpy delfiles.txt
for %%A in ("C:\Destination\*.*") do echo del %%~nxA>>delfiles.txt
echo bye>>delfiles.txt
ftp -v -w:2000000 -s:delfiles.txt
 
===== getfiles.txt =====
prompt
open <ftp address>
username
password
cd Source
binary
lcd C:\Destination
mget *.*
lcd C:\Destination
cd ..
cd Picked
mput *.*
bye
 
===== delfiles.cpy =====
prompt
open <ftp address>
username
password
cd Source

Open in new window

0
 

Author Comment

by:indikad
ID: 33583286
Thanks I will try now, and let you know.
0
 

Author Comment

by:indikad
ID: 33583473
Works perfectly exactly the way I want.
Appreciate your time, this is worth 1000 points given the situation.

Thanks.
0
 

Author Closing Comment

by:indikad
ID: 33583482
Thumbs Up!
0
 

Author Comment

by:indikad
ID: 33583494
for anyone else using this code - make sure to have a CR / LF after the last line of the delfiles.cpy
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33585453
So glad that was useful, and thanks for the kind words, and the grade.

~bp
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unexpected Windows system folders on D drive 16 111
2003 Server DNS/FS errors 6 81
Automated File Transfer with FileZilla 9 209
Dropbox phishing tutorial 5 69
Cloud file services can fill many different roles for your business. Often, the use of cloud file services begins with employees using consumer products, like Dropbox, to share files with customers and each other. While sync-and-share can be an effe…
Outsource Your Fax Infrastructure to the Cloud (And come out looking like an IT Hero!) Relative to the many demands on today’s IT teams, spending capital, time and resources to maintain physical fax servers and infrastructure is not a high priority.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

739 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