Link to home
Start Free TrialLog in
Avatar of horstuff
horstuff

asked on

trying to create batch file for ftp

What I have there is a poor example of anything, I'm sure. I've tried everything. What I am trying to put together is a batch file that will, via ftp, check to see if a file exists, and if not, spit out a message. If it does exist, then the file will be downloaded. I have WS_FTP and I can create a batch to run it with the right parameters, but I can guess at this point that isn't what I will end up using. I got the following working, except the file name it is looking for is a local file just to test. What I really need is to check for a file on our web server and if it exists, download. If not, then say "No File" or something.

@ECHO off
set file="c:\test.txt"
if  exist %FILE%  goto  filefound
ECHO The file you requested is not available. Please try again later.
PAUSE
goto end
:filefound
"C:\Program Files\WS_FTP Pro\wsftppro.exe" -s "myserver.host.com:/usr/www/users/slub/OM_data/QMExportFromFieldOffice.mdb" -d "local:C:\data\field_office_data\QMExportFromFieldOffice.mdb"
:end
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Here's my 3 file solution.  While you can probably use WS_FTP (I like it, but never scripted it before), you can also use the built in ftp client with Windows 2000.  The first two files are scripts to be run via FTP.  One checks for the filename you are seeking, the other gets that filename.

The DOS part of the script takes the output from the FTP client and puts it into a temporary text file.  Then checks that text file for the filename we're looking for.  If found, then the FTP-GET.SCR FTP script is run to retrieve the file.  If not, then it displays "No File".

-----8<-----[FTP-LS.SCR]
username
password
ls
quit
-----8<-----[FTP-LS.SCR]

-----8<-----[FTP-GET.SCR]
username
password
get filename.ext
quit
-----8<-----[FTP-GET.SCR]

-----8<-----[CHECK4FILE.CMD]
@echo off
ftp -s:ftp-ls.scr ftp.host.com > %temp%\ftp-ls.tmp 2>&1
find /i "filename.ext" "%temp%\ftp-ls.tmp"
If "%errorlevel%" == "1" (
Echo No File
Goto End
)
ftp -s:ftp-get.scr ftp.host.com
:End
del %temp%\ftp-ls.tmp
-----8<-----[CHECK4FILE.CMD]
Avatar of horstuff
horstuff

ASKER

This looks good, except maybe I need to clarify two things - this batch file is going to be run on XP pro, and the file I need to transfer is a .mdb. Will all this still work? Thanks.
Sure, NT/2000/XP - all the same (as far as script capabilty).  Just replace all "filename.ext" entries the name of your database.
I'm sorry, but this is beyond me right now. Are these three seperate files, and if so, what are their names, or does it matter?
 
How are they executed?

Is ftp.host.com the exact protocol, or can I specify the actual address of our webserver (without the ftp in front)?

 How do I tell the script which directory to look in for the .mdb, or does it have to be in the root?

Thanks.
It's three seperate scripts, each one enclosed in ----8<-----[SCRIPT.NAME] areas.

ftp.host.com is simply the ftp server name.  so if you're connecting to ftp.aol.com - then change ftp.host.com to ftp.aol.com.  If you are connecting to mypc.mydomain.net, then change ftp.host.com to mypc.mydomain.net.

I'll revise the FTP scripts below to account for changing directories (the third is a dos script that utilizes the FTP script).

This begins the first FTP Script.
-----8<-----[FTP-LS.SCR]
username
password
cd /directory1/directory2/etc/etc
ls
quit
-----8<-----[FTP-LS.SCR]



This begins the second FTP script.
-----8<-----[FTP-GET.SCR]
username
password
cd /directory1/directory2/etc/etc
get filename.ext
quit
-----8<-----[FTP-GET.SCR]

Hey, thanks for the quick reply. I got it working now based on your last post, but if the file is not on the server, the dos window just blinks quickly with no message. If it is on the server, the dos window has a bunch of text, the last part something to the effect of "logged in, opening port, etc." Was it supposed to display and hold a message? Thanks again.
Also, I guess to wrap this up I should tell you how I will use this in my situation. I need to have a file a user can click on (which we now have, I guess) that will connect to our server and check if a file exists. If it does exist, thier pc will download it to a certain directory. If it doesn't exist, they will get a message saying "File does not exist" or something.

On the other side of this, I need to also create something that will, once the file has been downloaded, delete it from the server. The whole point of both these tools is that we don't want to ever download the same file twice.

Thanks.
Yes, that info helps.  What OS are the clients running?  98?  XP?  2000?
-----8<-----[FTP-LS.SCR]
username
password
cd /directory1/directory2/etc/etc
ls
quit
-----8<-----[FTP-LS.SCR]



This begins the second FTP script.
-----8<-----[FTP-GET.SCR]
username
password
cd /directory1/directory2/etc/etc
lcd c:\local\directory\path
get filename.ext
delete filename.ext
quit
-----8<-----[FTP-GET.SCR]


This is the batch file to place on the user's computer (the FTP Script files must be in the same directory)
-----8<-----[CHECK4FILE.CMD]
@echo off
ftp -s:ftp-ls.scr ftp.host.com > %temp%\ftp-ls.tmp 2>&1
find /i "filename.ext" "%temp%\ftp-ls.tmp"
If "%errorlevel%" == "1" (
Net Send %computername% File does not exist
Goto End
)
net send %computername% File downloaded
ftp -s:ftp-get.scr ftp.host.com
:End
del %temp%\ftp-ls.tmp
-----8<-----[CHECK4FILE.CMD]
The first part (upload only if the .mdb doesn't already exist on the server) will be run on xpPro.

The second part (delete after file has been downloaded) will also be run on w2000.


The first part has changed a bit since my last description, sorry. It seems like it should still work, though.
Now that I read that, I want to make sure: If what we are trying to do is create a tool which will allow the user to upload a file ONLY if the file doesn't currently exist on the server (no over-writing of this file allowed), then what you came up with will work, right?
and I see I said a misleading word in explaining the os

to reiterate: first script runs on xpPro, second on w2000
Your solution is exactly along the lines of what I asked for, sorry for the non-precise outlining of what I need. If you can help any more based on what I have finally outlined, that would be great.
I'm confused - you want to UPLOAD and DOWNLOAD?

If you can, please be as specific and detailed in a step-by-step description as possible.  Otherwise, there's a chance for greater miscommunication and a possibility that we end up deleting something we shouldn't or not doing something we should.

The script I wrote for you will:
1.  connect to the FTP server via the FTP-LS script and see if a file exists.  It does this by changing into the appropriate directory and listing the contents of that directory.  That listing is sent to a text file, ftp-ls.tmp in the temp directory.
2.  the find command looks through the ftp-ls.tmp file for a line that includes the name of the file you are looking for.  If it does not find one, then it sets a special dos error variable, %errorlevel% to 1.
3.  The script then checks to see if the %errorlevel% is 1.  It is if the file was NOT found.  If the file was NOT found, then a windows message box pops up on the user's computer with the message that the file does not exist.  The script then ends.
4.  If the errorlevel is NOT 1, then it's almost certainly 0 - meaning that find successfully found a line with the requested file name.  Thus, the file exists on the FTP server.  Since it exists, we continue on...
5.  A message pops up saying the file has been downloaded.  (This should be changed in the script and come AFTER the second FTP statement)
6.  Another FTP script runs, written to retrieve the desired file.  This script logs in to the FTP server, changes into the approprate directory (CD command) on the FTP server, then changes into the appropriate local directory where you want to download the file to (LCD command) on the local computer.
7.  In the end, whether the file is found or not, the temp file created is removed.
6a. (forgot this in the last post) The script then downloads the file and after it downloads it will DELETE the file.
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
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
Sorry. I realized how screwed up that was afte posting it, and I should have been more careful before beginning. Yes, I need to both upload and download.

The XPpro script will upload the .mdb only if the .mdb doesn't already exist on the server. If it does exist, an error pops up saying "File already exists" (I know, I know. Sorry)

The win2000 script will delete the .mdb after file has been downloaded to the win2000 pc, and say something like "File Downloaded".

Again, the point of all this is to prevent over-writing of the file when two different users on two different pc's could attempt to upload/download at any moment, neither of them aware of what the other is doing. Here's the nutshell (the REAL one):

The XPpro pc CANNOT upload the file until the win2000 pc has downloaded it.

or, in other words...

The win2000 pc HAS to download the file before the XPpro pc can upload it.

It seems like your script will work for all this with minor tweaking, but I of course could be wrong.
increasing point value...
OK, here's what is happening with these as I test them:

Upload (XPpro): if file exists, error pops up, ends (all good)
Upload(XPpro): if file doesn't exist, it looks like it connects, and a message comes up saying "file doesn't exist-uploading", then, after I acknowledge the first pop-up, right away another message saying "file exists". Then, if I look for the file on our server, it isn't there. So maybe it is putting it and then immediately deleting it?

Download (win2000): If file doesn't exist, nothing happens except a quick flash of the window. Can it say a message there before it ends like "File is not on the server"?
Download (win2000): If file does exist, it works great, downloads the file, but it does not delete the file from the server after it is done. Also, can there be a message saying "File downloaded" and can I specify the download path? It is currently placing it in the same directory as the script.

P.S. - Do I have to worry about the XP/w2000 difference, or are these interchangable?

This is very very close...Thanks for all your help.
XP/2000 - doesn't matter - would only make a difference if one was 9x/Me.

Try running the scripts from a command prompt (start - run - "cmd.exe") and then type the script name there - this way the window won't close and you can see what's happening.  I tested the second script with my own FTP site and it worked fine for me.  We could just be missing something with you, but running if from the dos box will make things clearer (NOTE: Windows 2000 and XP have two command interpreters - command.com and cmd.exe - by default this runs as a CMD.EXE dos box, but if you open a dos box manually, make sure it's one that is using cmd.exe and NOT command.com (command.com is more like the 9x/Me environment.
I tried the download script (the path being C:\>OMFOmdb_download\CHECK4FILE.CMD) from cmd.exe, and this is what i get (wheteher the file exists on the server or not):

---------- C:\DOCUME~1\ADMINI~1\LOCALS~1\TEMP\FTP-LS.TMP
No File


If I just click on the script itself (CHECK4FILE.CMD), it works, except for what I outlined above (doesn't delete from server after downloading, I need to specify local path, no message saying "File Downloaded")
Commuting home now - will try to work on this for/with you in a few hours.  
Thanks much. I'll be here.
I put a PAUSE in right before the net send %computername% File Already Exists in UPLOADFILE.CMD.

I removed the lcd C:\FieldOffice\ line from FTP-PUT.SCR and placed the .mdb in the same folder as the upload scripts and it did upload the .mdb. FTP-PUT.SCR looked like this before:

sca
password
cd /usr/www/users/sca/OM_data/
lcd C:\FieldOffice\
bin
put QMExportFromFieldOffice.mdb
quit

so at least that's one thing - the lcd isn't finding the local file
As far as the two messages one after the other, I fixed it - Goto End was missing from UPLOADFILE.CMD

I added the net send messages where I want them, all is good, except two things:

1-the file is not being deleted from the server after downloading (this is critical)
2-the local paths are still an issue. It wasn't uploading before because it was looking in the same folder for the .mdb. Same way the download won't work unless it's in the same folder. Can I specify local upload source/download destination locations, or do they have to be in the same folder as the scripts?

Last thing, the net send is cool, but how do I make the first message force the user to wait, then when activity is done,  the box says "done" (like a file download in IE)
Try running the FTP script that has the "delete" command manually. "ftp ftp.host.com"
Then do everything manually (line by line from the script).  What happens when you try executing the command "delete filename.ext"?  It MAY be that the account you are logging in as does not have write permissions (do you know if this is a unix or Windows FTP server?  May not ultimately matter.  But I believe when "put" files, default permissions on most unix systems are owner Full, everyone else read + execute.  Which means if you don't use the SAME account to upload and download, then you may not be able to delete the file after downloading it.

2.  Not sure about this.  LCD is supposed to set the current local directory to another location.  I've used this numerous times in the past quite successfully.  Again, try running the scripts manually.  The Windows command line FTP supports most of the standard FTP features.  Simply open a connection and type ? or help and a list of commands will be displayed.  By running the scripts manually we can see error messages and other issues as they occur.  One thing.  Try using directory paths without spaces.  For example if your files are stored in c:\program files\my program\data, use c:\progra~1\myprog~1\data  (dir /x should list the short file names where necessary).

I know of no way to force the user to wait.  Again, to have the message appear AFTER the download is done, place the net send immediately AFTER the ftp command.
the lcd isn't working. Ive tried enclosing the path with [], Ive tried putting ftp> in front of lcd, nothing works. Is it in the right spot, below the cd line, just above the bin line which is just above the put line? Is that correct placement
the delete after download is working fine, my screwup. This looks like it would delete it from my local system though. Something about how these paths are defined is tripping me up from understanding all this. The lcd line below doesn't appear to have any affect on the script, it just downloads it to the script directory.

Is this right?:

sca
password
cd /usr/www/users/schanna/OM_data/
lcd [C:\FieldOffice\]
bin
get QMExportFromFieldOffice.mdb
delete QMExportFromFieldOffice.mdb
quit
i got the upload file working, just specified the full path after put.

On the download/get one, (the one above), I can't see where to get the .mdb into a folder other than the script folder. Like I said, not an issue anymore with upload/put (don't need to cd)
Hey! I figured a way to get around the lcd path issue. I just added a copy line to CHECK4FILE.CMD and now as part of the whole thing it will copy the .mdb from where the script lives to where I want it to be. Perfectly fine. I would call this one done.

Last thing: you know of no other way to alert/message/inform the user that "activity is taking place, please stand by"? My concern is that they will get the first one saying "Uploading", click ok, then do something stupid like unplug the computer before the scripts can finish. If you have any thoughts on this, great. If not, I will mark this as done and done and done. Many thanks to you, leew, for putting up with all the inconsistencies on this one. I'll make sure of what I want first next time.
My only suggestion is to put in a few ECHO lines with a message to the user.  You can also consider scheduling the job - that MIGHT hide the DOS Box.
leew - I hide the dos box by creating a shortcut to the .cmd file then running it minimized. All is great except the net send limitation. I will poke around to see if there is an alternative. I have accepted an answer from you and scored you mega-high - you have been great. Thanks much.

Bobby
Glad to help - if I come across anything, I'll let you know.