FTP - in one batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

FTP - in one batch file?

#1 Post by Adrianvdh » 27 Sep 2013 05:36

Hello everyone...

I am having some issues with this batch file...

(http://www.dostips.com/?t=Batch.FtpBatch)

Code: Select all

set "myvar=%~d0%~p0File v%currentversion%.bat"
@ftp -i -s:"%~f0" >nul 2>nul &goto :EOF
open mywebsite.com
anonymous
anonymous
cd  public_html/public
ascii
get "file.bat" "%myvar%"
quit


As you can see I have changed some stuff :)

But I don't think to make it run without showing the output result code [>nul 2>nul] is working
and how do I use variables as well, I remember when using ftp with 2 batch files, it would echo the directory as a variable.

Thanks :)

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: FTP - in one batch file?

#2 Post by Adrianvdh » 27 Sep 2013 08:59

Anyone?

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

Re: FTP - in one batch file?

#3 Post by aGerman » 27 Sep 2013 10:40

You can't since ftp.exe doesn't expand environment variables. You can write a temporary ftp script via batch and pass it to ftp.exe.

Regards
aGerman

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: FTP - in one batch file?

#4 Post by penpen » 27 Sep 2013 11:05

If you want to avoid temporary file(s), you could also pipe the needed data to ftp.exe:

Code: Select all

setlocal
set "myvar=%~d0%~p0File v%currentversion%.bat"
(
  echo open mywebsite.com
  echo anonymous
  echo anonymous
  echo cd  public_html/public
  echo ascii
  echo get "file.bat" "%myvar%"
  echo quit
)|ftp
endlocal
Although i would prefer in this case aGermans solution as this is better for debugging (and should be nearly the same source).

penpen

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

Re: FTP - in one batch file?

#5 Post by foxidrive » 27 Sep 2013 11:23

I just want to comment (again) that anyone who lived through the MS-DOS days knows just how many
temporary files were used back then, and laughs at the youngsters these days who want to avoid using them.

Back then temporary files were created on far slower hard drives (and floppies!!) and with far slower CPUS - and the files weren't an issue. (ok, we *had* to use them too)

Nowadays temporary files are created on super fast SSDs (and swift HDD) and with super quick CPUs - so what's all the fuss over temporary files, I say!

s'funny. :)

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: FTP - in one batch file?

#6 Post by Adrianvdh » 27 Sep 2013 11:51

Thanks penpen, I will test that in a moment :)

But how would I check if ftp.exe downloaded the file? I have tried to use this...

Code: Select all

mode 80, 30
@ftp -i -s:"%FTPDown%" & goto :downloadupdatefileFTPcheck
::@ftp -i -s:"%FTPDown%" >nul 2>nul& goto :downloadupdatefileFTPcheck

:checkUpdateWriteFTP
::call :checkupdateps1DownUAC
if exist "%FTPDown%" del "%FTPDown%"
(echo open %updatefileURL%
echo %FTPUser%
echo %FTPPass%
echo ascii
echo get "%updatefilename%" "%newversionupdatefile%"
echo disconnect
echo bye )>>"%FTPDown%"
exit /b

:downloadupdatefileFTPcheck
[color=red]for /f "tokens=*" %%i in ('@ftp -i -s:"%FTPDown%"^|findstr /i "Can't open"')[/color]
if "%errorlevel%"=="0" (goto downloadupdate1 ) else (
if exist "%newversionupdatefile%" (goto searchupdatefile ) else (goto downloadupdate1 ) )


But no luck, any idea? Thanks guys your always helpful :D

P.S. Sorry about the mess I am just debugging :)

P.P.S WT FAQ? Why won't phpBB allow me to colour/color stuff?
Last edited by Adrianvdh on 27 Sep 2013 17:12, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: FTP - in one batch file?

#7 Post by ShadowThief » 27 Sep 2013 15:41

Adrianvdh wrote:P.P.S WT FAQ? Why won't phpBB allow me to colour/color stuff?

because the code tag breaks formatting

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: FTP - in one batch file?

#8 Post by Adrianvdh » 27 Sep 2013 15:54

ShadowThief wrote:
Adrianvdh wrote:P.P.S WT FAQ? Why won't phpBB allow me to colour/color stuff?

because the code tag breaks formatting


I do not understand?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: FTP - in one batch file?

#9 Post by ShadowThief » 27 Sep 2013 16:58

test

Code: Select all

[color=red]test[/color]


Same BBcode, but the code tag prevents the color tag from being used.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: FTP - in one batch file?

#10 Post by Adrianvdh » 27 Sep 2013 17:10

Oh ok thanks :). But could e get back to topic? I cannot get penpen's code to work.

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

Re: FTP - in one batch file?

#11 Post by foxidrive » 27 Sep 2013 23:27

Adrianvdh wrote:But how would I check if ftp.exe downloaded the file? I have tried to use this...


There is no simple way to check if a file has completely downloaded - except by capturing the FTP transfer chatter and parsing the result, to see if there is a success message,
or by capturing and parsing the FTP directory listing to get the filesize and then comparing the filesize of the downloaded copy.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: FTP - in one batch file?

#12 Post by Adrianvdh » 28 Sep 2013 05:29

Can't I just findstr "Can't open" in the output result?

That is the problem I have, and I can't get the code I posted to work. Also penpen's code is not working, how can I do what he did, using variables and not making a ftp script?

Thanks :)

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

Re: FTP - in one batch file?

#13 Post by foxidrive » 28 Sep 2013 06:44

This doesn't work here with my ftp server and user/pw details.

Code: Select all

@echo off
(
echo open ftp.domain.com
echo myuser
echo mypassword
)|ftp



I get this - but it works if I log in interactively.

User (ftp.domain.com:(none)): Password:
Login incorrect.
Login failed.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: FTP - in one batch file?

#14 Post by Adrianvdh » 28 Sep 2013 07:16

Same here I also get that :), but it is not what I am looking for. Thanks anyway :D

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

Re: FTP - in one batch file?

#15 Post by aGerman » 28 Sep 2013 09:30

Code: Select all

@echo off &setlocal
(
  echo open ftp.domain.com
  echo myuser
  echo mypassword
  echo disconnect
  echo bye
)>"%temp%\test.ftp"
for /f "delims=" %%i in ('ftp.exe -i -s:"%temp%\test.ftp" 2^>^&1 ^| findstr .') do echo %%i
del "%temp%\test.ftp"
pause

... should pipe everything to FINDSTR including error messages. If that works try to modify the FINDSTR portion using /ic:"Can't open".

Regards
aGerman

Post Reply