FTP Directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
damQuick
Posts: 5
Joined: 28 Dec 2010 14:54

FTP Directory

#1 Post by damQuick » 28 Dec 2010 15:08

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?)

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: FTP Directory

#2 Post by aGerman » 29 Dec 2010 08:36

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

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: FTP Directory

#3 Post by ChickenSoup » 29 Dec 2010 09:30

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?

damQuick
Posts: 5
Joined: 28 Dec 2010 14:54

Re: FTP Directory

#4 Post by damQuick » 29 Dec 2010 11:16

Yeah I want it to display neatly

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: FTP Directory

#5 Post by aGerman » 29 Dec 2010 12:56

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

Post Reply