Solved

trying to create batch file for ftp

Posted on 2004-10-05
33
740 Views
Last Modified: 2010-04-13
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
0
Comment
Question by:horstuff
  • 20
  • 13
33 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
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]
0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
Sure, NT/2000/XP - all the same (as far as script capabilty).  Just replace all "filename.ext" entries the name of your database.
0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
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]

0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
Yes, that info helps.  What OS are the clients running?  98?  XP?  2000?
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
-----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]
0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 

Author Comment

by:horstuff
Comment Utility
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?
0
 

Author Comment

by:horstuff
Comment Utility
and I see I said a misleading word in explaining the os

to reiterate: first script runs on xpPro, second on w2000
0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
6a. (forgot this in the last post) The script then downloads the file and after it downloads it will DELETE the file.
0
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 250 total points
Comment Utility
In the FTP-GET.SCR script, please add "BIN" immediately before the "GET..." line.  Failing to do this MAY result in the download being treated as ASCII and you'd have a corrupt file.  BIN tells the FTP client/server to treat the download as a binary download, ensuring the file is reconstructed correctly.

Now, if I'm reading one of your posts correctly, you want to have ANOTHER set of scripts, run from a different computer, to PUT this database file on the network.

That set of scripts would likely look like this:

The first FTP script is essentially the same.
-----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-PUT.SCR]
username
password
cd /directory1/directory2/etc/etc
lcd c:\local\directory\path
bin
put filename.ext
quit
-----8<-----[FTP-PUT.SCR]



This is the batch file to place on the user's computer (the FTP Script files must be in the same directory)
-----8<-----[UPLOADFILE.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 - uploading
ftp -s:ftp-put.scr ftp.host.com
)
net send %computername% File Already Exists
:End
del %temp%\ftp-ls.tmp
-----8<-----[UPLOADFILE.CMD]

Now, if you don't want to see the progress of the uploads and downloads, then append " > NUL" without quotes to the end of the ftp lines that include ftp-put... and ftp-get.  The lines with ftp-ls already use a redirect (>) to a file and won't display on screen.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:horstuff
Comment Utility
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.
0
 

Author Comment

by:horstuff
Comment Utility
increasing point value...
0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
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.
0
 

Author Comment

by:horstuff
Comment Utility
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")
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
Commuting home now - will try to work on this for/with you in a few hours.  
0
 

Author Comment

by:horstuff
Comment Utility
Thanks much. I'll be here.
0
 

Author Comment

by:horstuff
Comment Utility
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
0
 

Author Comment

by:horstuff
Comment Utility
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)
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
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.
0
 

Author Comment

by:horstuff
Comment Utility
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
0
 

Author Comment

by:horstuff
Comment Utility
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
0
 

Author Comment

by:horstuff
Comment Utility
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)
0
 

Author Comment

by:horstuff
Comment Utility
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
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.
0
 

Author Comment

by:horstuff
Comment Utility
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
0
 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
Glad to help - if I come across anything, I'll let you know.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now