Link to home
Start Free TrialLog in
Avatar of Dale Fye
Dale FyeFlag for United States of America

asked on

cmd.exe will not close when running .bat file to perform FTP upload

I'm trying to  create a batch file and FTP script to upload a file to a FTP site.  The code is working but the cmd.exe fails to close when the file is uploaded.  My Bat file looks like:

ftp -n -s:"C:\myFolder\FTP_Script.txt"

Open in new window

The script file looks like:
Open  FTPURL.Com
User MyUserName
MyPassword
CD "/FTP Folder"
bin
Put "C:\Work\dummy.txt"
quit

Open in new window

The file gets uploaded properly, but the Command window does not close.  The lines in the cmd.exe window look like:
ftp> Open FTPURL.com
220 Serv-U FTP Server v15.1 ready...
ftp> User MyUserName
331 User name okay, need password.

230 User logged in, proceed.
ftp> CD "/FTP Folder"
250 Directory changed to /FTP Folder
ftp> bin
200 Type set to I.
ftp> put "C:\Work\dummy.txt"
220 Port command successful.
150 Opening Binary mode data connection for Dummy.txt
_

Open in new window

Then it just hangs there, and never closes the cmd.exe window.
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

You can try using mput for put. That has worked for me - though years ago in the days of Windows NT/2000.

/gustav
Update your BAT file:
START ftp -n -s:"C:\myFolder\FTP_Script.txt"
EXIT

or 

EXIT /B

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
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 Dale Fye

ASKER

Tried most of the above.  

Gustav, tried the mput last night, but this is only going to be a one file, once a day process, so I don't need the multiple aspect of that.

pcelba, tried the EXIT and Exit /B commands in the .bat, but those did not make a difference either.

Jim, didn't use your entire code because the client is insisting running this from a batch file, but compared your FTP File to mine and made the appropriate modifications and although the file is being uploaded, the Quit command is not terminating the connection to the FTP site.  

The FTP is working fine, but the Command Window will not close.

I added a > LogFile.txt to the .bat file, but that is simply returning the same information as was previously visible in the cmd.exe window.
I also use mput for single files. It behaves a bit differently.

Perhaps you need to politely close the session by the commands:

    close
    bye

though I believe bye is just a synonym for quit.

/gustav
"tried the EXIT and Exit /B commands"  I've also recommended to use  START  ftp.  Did you try it?  Also START /B alternative is worth to try.

Your ftp seems to be frozen after the put command because the quit  or  bye  command should return  "221 Goodbye".

You may try HASH command to display the file transfer progress.
Couple things:

1. I would put the FTP command itself in the script file.
2. I would then try and execute the script file from the command line manually.
3. I would then call the transfer with the shell command.
4. I noticed your transferring a .txt file, but your using binary mode?  
5. As pcelba said, seems like your hanging on the transfer itself, not that the batch file itself won't quit.

 Doing the above should get you to an answer where the problem is.

Jim.
Check the active vs passive mode on the FTP script....the server may require passive mode.

Again, walk through the commands your using manually, then build up from there (have the script file include the FTP command, and quit, then execute the script manually, then call with shell from the program).

Jim.
Could you please give it a try by making firewall off ?

How about the result when you run each ftp command manually ?
What is the title of the cmd window when it is hanging? If it is FTP then the exit/bye command isn't working. Try adding a 'verbose' command at the beginning of the ftp script to see what is happening. Also, try adding an exit command in the bat file, after the ftp line.
i concur with pelba.... depending upon server & client, should expect a
226 Closing data connection
after the successful transfer
Jim,

Not familiar with "WaitWhileRunning", is that one of your functions?

Also, what are you using for "IsValidFTP"


Dale
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
Jim,

Strange,  I made no changes to either the batch file or the actual FTP script code this morning (still running the original code I posted in my original post) and the process worked completely, including closing the cmd.exe window, so I still have no idea what caused the original problem.

I like that WaitWhileRunning routine.  That is pretty slick.

In your IsValidFTP function, it doesn't look like you are doing any testing for a failure of any type, just a single 226 or 250 will result in a True.  I'll have to take a look at the log file, now that we have the upload working.  It turns out the client will have multiple files being uploaded, so I want to be able to confirm that all files were uploaded successfully.  Maybe I can put a counter in there, but also check for an incomplete or invalid upload code, in case it fails.

Dale
<<In your IsValidFTP function, it doesn't look like you are doing any testing for a failure of any type, just a single 226 or 250 will result in a True.>>

 Anything besides getting a 226 or 250 is a failure.

<< It turns out the client will have multiple files being uploaded>>

 My routines are written to process one file at a time.  Not very efficient if you have a lot of files though as your logging in/out for every one.

 Actually, I don't like using FTP and stick with AS2 whenever possible.  It's a managed file transfer and has none of the short comings of FTP.

Jim.
I'm not familiar with AS2 (I'll have to look that up), I have not been doing much file transfer stuff for quite a while, but have had two clients in the last two months who have had a requirement.
AS2 is an encrypted managed file transfer process from peer to peer using the http protocol (AS1 uses e-mail, AS3 uses FTP).

It's a managed file transfer, meaning it has  end to end checks on  the file being received,getting it in its entirety,  and re-tries when a send files.   Optionally you can get a receipt back back as well either async or sync.

This company:

https://www.rssbus.com/ports/

 Offers AS2 software for Free with one partner, so it's perfect for most ad-hoc situations.   And even if you need to buy a license because of needing some of the advanced features, it's still reasonably priced. They have some info as well there.

  Setup can be a bit of a pain at first, but no more so than using sFTP or FTPs and once you do a couple, it becomes easy.

 In terms of sending/receiving, all you have is in and out baskets (directories).  Drop a file in the out basket, software picks it up, sends it, and then either deletes it or renames it.   For receiving, you just do a fast directory check on the in-basket.

 Since it's a "push" of a file from both sides, it's very efficient as you don't need to continually log onto a FTP server and check for files.

Jim.
Thanks, Jim.  I think my client is going to stick with the FTP process, but I will probably adopt your WaitWhileRunning and some variant of the IsFTPValid.