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

Re: FTP - in one batch file?

#16 Post by Adrianvdh » 28 Sep 2013 12:34

Thanks it works :)

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

Re: FTP - in one batch file?

#17 Post by penpen » 28 Sep 2013 15:26

Sorry that i answer that late, but i had not the time to do it earlier.

foxidrive wrote: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. :)
My first contact with MSDOS 5.0 was in 1993, when i was 16.
My first (real == non school) programs i have build when i was 18 (borland turbo pascal, c++ and DOS scripts). So if that fulfill to be a youngster i cannot change it... .
In the company i worked that days, the access to the MS-DOS pc was limited (in time), so we were ordered by our supervisors to program that stuff as fast as possible, and we were told (beside other points):
"Piping (not real piping, but ms called it so) was faster than storing to file and reading from file.
Working with files should be done only in that cases where it can't be done in another way."
This was true in that old days with the (from nowadays view) super slow hdds, so it WAS useful to program it this way, and no curiosity to laugh about (my opinion only).

Nowadays the hdds are faster than the (now real == faster) piping mechanism, so it is not first choice anymore to pipe data, but if you want to avoid using temp files, for example if writing to disk is disabled, then it is still useful and there is no alternative to do it in anotherway (in such a case).

Adrianvdh wrote:Also penpen's code is not working, how can I do what he did, using variables and not making a ftp script?
I remembered doing it in this way on MS-DOS 6.22, and have tested it on the Windows 7 home pc of a friend (network party) where it just worked, so i thought it would work on all other windows/dos versions inbetween without testing it...
i've just tested it on my WinXP home and it doesn't work there and
actually i'm even not sure if there was installed another ftp download tool.
It doesn't came in my mind yesterday so in whole: Sorry for that.

Although: I think it is strange that all seems to be passed to/processed by the (WinXP home MS-DOS) ftp.exe except the password input, all other commands seem to work correct even if piped, maybe another one knows why.

penpen

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

Re: FTP - in one batch file?

#18 Post by foxidrive » 28 Sep 2013 19:19

penpen wrote:but if you want to avoid using temp files, for example if writing to disk is disabled


The people asking for solutions with no temporary files aren't in that category. :)


i've just tested it on my WinXP home and it doesn't work there and

Although: I think it is strange that all seems to be passed to/processed by the (WinXP home MS-DOS) ftp.exe except the password input, all other commands seem to work correct even if piped, maybe another one knows why.

penpen


FWIW:

I tested it in XP and Vista and Win 7, and it paused for a password in each case. It didn't accept the piped password.

In Win 8 it didn't pause, but the password wasn't entered.

Rydell
Posts: 11
Joined: 02 May 2013 17:45

Re: FTP - in one batch file?

#19 Post by Rydell » 28 Sep 2013 21:27

Just posting my 2 cents. If I'm understanding correctly, you want your batch to include automatically logging into your server, grab the file you want, then verify it was downloaded(but not its integrity)? If so, the following works for me:

@ftp -i -s:"%~f0"&GOTO continue
open ftp.myserver.com /v
Username
Password
lcd "C:\Program Files\MyProgramFolder"
mget MyProgram.exe
bye

:continue
cls
@echo off
IF EXIST "C:\Program Files\MyProgram\MyProgram.exe" echo File Downloaded.
IF NOT EXIST "C:\Program Files\MyProgram\MyProgram.exe" echo File Not Downloaded.

pause

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

Re: FTP - in one batch file?

#20 Post by foxidrive » 29 Sep 2013 06:07

Rydell wrote:then verify it was downloaded(but not its integrity)? If so, the following works for me:

IF EXIST "C:\Program Files\MyProgram\MyProgram.exe" echo File Downloaded.
IF NOT EXIST "C:\Program Files\MyProgram\MyProgram.exe" echo File Not Downloaded.


It could be a zero byte file, so that check is pretty pointless.

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

Re: FTP - in one batch file?

#21 Post by penpen » 29 Sep 2013 07:19

I have searched my old patterns, as i couldn't believe something has been changed to be impossible now.
I just had remebered false, using dynamic ftp script without a temporary file is done this way (working on xp 32 home/prof):

Code: Select all

@echo off
setlocal
set "myvar=README.txt README3.txt"

(
  echo open 127.0.0.1
  echo user userLoginName userLoginPassword
  echo get %myvar%
  echo quit
)|ftp -n
endlocal
goto :eof
The flaw was to automatically login on first connection, and try to send user login data as two lines.
So using the option "-n" and login using the command "user" was the solution.

We had controlled the success of the download via:

Code: Select all

if exist README2.txt del README2.txt
if exist README2.txt rem goto error handling: not deletable
rem download via script above
if not exist README2.txt rem goto error handling: not downloaded
Sometimes the size was checked, too.

Edited: If you download via ftp using this script and you want to use aGermans solution (for loop) to test if files were dwonloaded successfully you may add:

Code: Select all

  echo verbose ON
as the first ftp script building line. additionally you may use the ftp.exe option "-d".

penpen

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

Re: FTP - in one batch file?

#22 Post by foxidrive » 29 Sep 2013 08:29

Code: Select all

@echo off
setlocal

(
  echo open ftpserver.domain.com
  echo user myUsername myPassword
  echo dir
)|ftp -n
endlocal
pause
goto :eof




Yes, that works on Windows 8 too. I tested the above.

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

Re: FTP - in one batch file?

#23 Post by aGerman » 29 Sep 2013 08:37

True but to get it to work in a FOR loop is pretty complicated and dangerous.

Code: Select all

@echo off &setlocal
for /f "delims=" %%i in (
  '^(^(^
    echo verbose on^&^
    echo open ftpserver.domain.com^&^
    echo user myUsername myPassword^&^
    echo disconnect^&^
    echo bye^
  ^)^|ftp -n -d^) 2^>^&1 ^|findstr .'
) do echo "%%i"
pause

This didn't work for me at the first attempt since I have a comma in my password that I had to escape as well.

Regards
aGerman

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

Re: FTP - in one batch file?

#24 Post by Adrianvdh » 29 Sep 2013 11:22

Hi everyone.

After reading for the past 10 minutes on all your posts, period (That was just something to say :D)

I have been/am working on a batch script project for the last 9 months and 29 days (this is when I am posting this)
(I got my project idea on new years day, while being so bored and nothing else to do :))

The little section of my script is an auto updater, believe it or not.

How it works is it writes a .ps1 file to a temp directory, then uses that ps1 file to download command variables
form my website. It is just a batch file with a bunch of declared variables, such as the current version and release
date and the changelog etc, oh and the download link. Now powershell would download that in binary mode, I think
and when I open the batch file it downloaded it is just one line of code, not ascii form, I guess you could say :).

Now if the current version if false to the latest version there update available. OK so then I the main batch file
would then download the latest version.

Therefore:

Am only wanting to use ftp instead of a powershell script I found online (I think other website?) is because ftp.exe
and ftp clients can use ascii and the end result of the update it downloaded would be ascii. Maybe you don't understand?

For example php, the script is written in multiple lines going down, and a web server would read that whole file in one line.
So that is why I need to have so MANY more lines of unnecessary code to use ftp in my batch file.

If you can help me with the powershell script to download in ascii, that would be great :)

Code: Select all

param($url, $filename^)
 try {
    $client = new-object System.Net.WebClient
    $client.DownloadFile( $url, $filename^)
    Exit 1
 }
 catch [System.Net.WebException] {
    Exit 2
 }
 catch [System.IO.IOException] {
    Exit 3
 }
 catch {
    Exit 4
}
 Exit 0


Usage:

Code: Select all

powershell Set-ExecutionPolicy Unrestricted
powershell -ExecutionPolicy RemoteSigned -File "ps1 file" "URL" "downloaded file to local"



but I will also try aGerman's code :)

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

Re: FTP - in one batch file?

#25 Post by Adrianvdh » 29 Sep 2013 12:12

Tested aGerman's code and works :) Thanks

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

Re: FTP - in one batch file?

#26 Post by aGerman » 29 Sep 2013 12:44

Don't thank me. It's merely penpens code in a loop :wink:

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

Re: FTP - in one batch file?

#27 Post by Adrianvdh » 29 Sep 2013 12:49

Oh ok. But I don't understand it really, because this is my first language. But I do know more that I have learned over the years :)

So could you explain it. I don't really know how to understand this type of advanced code.

BTW how long would it take to learn how to code this advanced? I am completely stuck :(

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

Re: FTP - in one batch file?

#28 Post by aGerman » 29 Sep 2013 13:36

To understand the code it's important to know that the expression enclosed into single quotes ('...') is executed in a separate instance of cmd.exe. For that reason you have to escape all characters with a special meaning. Otherwise these characters are already interpreted in the first instance of cmd.exe and your batch file crashes.

how long would it take to learn how to code this advanced?

I can't answer this. I was a teenager when Windows was born. So I learned over the years and believe me you will never stop to learn.

Regards
aGerman

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

Re: FTP - in one batch file?

#29 Post by Adrianvdh » 29 Sep 2013 14:29

I know that you never stop learning, AFAIK C++ takes forever to learn, lets say a *friend* is still learning it and he has been using c++ for 7
years.

But that is C++. I am talking about Batch, which was written in C++. Influenced by BASIC and so as VB.
But I know a little of C++ and VB and HTML, I can grasp. HTML is easy but I am lazy so, I need to learn that quick.

But I think batch was a good way to go for a first language. My cousin, a year older than me knows Java, C#, C, man! Well
he got his father to help with that. Unlike me I had to learn from the internet with crappy 12 making tutorials.

But what should I approach 2nd? Java, C++? VB I can also grasp the basics. I am not really interested to code games, but
it would be fun too, I prefer to code application for the economic user. So would you be interested in telling me how many languages you
know.

if you're curious I am 16 years old and I live in South Africa. So I could not enjoy the good days when everything was still immature. Like DOS.

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

Re: FTP - in one batch file?

#30 Post by penpen » 29 Sep 2013 14:35

The powershell script looks familiar 8) .

Adrianvdh wrote:Am only wanting to use ftp instead of a powershell script I found online (I think other website?) is because ftp.exe
and ftp clients can use ascii and the end result of the update it downloaded would be ascii. Maybe you don't understand?

For example php, the script is written in multiple lines going down, and a web server would read that whole file in one line.
So that is why I need to have so MANY more lines of unnecessary code to use ftp in my batch file.
As nothing has changed since i learned it:
If you download a file in binary mode it is stored unchainged, as the bytestream is send and stored.
If you download a file in ASCII then the stream contains only ASCII data (less significand 7 bits of a byte) and so the result may have corrupted the data.
So downloading in binary mode is the better one.

The problem in your case seems to be that your server sends not the right (changed) data.
I assume the server can be set up to do what you want.
If you have no access to the servers properties, you may zip the file, or maybe it suffices, to use an known binary or unknown (by server) filetype and just rename it after downloading.

Edit:
Adrianvdh wrote:But what should I approach 2nd? Java, C++? VB I can also grasp the basics.
I recommend you to learn java it is better documented than the alternatives. (Mabe the javadoc of the old (sun) version was slightly better to read, i think; but this may be caused, because i have an old 1024x768 screen and the characters are to big there - using higher resolution the new one should be more readable).

penpen

Post Reply