<http://www.netikka.net/tsneti/info/tscmd084.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Sat 28-Jan-2017 11:42:20

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



84} How can I automate downloading files from an ftp-site?

This example got the following files from the Garbo program archives at the University of Vaasa, Finland.
(Even if the Garbo library now is gone, the method naturally stays valid.)
 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
  @echo off & setlocal enableextensions
  echo open garbo.uwasa.fi>"%temp%\ftp$$$.tmp"
  echo anonymous>>"%temp%\ftp$$$.tmp"
  echo myid@mypc.mydomain>>"%temp%\ftp$$$.tmp"
  echo binary>>"%temp%\ftp$$$.tmp"
  echo cd /pc/link>>"%temp%\ftp$$$.tmp"
  echo literal pasv>>"%temp%\ftp$$$.tmp"
  echo lcd C:\_MyLocalDownloadsFolder>>"%temp%\ftp$$$.tmp"
  echo get tsbat.zip>>"%temp%\ftp$$$.tmp"
  echo get tscmd.zip>>"%temp%\ftp$$$.tmp"
  echo quit>>"%temp%\ftp$$$.tmp"
  ftp -s:"%temp%\ftp$$$.tmp"
  del "%temp%\ftp$$$.tmp"
  endlocal & goto :EOF
Or, written slightly differently for better clarity
  @echo off & setlocal enableextensions
  set scr_=%temp%\ftp$$$.tmp
  > "%scr_%" echo open garbo.uwasa.fi
  >>"%scr_%" echo anonymous
  >>"%scr_%" echo myid@mypc.mydomain
  >>"%scr_%" echo binary
  >>"%scr_%" echo cd /pc/link
  >>"%scr_%" echo literal pasv
  >>"%scr_%" echo lcd C:\_M
  >>"%scr_%" echo get tsbat.zip
  >>"%scr_%" echo get tscmd.zip
  >>"%scr_%" echo quit
  ftp -s:"%scr_%"
  del "%scr_%"
  endlocal & goto :EOF

The clearest option, however, probably is
  @echo off & setlocal enableextensions
  set scr_=%temp%\ftp$$$.tmp
  (
    echo open garbo.uwasa.fi
    echo anonymous
    echo myid@mypc.mydomain
    echo binary
    echo cd /pc/link
    echo literal pasv
    echo lcd C:\_M
    echo get tsbat.zip
    echo get tscmd.zip
    echo quit
  )>"%scr_%"
  ftp -s:"%scr_%"
  del "%scr_%"
  endlocal & goto :EOF

The output used to be
C:\_D\TEST>cmdfaq
ftp> open garbo.uwasa.fi
Connected to garbo.uwasa.fi.
220->
220->        @@@@                        @@@
220->       @@  @@                        @@
220->      @@         @@@@     @@ @@@     @@        @@@@
220->      @@            @@     @@@ @@    @@@@@    @@  @@
220->      @@  @@@    @@@@@     @@  @@    @@  @@   @@  @@
220->       @@  @@   @@  @@     @@        @@  @@   @@  @@
220->        @@@@@    @@@ @    @@@@      @@ @@@     @@@@
220->
220-> Welcome to Garbo ftp archives at the University of Vaasa, Finland!
220-> You are from 85.157.105.239
220->
220-> Login as 'anonymous' and give your email address as the password.
220-> Also the first part (like joe@) will do (but use yours, *NOT* joe@).
220->
220-> If you have problems please see http://lipas.uwasa.fi/~ts/garbo.html#faq
220->
220-> You can log to Garbo also by http://garbo.uwasa.fi/
220 "garbo.uwasa.fi"
User (garbo.uwasa.fi:(none)):
331 Please specify the password.

230-> Welcoming to Garbo archives.
230->
230-> Type 'ls -CF' or 'ls -lF' or 'ls -lF |more' for the directory contents.
230-> Type 'cd /pc' to go to the main MsDos directory. May differ on mirrors.
230-> ^^^^^^
230-> No support. All the moderators have other, principal occupations.
230-> For any questions please see http://lipas.uwasa.fi/~ts/garbo.html#faq
230->
230->     +------------------------------------------------------------+
230->     ! If you have files to submit please do *NOT* upload before  !
230->     ! first carefully reading the /pc/UPLOAD.TXT instructions.   !
230->     ! Uploads without _*EMAILED*_ announcements are rejected.    !
230->     ! First see http://lipas.uwasa.fi/~ts/garbinfo/garb1996.html !
230->     +------------------------------------------------------------+
230->
230-> Your actions at Garbo are logged. If this bothers you, type 'quit' now.
230 Login successful.
ftp> binary
200 Switching to Binary mode.
ftp> cd /pc/link
250- /pc/link Linked material (to waive some version numbers)
250-
250- author.zip SW & PD authors on InterNet (linked from /pc/pd2)
250- moder.zip MsDos FTP sites & moderators (linked from /pc/pd2)
250- pd2ans.txt Garbo Frequently Asked Questions (linked from /pc/pd2)
250- tsans.txt Answers about ts-packages (linked from /pc/ts)
250- tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi, linked
250- tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi, linked
250- tsfaqn.zip Questions from Usenet and Timo's answers, linked
250- tsfaqp.zip Common Turbo Pascal Questions and Timo's answers, linked
250 Directory successfully changed.
ftp> literal pasv
227 Entering Passive Mode (193,166,120,5,74,76)
ftp> lcd C:\_M
Local directory now C:\_M.
ftp> get tsbat.zip
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for tsbat.zip (254761 bytes).
226 File send OK.
ftp: 254761 bytes received in 0.42Seconds 603.70Kbytes/sec.
ftp> get tscmd.zip
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for tscmd.zip (223203 bytes).
226 File send OK.
ftp: 223203 bytes received in 0.38Seconds 595.21Kbytes/sec.
ftp> quit
221 Goodbye.

C:\_D\TEST>dir C:\_M\*.ZIP |find /i "zip"
22.05.2008  23:27           254,761 tsbat.zip
22.05.2008  23:27           223,203 tscmd.zip

If you need to include sensitive user and password information into the script, see Item #182.

Another question from the Usenet news:
> ftp client in Windows can show me the DIR of a[n] ftp folder.
> Switching to FTP mode by typing FTP in the dosbox
> Under FTP, a "dir >C:\dir.txt" does not work for me.
> How can i send that output to a txt File?
> I want to get the directory listing into a textfile.

An example of getting the listing of Garbo's /pc/ts directory using generic logging
  @echo off & setlocal enableextensions
  set scr_=%temp%\ftp$$$.tmp
  set log_=%temp%\ftp.log
  > "%scr_%" echo verbose
  >>"%scr_%" echo open garbo.uwasa.fi
  >>"%scr_%" echo anonymous
  >>"%scr_%" echo myid@mypc.mydomain
  >>"%scr_%" echo cd /pc/ts
  >>"%scr_%" echo verbose
  >>"%scr_%" echo ls -lF
  >>"%scr_%" echo verbose
  >>"%scr_%" echo quit
  ftp -s:"%scr_%">"%log_%"
  type "%log_%"
  for %%f in ("%scr_%" "%log_%") do if exist %%f del %%f
  endlocal & goto :EOF
If you wish to keep the directory listing, naturally don't delete the %log_% file.

What is in the above is based on general FTP logging. However, if you indeed wish to store the directory listing in particular, then
  @echo off & setlocal enableextensions
  set scr_=%temp%\ftp$$$.tmp
  set log_=%temp%\ftptsdir.log
  > "%scr_%" echo verbose
  >>"%scr_%" echo open garbo.uwasa.fi
  >>"%scr_%" echo anonymous
  >>"%scr_%" echo myid@mypc.mydomain
  >>"%scr_%" echo dir /pc/ts "%log_%"
  >>"%scr_%" echo quit
  ftp -s:"%scr_%"
  more "%log_%"
  for %%f in ("%scr_%" "%log_%") do if exist %%f del %%f
  endlocal & goto :EOF

> How do I just test if a file exists on an FTP site?
All the ingredients have more or less already been covered here, but below is nevertheless an example.
  @echo off & setlocal enableextensions
  ::
  :: Set some environment variables

  set scr_=%temp%\ftp$$$.tmp
  set log_=%temp%\ftpdir.log
  set find_=tscmd.zip
  ::
  :: Build and send the FTP commands

  (
    echo open garbo.uwasa.fi
    echo anonymous
    echo myid@mypc.mydomain
    echo cd /pc/link
    echo ls . "%log_%"
    echo quit
  )>"%scr_%"
  ftp -s:"%scr_%" > nul
  ::
  :: Was the log created?

  if not exist "%log_%" (
    echo The connection has probably failed
    goto _cleanup)
  ::
  :: Check the outcome

  find "%find_%" "%log_%" > nul
  if %errorlevel% EQU 0 (
    echo Found file "%find_%" on the FTP site
    ) else (
    echo File "%find_%" not found on the FTP site)
  ::
  :: Clean up

  :_cleanup
  for %%f in ("%scr_%" "%log_%") do if exist %%f del %%f
  endlocal & goto :EOF

The output could be e.g.
  C:\_M>C:\_D\TEST\CMDFAQ.CMD
  Found file "tscmd.zip" on the FTP site


References/Comments: (If a Google message link fails try the links within the brackets.)
  Google Groups Nov 22 2008, 11:21 pm [M]
  Google Groups 12 Jan 2009 18:50:13
  Using FTP Batch Scripts Microsoft Help and Support