Link to home
Start Free TrialLog in
Avatar of amd2002a
amd2002a

asked on

Batch file to copy remote directory via ftp then delete the directory.

Hi all:

I need to write a batch file that will download a remote directory (and all subdirectories) and then delete the downloaded directory on the remote server. The remote server is an AIX box and everything can only be done via ftp. Anyone has any idea how I can do this in a batch file?

Thanks.
Avatar of fulscher
fulscher

ams2002a

Questions:
- Do you know the names of the subdirectories to be downloaded? If so, you could put together a ftp script which does what you need. If not, things become more complicated, because you'd need to download the list of directories first and then create a second ftp script which download and deletes those directories (maybe there is a better solution?)

- Does it need to be a batch file or could it be, for example, a VB script file?

J.
Avatar of amd2002a

ASKER

There is no way for me to tell what files and directories are contained in the main directory. I only know the name of the main directory. It doesn't have to be a batch file, it just needs to be something that I can run/call from a Windows environment. I should be able to invoke it from another script.
Try this as a Batch file (save it with a .bat extension then run it):


@echo off
echo Creating FTP connection, then downloading necessary directory . . .

set directory=mainDirectory
set outputDir=c:\folder\to\save\it\to

cd %outputDir%

set server=server_IP_or_Hostname

set username=user
set password=drowssap

set tempFtpScript=script.ftp

echo %username%>%tempFtpScript%
echo %password%>>%tempFtpScript%
echo get %directory%>>%tempFtpScript%
echo del %directory%>>%tempFtpScript%
echo bye>>%tempFtpScript%

ftp -i -s:%tempFtpScript% %server%

echo complete [%errorlevel%]



Just an idea.
Rob.
Sorry - more questions:
Is the directory structure known?
Can a directory contain files AND other directories?
Do you want to replicate the directory structure of the server on the PC where you're downloading to?

ASKER CERTIFIED SOLUTION
Avatar of softplus
softplus

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
Thank you, softplus, that was what I was getting to.
Actually - upon checking the kermit documentation (I haven't been using kermit for ages), I found that the following command will do what's requested:

MGET /RECURSIVE /DELETE *

So - all you need to do is to get yourself a trial version of the good, old kermit from http://www.columbia.edu/kermit/k95.html, install it and try it Unfortunately, a full licence costs $64.

An alternative could be to buy another scriptable ftp client. They're all in the same price range - somewhere between $50 and $100.

I believe it will be difficult to program such a utility as you need it with built-in tools only, mainly because the ftp protocol is not well supported by the operating system. VBS (visual basic script) is probably not sufficient. -- This can be solved either with a commercial tool (again) or using a complete programming environment, like .NET or VB6. If you have access to one of those, we probably can hack something together.

How do you want to continue?
Softplus and fulscher:

I was hoping there would be a solution that wouldn't cost anything but brain cells. I work for a large company so by the time I put in a request and have it reviewed and actually get approval to buy a license we could be a couple months down the road(that's assuming I'll get approval). I don't have the time to wait for that. I work only in Java and I could do something in java but I just thought that would be overkill for such a seemingly simple task. I guess it's not as simple as I thought.

InteractiveMind:

I'll give your script a try although I think it looks similar to something I tried before that didn't work.
Ok - I've downloaded a number of freeware clients and will install them in a VirtualPC to check whether they would allow for recursive operations. Will report soon.
Ok - results:

CoreFTP lite: Might be able to, commercial use requires a licence. Haven't checked further.
FileZilla: Great open source client, no command-line operation, no scripting
LeechFTP: Nice client although did not work for me, no command-line operation, no scripting
MoveItFreely: Command-line client, does not support recursive operation

NcFTP: YES!!! This is a command-line utility (actually, a set of them) where one of them allows for recursive downloads AND deletions (according to the manual).

Since we're at it, we can go quickly through the rest of them:

psftp - command-line, recursive download, no recursive delete.
SmartFTP - commercial.
WarFTP - seems not so support deletion.

So, here's your answer: NcFTP is the tool to use. Download NcFTP from http://www.ncftp.com/ncftp/ ,  install it, open a explorer into the installation directories and browse through the docs in the doc\html subdirectory. The tool you need is ncftpget.exe. The parameters to use are -R and -DD.

HTH, J
SOLUTION
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
Fulscher:

NcFTP came very close but I need the empty directories to be deleted because the directory I am downloading and deleting is created by another process running on the remote AIX server. I found a program that was listed as freeware on one site but when I went to the actual application site I found that it was not actually freeware. ScriptFTP does everything that I need (http://www.scriptftp.com/). It has a 21 day trial and a site license is only $39.95.

Since softplus was the first to suggest using a scriptable client he/she gets the points. Thanks for pointing me in the right direction. I've already incorporated it into my script and everything is working as expected.
amd2002a thanks for the points, but I feel a bit bad about taking them all :) - you are aware that you can split them? say 1/3 me, 2/3 fulscher (for so much work)? Good to see that it's working, though!
I would have liked to have shared the points. I actually thought about it but I didn't see an option to share the points when I accepted the answer. I decided that the fair thing to do would be to assign the points to the person that first suggested the solution. If I had seen an option to share the points I would have done so. I might have overlooked it.
Well, I usually do not spend that much time for a solution. Now I remember again why.
Fulscher:

I appreciate all of your time and effort. If there is a way for me to split the points now I would. My intention was to split the points between the two of you.

Please do not think that I do not appreciate the time that you took to help me out.
Well, don't worry about the points - I need 3'000 a month to keep my premium services and I've got already more than enough. And since softplus is in a point-collecting mood, I guess, when looking at his statistics, he probably appreciates them more than I would...

But maybe you could tell the guys working on something a bit earlier if you change your mind about what's a valid solution in order to avoid them wasting their time...

The good thing is that you found a working solution - which is all EE is about.

J.
amd2002a: just post a request for a split with the link to this question here: https://www.experts-exchange.com/Community_Support/
Sure, I'm collecting points ;), but I also appreciate it when I get a little "something" for my work just as fulscher surely does.
Thank you for the points. I really appreciate it.