Page 1 of 1

FTP Directory

Posted: 28 Dec 2010 15:08
by damQuick
Is there a way to get a list of all the files and Directories in a FTP Directory?

Like:

Directories:
HOST-->
-HTDOCS-->
--Dir1/
--Dir2/
--File1.txt
--File2.htm

Is there a way to get it like that? (And display it as well?)

Re: FTP Directory

Posted: 29 Dec 2010 08:36
by aGerman
I don't use ftp, but probably the (ftp-)dir command can do it for you. The permissions show if it is a file or a directory.

Regards
aGerman

Re: FTP Directory

Posted: 29 Dec 2010 09:30
by ChickenSoup
As aGerman said, when connected to the FTP host, use the DIR command. LS gives a bare format as well but will not differentiate directories from files. Did you want this scripted to display in a clean/nice format?

Re: FTP Directory

Posted: 29 Dec 2010 11:16
by damQuick
Yeah I want it to display neatly

Re: FTP Directory

Posted: 29 Dec 2010 12:56
by aGerman
Such things are terrible with ftp. You could redirect the dir-output to a file and then process it.

Code: Select all

@echo off &setlocal

set "ftpHost=ftpHost.com"
set "ftpUser=yourUserName"
set "ftpPwd=yourPassword"
set "ftpRemoteDir=HTDOCS"

(
  echo open %ftpHost%
  echo %ftpUser%
  echo %ftpPwd%
  echo dir "%ftpRemoteDir%" "%temp%\dir.txt"
  echo disconnect
  echo bye
)>"%temp%\test.ftp"
ftp -i -s:"%temp%\test.ftp"
del "%temp%\test.ftp"

echo %ftpHost%
echo -%ftpRemoteDir%/

for /f "usebackq skip=2 tokens=1,8*" %%a in ("%temp%\dir.txt") do (
  set "permissions=%%~a"
  set "name=%%~c"
  call :createtree1
)

for /f "usebackq skip=2 tokens=1,8*" %%a in ("%temp%\dir.txt") do (
  set "permissions=%%~a"
  set "name=%%~c"
  call :createtree2
)

:: del "%temp%\dir.txt"
pause
goto :eof

:createtree1
if /i "%permissions:~0,1%"=="d" (
  echo --%name%/
)
goto :eof

:createtree2
if /i "%permissions:~0,1%" neq "d" (
  echo --%name%
)
goto :eof


My dir.txt for testing was:

Code: Select all

drwx------    3 XXXXXXXX   XXXXXXXX         4096 Sep 28 14:21 .
drwx--x--x    4 XXXXXXXX   XXXXXXXX         4096 Sep 19 18:33 ..
-rw-------    1 XXXXXXXX   XXXXXXXX       204115 Sep 26 17:02 01.jpg
drwx------    2 XXXXXXXX   XXXXXXXX         4096 Sep 28 14:21 sub1
-rw-------    1 XXXXXXXX   XXXXXXXX            4 Sep 26 08:20 test.txt


The output:

Code: Select all

ftpHost.com
-HTDOCS/
--sub1/
--01.jpg
--test.txt
Drücken Sie eine beliebige Taste . . .


Hope that helps
aGerman