Question Locked

Batch files

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I needed to code a batch file to first open CMD.EXE then go to D:\[folder] .. then run ftp to connect to the ftp server and then login using a user name and pass .. then go into a directory .. and do a dir serch and then a mget .. I am new to batch files and use win XP ... I spent and entire day trying to do it .. but it just doesnt seem to work .. can any one help me out on how to script the .bat file ?

9 replies

by justinshattuck

Honestly, your request seems a little awkward, I guess its just my paranoia. However, you are essentially going to want to create a few files to perform the actions you request. First, a batch to execute the commands and then a file containing the sequence of FTP commands you're wishing to execute.

Example(s):

*** EXECUTE.BAT ***
@echo off <-- turns off verbosity
cls
ver > info.txt
ipconfig /all >> info.txt <-- writes i
pause
ftp -s:commands.txt
pause
exit

*** COMMANDS.TXT ***
open ftp.yourdomain.com

setup your login credentials as well as mget commands.

This should at least get you started in the right direction..

by dumphrey

the call command starts one batch file from inside another..

by rob miners

This file is saved as connect.cmd Place both files in the same folder.

================

ftp -s:SAVCEXDB.txt

================

This is the content for SAVCEXDB.txt

================

open tr.com.com ::(open FTP site)
anonymous ::(logs in as user anonymous)

SAVXDB@tr.com.com ::(password for FTP access.)

cd VDTM ::(change FTP directory to VDTM.)

lcd "C:\Program Files\SAV" ::(change local directory to where Symantec Antivirus Corporate Edition is installed. Change if installed in different directory.)

bin

hash

prompt

mget *.XDB ::(CASE is important. Must be uppercase.)

quit

================

by dr dij

we use this kind of FTP in some applications. Besides editing by hand the FTP file and command file, you can have your program create a simple text file, name it as a batch file, have the program also create the command file.

On the first run log into the FTP site and don't do ANY gets. Instead do ls command to list the FTP site files (cd first on FTP site if you need to). lcd will determine where the ls will goto on your local drive (put a file name after ls so it stores locally)

quit FTP
read the directory of the FTP site, to determine if there are any files you want to process. We went as far as checking a database table of already-processed file names, so we'd skip downloading or processing anything we had already got from FTP site (files named with date in name rather than replaced). When you DO transfer a file, the LS text file contains the date the file was posted on the FTP site and you can capture this to log files if needed, i.e. date and time a customer posted file to FTP sites, handy to avoid blame if they were late, etc.

this way you don't need to process files every day or get them overwritten, instead any files not run will run whenever your prog runs.

After you've determined what files need to be run, write a second batch file that has same commands but writes a get to the command text file for each file you want to download.

Then your prog calls second time ftp,
gets the files
and does whatever you want to do with them.

At end of run if you do whatever you want to do successfully, write to database table that you have processed this file. that way next run won't run it again.

We generally had the FTP go to a local staging directory then have the program do a directory of this staging area, read thru it and ask user to select a file that needed processing. only files that had not been run show up. Also having a local staging area means you can just pop a file in that directory if you need to do a test run for the later process and know FTP works but not have to have the slowdown of FTP xfer of large files.

Sounds complicated but takes program only a few seconds and is good pattern for error free transfer.

If going the other way and doing puts, you can use the 'ls' to ensure the file actually made it up to the FTP site and branch to an error catch or trap if needed.

While the above was for production files at a company, I also use something similar to get my raw access log files from the web sites I created. This doesn't need the 'ls' as the file name is always the same on the website. However they reset the file periodically so I have it setup to load every hour. I have the command file created each time and append the date plus time to the download file.

Then I read thru this text file, generate a unique key for each record based on time and date of access and write to a web site access database, skipping duplicates I wrote previously, and steering robot accesses (based on 4 octets of the IP address, and if not present 1st 3 octets) from a lookup table compared to the ip in the record, and if present writing a robots stats file instead. I have to do this as not all robots identify themselves in the UA and progs like awstats or analog, etc don't separate them out.

by adnan.m.rehman

Hey,

Thanks ... u seemed to have got exactly what I wanted out of the batch file ... I got the batch file to run using a ftp -s:(ftp site) ... it logs in and stuff ... but i am having difficulity to get it to do the rest of the stuff u said like make a list of the dir .. and then ask me which ones i wanna do a mget on .. can u help me with a code for that ?

by bizzo

Maybe you could post what you have so far and tell us where it goes wrong.

Obviously amending any servernames or passwords.

by adnan.m.rehman

well I have got to a point where i get it to log into the ftp server .. but i still need a code that will copy the directory list onto something .. then ask me which files i wanna do a mget for and then do the mget.

by adnan.m.rehman

Thanks every one .. it helped ... i got my batch file up to pull a script from another file ... the only place where i am stuck now is getting the batch file to copy the list of files on the dir ... and asking me which ones i wanna do a mget on and then do it ..

This discussion is closed to new replies.