Downloading file from server over FTP

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ucancallmebiru
Posts: 2
Joined: 08 Jul 2014 22:32

Downloading file from server over FTP

#1 Post by ucancallmebiru » 08 Jul 2014 22:52

I need to download a file that is placed in a folder called abc_20140221_123456 in server1 over ftp to my local directory. The problem is that the last six characters of the folder name are not fixed. For example today the folder may be called abc_20140221_123456 and tomorrow it might be called abc_20140221_234567. I am having problems in writing an automation batch script to do the same.

Here's the script I am working on:

Code: Select all

@echo off
setlocal
set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set folderName=abc_%dateStr%_
echo open server1>>file.tmp
echo username>>file.tmp
echo password>>file.tmp
echo prompt>> file.tmp
echo binary>>file.tmp
echo lcd E:\>>file.tmp
:: Not sure how to cd to abc_20140221_* from here
echo get filename.txt>>file.tmp
echo y>>file.tmp
echo disconnect>>file.tmp
echo bye>>file.tmp
ftp -i -s:file.tmp
pause


I know that I can loop through directories using for like this:

Code: Select all

for /d %%d in (' %path%/*%folderName%* ') do (
echo get filename.txt>>file.tmp
echo y>>file.tmp
)


But "for" doesn't work inside ftp>.

Any help is highly appreciated. Thanks.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Downloading file from server over FTP

#2 Post by foxidrive » 08 Jul 2014 23:57

You will need to launch an initial FTP script to gather the folder names and then parse the log file to get the folder.

The second FTP script will need to be created on the fly to use the variable with the folder name that you need.

ucancallmebiru
Posts: 2
Joined: 08 Jul 2014 22:32

Re: Downloading file from server over FTP

#3 Post by ucancallmebiru » 09 Jul 2014 00:50

foxidrive wrote:You will need to launch an initial FTP script to gather the folder names and then parse the log file to get the folder.

The second FTP script will need to be created on the fly to use the variable with the folder name that you need.


Good to see you here as well. :D
I am looking for a simpler way to do it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Downloading file from server over FTP

#4 Post by foxidrive » 09 Jul 2014 01:22

Can you use third party tools?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Downloading file from server over FTP

#5 Post by Samir » 10 Jul 2014 23:31

I haven't tested this, but is it possible to use

Code: Select all

cd abc_20140221_*
within ftp like you can from the command line? If so, this might be of some use.

RightBehindu
Posts: 26
Joined: 23 Dec 2013 07:07

Re: Downloading file from server over FTP

#6 Post by RightBehindu » 11 Jul 2014 01:50

If you want to use thrid party tools, I suggest wget.exe
It is very helpful and easy to use.

Yury
Posts: 115
Joined: 28 Dec 2013 07:54

Re: Downloading file from server over FTP

#7 Post by Yury » 12 Jul 2014 07:00

Code: Select all

@echo off
setlocal

set server=server1
set user=username
set password=password
set "$path=//misc/files"
set "loc_dir=E:\"
set "temp_file=file.tmp"

set buildDate=%DATE:~0,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
set "folderName=abc_%dateStr%_"
(
for %%i in ("open %server%" "%user%" "%password%" "ls $%$path%$ list" "disconnect" "bye") do (
 set command=%%~i
 if defined command (
  call echo %%command:$="%%
  ) else (
  echo.
  )
 )
)>"%temp_file%"
ftp -s:"%temp_file%"
for /f "delims=" %%i in ('findstr /c:"%folderName%" list') do (
 (
 for %%j in ("open %server%" "%user%" "%password%" "prompt" "binary" "lcd $%loc_dir%$" "cd $%$path%$" "mget $%%i$" "disconnect" "bye") do (
  set command=%%~j
  if defined command (
   call echo %%command:$="%%
   ) else (
   echo.
   )
  )
 )>"%temp_file%"
 ftp -s:"%temp_file%"
 )
del list
pause



???

Post Reply