Arthor
Arthor

Reputation: 674

BATCH / connect to FTP and put list of directories in an array

I have tried to do this in BAT however I think it cannot be done.

Asked already - Link: batch-ftp-list-directories-and-put-in-array

Objectives:

I am very new to VBS and it is really not my thing. However BATCH seems to be limited with FTP functions.

echo user xxx> c:\cmd.dat
echo 1234>> c:\cmd.dat
echo cd /var/www/xxx/html/TEST>> c:\cmd.dat
echo ls * c:\list.txt>> c:\cmd.dat
echo bye>> c:\cmd.dat
ftp -n -s:c:\cmd.dat xxx xxx xxx

REM This reads a file called list.txt and makes and array


REM - Removes blank lines   
For /F "tokens=* delims=" %%A in (c:\list.txt) Do (
Echo %%A >> c:\list_clean.txt )

pause
set "file=c:\list.txt"
set /A i=0

for /F "usebackq delims=" %%a in ("%file%") do (
set /A i+=1
call echo %%i%%
call set array[%%i%%]=%%a
call set n=%%i%%
)

for /L %%i in (1,1,%n%) do call echo %%array[%%i]%%

pause
del c:\cmd.dat
del c:\wfslist.txt

I need to add these fictions:

Upvotes: 1

Views: 2926

Answers (2)

Tomalak
Tomalak

Reputation: 338316

Here is my attempt at it.

This is a utility script that lists a remote directory (full paths, nothing else). It's designed to be reusable (i.e. it works with parameters) and to be called from other scripts.

Save as ftpls.bat.

@echo off
setlocal

rem USAGE: call with 4 parameters
rem ftpls hostname USER PASS path/on/remote/machine

set "script=%TEMP%\ftpscript.txt"
set "output=%TEMP%\ftplist.txt"
set "host=%1"
set "user=%2"
set "pass=%3"
set "dir=%4"

>  "%script%" echo open %host%
>> "%script%" echo user %user%
>> "%script%" echo %pass%
>> "%script%" echo cd %dir%
>> "%script%" echo ls * %output%
>> "%script%" echo bye

ftp -n -s:"%script%"

rem modify "skip" and "tokens" to select the right lines and columns
for /f "skip=1 tokens=6,*" %%a in (%output%) do (
  echo %dir%/%%b
)

del "%script%"
del "%output%"

The for loop cuts out the filename (which is the last part on every line of the listing). Choose the correct tokens parameter if 6,* doesn't work for your FTP server. 6,* means that column 6 will become %%a and the rest of the line (*) will become %%b. 6 should be the column in front of the filename.

Call it on its own like

ftpls xxx.xxx.xxx.xxx USER PASS /var/www/xxx/html/TEST

Use it in another batch file like

set cmd=ftpls xxx.xxx.xxx.xxx USER PASS /var/www/xxx/html/TEST
for /f "delims=" %%a in ('%cmd%') do (
  echo %%a
)

Upvotes: 0

Hackoo
Hackoo

Reputation: 18837

This an example in batch just for testing that can list only files from a folder located on a public ftp server like ftp.microsoft.com in order to create a list.txt file to download it after, so give a try and tell us the result.

NB : Please don't forget to add the tag Batch

@echo off
mode con cols=85 lines=22 & Color A
::***********************************
Set FTPSERVER=ftp.microsoft.com
Title Lister les fichiers et les dossiers sur un serveur FTP (%FTPSERVER%) by Hackoo
Set USER=anonymous
Set [email protected]
Set DossierFTP=/bussys/winsock/winsock2/
Set DownloadFolder=winsock2
::*******************************************************
Goto Lister
:Lister
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
>> ft.do echo ls -h TLIST.txt
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9B
echo Download la liste
pause
Goto Download
::*********************************************************
:Download
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
for /F %%f in (TLIST.txt) do ( >> ft.do echo get %%f) 
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9A
pause
echo Deplacer la liste
Goto Deplacer
::*********************************************************
:Deplacer
Set Source=%~dp0
Set Destination=%Source%%DownloadFolder%
if not exist %DownloadFolder% MD %DownloadFolder%
for /F %%f in (TLIST.txt) do (move "%Source%%%f" "%Destination%")
pause

Upvotes: 3

Related Questions