Windows ftp command: How to count number of files in remote directory?

6,197
@echo off

echo open ftp.example.com>ftp.txt
echo user>>ftp.txt
echo password>>ftp.txt
echo ls /remote/path files.txt>>ftp.txt
echo bye>>ftp.txt

ftp -s:ftp.txt

set /a LINES=0
for /f %%j in ('type files.txt^|find "" /v /c ') do set /a LINES=%%j

del files.txt
del ftp.txt

echo Number of files: %LINES%

As others have commented, using PowerShell script would be more elegant and reliable though.

Share:
6,197

Related videos on Youtube

KAE
Author by

KAE

Updated on September 18, 2022

Comments

  • KAE
    KAE over 1 year

    When I use the Windows ftp command at the command window which I think is the DOS ftp command, is there a way to count the number of files in the remote directory? That way I can check that I ftp'ed them all. I am stuck using Window 7 built-in commands and can't use a different program.

  • ashleedawg
    ashleedawg over 3 years
    the question says "count the files in a directory", not "count the lines in a file"
  • Martin Prikryl
    Martin Prikryl over 3 years
    @ashleedawg Sure. And the answer shows how to do that by saving the file list to a file and counting the lines in the file. It wouldn't be the accepted answer, if it did not answer the question. If you have a better way how to count the files in a batch file, please post your own answer.