FTP list

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
codemonkey
Posts: 6
Joined: 02 Dec 2009 15:40

FTP list

#1 Post by codemonkey » 02 Dec 2009 15:48

Is there a way to run do ftp ls and get the list back into the batch file?

I'm calling:
:FTPSetup
> script.ftp ECHO lcd "%LCL_PATH%"\%folderpath%
rem >> script.ftp ECHO cd "%BKUP_PATH%"/%folderpath%
>> script.ftp ECHO binary
>> script.ftp ECHO ls
>> script.ftp ECHO bye
pause
echo running ftp script...
FTP -s:script.ftp -i -A %FTP%
del script.ftp
goto Menu

I'd like to get the list of folders off the FTP server and make a menu list out of it for users to select what they want to download. I originally hard coded the menu, but to many folders get added or name changed to keep up with the script.

Thanks in advance

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 03 Dec 2009 23:09

codemonkey,

This little batch should get you the list of directories and show them as a menu with numbers in front of each directory name.
The user can then chose a number. The number is resolved to the directory name and shown on the screen.
Not that the ftp script is embedded into the batch script, so no extra ftp file is created.

Code: Select all

@goto:Smartstart
open example.com
[user]
[pass]
cd public_html
binary
dir
bye

:Smartstart
@echo off
Setlocal Enabledelayedexpansion
echo.Querying Remote Directories - this may take a while...
set list[#]=-1
for /f "tokens=8,*" %%A in ('ftp -i -s:"%~f0"^|findstr /b "d"') do (
    set /a list[#]+=1
    set "list[!list[#]!]=%%B"
)

echo.Listing of Remote Directories
for /l %%N in (0,1,!list[#]!) do (
    echo.  %%N  !list[%%N]!
)
set /p "choice=Select a dirctory: "
echo.
echo.You have chosen: %choice%  !list[%choice%]!
pause


Hope this helps :wink:

codemonkey
Posts: 6
Joined: 02 Dec 2009 15:40

#3 Post by codemonkey » 04 Dec 2009 09:39

Thanks,

It seems to connect, but doesn't actually show me the directories. Will it make a difference if some of the directory names have spaces in them? I did remove the user and password and added -A on the connection:

ftp -i -A -s:"%~f0"^|findstr /b "d"

I change the 'cd' to my folder location. I do get the connection message, then it says listing directories, but then doesn't show any and goes to the user input. I'll keep working with it and see if it's on my end. Thanx a ton, if I can get it to work it's exactly what I need.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#4 Post by DosItHelp » 06 Dec 2009 01:31

Hey codemonkey,
What does the output of the 'dir' command look like on your box when you connect manually?
Mine looks like this:

Code: Select all

ftp> dir
200 PORT command successful
150 Opening ASCII mode data connection for file list
-rwxr-xr-x   1 441219   15000           0 Jan 14  2008 .track
drwxr-xr-x   2 441219   15000        4096 Dec 30  2008 cache
drwxr-xr-x   4 441219   15000        4096 Nov 26  2008 public_ftp
drwxr-xr-x  44 441219   15000        8192 Nov  5 21:49 public_html
-rwxr-xr-x   1 441219   15000          20 Jan 10  2008 stats_log.gz

The findstr /b "d" command ensures only lines that start with the letter "d" are being processed, which on my box means d like directory. May be this doesn't work for you?

codemonkey
Posts: 6
Joined: 02 Dec 2009 15:40

#5 Post by codemonkey » 07 Dec 2009 09:02

i have to change folder names, but it looks like the following. my ftp script also does a cd to go down 2 folders, but this is at the root.

ftp> dir
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
11-19-09 02:39PM 73 10.0.200.50.url
12-03-09 06:02PM <DIR> Folder1
12-01-09 10:47AM <DIR> Folder2
09-14-09 06:25PM <DIR> Folder3
226 Transfer complete.
ftp: 2579 bytes received in 0.00Seconds 2579000.00Kbytes/sec.
ftp>

codemonkey
Posts: 6
Joined: 02 Dec 2009 15:40

#6 Post by codemonkey » 07 Dec 2009 09:10

i tweaked yours a bit and got it to work. I did the menu display and the list all in 1 loop. Part of my problem was that folder names can have spaces, so I was using " " around the names, but the For wouldn't work. Once I put ' ' around the folder name it started to work. Is there a way to check the number in the list?

I notice that If i set the LocalRoot and I us ' ' it doesn't end up using it, but I can't get the For to work if I use " ". Any ideas to use folder path with spaces in it?

Here's my version of the loop:

Code: Select all

For /f "tokens=*" %%i IN (%FilePath%) DO (
    Echo !count!. %%i
    set /A count+=1
   
    rem add entries to a list
    set /A list[#]+=1
    set "list[!list[#]!]=%%i"
)
set /p "userinp=Type the number to download: "
echo.
echo.Preparing to download: !list[%userinp%]!

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#7 Post by DosItHelp » 07 Dec 2009 19:39

May be like this?

FTP output dumped into a text file named ftpoutput.txt:

Code: Select all

ftp> dir
200 PORT command successful.
150 Opening ASCII mode data connection for /bin/ls.
11-19-09 02:39PM 73 10.0.200.50.url
12-03-09 06:02PM <DIR> Folder1 with space
12-01-09 10:47AM <DIR> Folder2
09-14-09 06:25PM <DIR> Folder3 with space
226 Transfer complete.
ftp: 2579 bytes received in 0.00Seconds 2579000.00Kbytes/sec.

Batch to extract directory names:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "FilePath=_postproc.txt"

set /a count=1
For /f "tokens=3,*" %%i IN ('findstr "<DIR>" "%FilePath%"') DO (
    Echo !count!. %%j
    set /A count+=1
   
    rem add entries to a list
    set /A list[#]+=1
    set "list[!list[#]!]=%%j"
)
set /p "userinp=Type the number to download: "
echo.
echo.Preparing to download: !list[%userinp%]!

Batch output:

Code: Select all

1. Folder1 with space
2. Folder2
3. Folder3 with space
Type the number to download: 3

Preparing to download: Folder3 with space

codemonkey
Posts: 6
Joined: 02 Dec 2009 15:40

#8 Post by codemonkey » 08 Dec 2009 14:27

I'm actually doing: dir -1 dirfile.txt so I only get the list w/o all the extra stuff.

The problem is the actual %FilePath% variable. if it's c:\folder with space\dirfile.txt then it fails to find the file. I've tried putting "" around it, but still fails to find it.

I have found a temp solution. I do an If Exists on the %FilePath% and if it doesn't exist I create it. Immediately following I do cd %FilePath%. This causes the dirfile.txt to get created into the same folder so the For loop doesn't need the whole file path. Not sure if this is the right approach, but seems to be working correctly.

Thanks again for all the help.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#9 Post by DosItHelp » 08 Dec 2009 21:24

Try:
For /f "usebackq tokens=*" %%i IN ("%FilePath%") DO ( ...

codemonkey
Posts: 6
Joined: 02 Dec 2009 15:40

#10 Post by codemonkey » 09 Dec 2009 09:23

that worked, thanks a ton.

another quick question.

If the folder I want to copy exists I need to ask the person if they want to overwrite it. I was trying to do:

echo Folder Exists, would you Like to Overwrite it (Y/N)?
choice /c:yn

but always get the error:
'choice' is not recognized as an internal or external command,
operable program or batch file.

Thanks in advance

Post Reply