More FOR loop confusion

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

More FOR loop confusion

#1 Post by Boombox » 03 Nov 2012 06:45

.
I have a text file containing links to files I regularly download...

http://download.piriform.com/ccsetup324.exe
http://download.piriform.com/spsetup118.exe
http://files.spybot.info/spybotsd162.exe


etc...

And I'd like to have them download automatically...

Code: Select all

@echo off
echo.

FOR LOOP HERE?

echo File2Download = "%VAR1%">Download.vbs
echo SaveDestination = "%userprofile%\Desktop\%VAR2%">>Download.vbs

echo Set NetFile = CreateObject("MSXML2.XMLHTTP")>>Download.vbs

echo NetFile.open "GET", File2Download, false>>Download.vbs
echo NetFile.send()>>Download.vbs

echo If NetFile.Status = 200 Then>>Download.vbs
echo Set NetStream = CreateObject("ADODB.Stream")>>Download.vbs
echo NetStream.Open>>Download.vbs
echo NetStream.Type = 1 >>Download.vbs

echo NetStream.Write NetFile.ResponseBody>>Download.vbs
echo NetStream.Position = 0 >>Download.vbs

echo Set objFSO = Createobject("Scripting.FileSystemObject")>>Download.vbs
echo If objFSO.Fileexists(SaveDestination) Then objFSO.DeleteFile SaveDestination>>Download.vbs
echo Set objFSO = Nothing>>Download.vbs

echo NetStream.SaveToFile SaveDestination>>Download.vbs
echo NetStream.Close>>Download.vbs
echo Set NetStream = Nothing>>Download.vbs
echo End if>>Download.vbs

echo Set NetFile = Nothing>>Download.vbs
Download.vbs


VAR1 Needs to be each line from my List.txt &
VAR2 Needs to be the filename.exe

Can somebody please give me a hand with this?

And is there an easier way of echoing a bunch of code (like above) to another file?
Last edited by Boombox on 03 Nov 2012 07:47, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: More FOR loop confusion

#2 Post by abc0502 » 03 Nov 2012 06:53

At first why you didn't escape the parentheses like "^(" and "^)".

To Echo multi-lines to a file do it that way:

Code: Select all

(
echo <line here>
)>>outfile
it will save you from writing ">>Download.vbs" at the end of each line.

The rest is coming soon :)

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

Re: More FOR loop confusion

#3 Post by foxidrive » 03 Nov 2012 06:54

This is easier to maintain, except the need to escape brackets, and just closing brackets really.

@echo off
(
echo 123
echo 456
echo 789 ^( xyz ^)
)>file.txt


Regarding your task - how do you know the new filename?


WGET is a good tool for automating a download too.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: More FOR loop confusion

#4 Post by abc0502 » 03 Nov 2012 07:26

Ok, here it is.

Edited Fixed line 14 & removed IF exist in the download function and replaced >> with > as Foxidrive suggested.
Edited Changed For/loop as Foxidrive Suggested

Code: Select all

@ECHO OFF

FOR /F "delims=" %%A IN (links.txt) DO Call :download "%%A" "%%~nxA"
Del /F /Q "Download.vbs" >nul
pause
Exit

:: Download Links to Specific location
:download
(
   ECHO File2Download = "%~1"
   ECHO SaveDestination = "%userprofile%\Desktop\%~2"
   ECHO Set NetFile = CreateObject^("MSXML2.XMLHTTP"^)
   ECHO NetFile.open "GET", File2Download, false
   ECHO NetFile.send^(^)
   ECHO If NetFile.Status = 200 Then
   ECHO  Set NetStream = CreateObject^("ADODB.Stream"^)
   ECHO  NetStream.Open
   ECHO  NetStream.Type = 1
   ECHO  NetStream.Write NetFile.ResponseBody
   ECHO  NetStream.Position = 0
   ECHO  Set objFSO = Createobject^("Scripting.FileSystemObject"^)
   ECHO If objFSO.Fileexists^(SaveDestination^) Then objFSO.DeleteFile SaveDestination
   ECHO  Set objFSO = Nothing
   ECHO  NetStream.SaveToFile SaveDestination
   ECHO  NetStream.Close
   ECHO  Set NetStream = Nothing
   ECHO End if
   ECHO Set NetFile = Nothing
)>"Download.vbs"

"Download.vbs"
GOTO :EOF

This batch assumes that the links will always be 3 tokens and "/" as a delims, So the file name will be the 3rd token.

BTW, This is another vb-script that download files, it's the same but yours check the availability of the webpage before download.

Code: Select all

Dim oXMLHTTP
Dim oStream
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
oXMLHTTP.Open "GET", "http://link/file.exe", False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
 Set oStream = CreateObject("ADODB.Stream")
 oStream.Open
 oStream.Type = 1
 oStream.Write oXMLHTTP.responseBody
 oStream.SaveToFile "d:\file.exe"
 oStream.Close
End If
Last edited by abc0502 on 03 Nov 2012 08:24, edited 5 times in total.

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: More FOR loop confusion

#5 Post by Boombox » 03 Nov 2012 07:36

|
Love this place, thanks ABC.
|

Line 14: 'SaveDestination' needs to be at the end of line 13:

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

Re: More FOR loop confusion

#6 Post by foxidrive » 03 Nov 2012 07:48

A minor point:

This isn't needed here:

IF Exist "Download.vbs" Del /F /Q "Download.vbs"


You can use this:

(
echo stuff
)>"Download.vbs"

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: More FOR loop confusion

#7 Post by abc0502 » 03 Nov 2012 07:52

Does that mean that the code between the ( & ) is written all at once, not like the line by line ?

@Boombox, i wrote the line 13 like that:
ECHO If objFSO.Fileexists^(SaveDestination^) Then objFSO.DeleteFile^(SaveDestination^)

Is that right ?!
Last edited by abc0502 on 03 Nov 2012 07:57, edited 1 time in total.

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

Re: More FOR loop confusion

#8 Post by foxidrive » 03 Nov 2012 07:56

Yes, in one block.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: More FOR loop confusion

#9 Post by abc0502 » 03 Nov 2012 08:00

Well, that's a new info :)

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: More FOR loop confusion

#10 Post by Boombox » 03 Nov 2012 08:03

.
There's no need for the brackets surrounding SaveDestination.

Any way we can get it to read the last token instead of the third?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: More FOR loop confusion

#11 Post by abc0502 » 03 Nov 2012 08:04

How ?!

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

Re: More FOR loop confusion

#12 Post by foxidrive » 03 Nov 2012 08:09

The file contains the URL, isn't the entire URL meant to be passed to the download script?

Instead of this:

FOR /F "delims=" %%A IN (links.txt) DO (
Set "link=%%A"
For /F "tokens=3 delims=/" %%a in ("!link!") Do Set "Fname=%%a"
Call :download "!link!" "!Fname!"
)

use this: ???

EDITED:

FOR /F "delims=" %%A IN (links.txt) DO Call :download "%%A" "%%~nA"

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: More FOR loop confusion

#13 Post by abc0502 » 03 Nov 2012 08:12

but the vbscript take two input's link and full location along with the file name

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

Re: More FOR loop confusion

#14 Post by foxidrive » 03 Nov 2012 08:13

Yes, I realised just then - and edited my post above yours.

The beauty of the for command is that you can parse a URL as well... just like a filename, I mean.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: More FOR loop confusion

#15 Post by abc0502 » 03 Nov 2012 08:18

:shock: this never came across my mind, using the %%~nA :)
Nice, i will change the code up
BTW, it should be "%%~nxA" to add the extension to the file when downloaded
Last edited by abc0502 on 03 Nov 2012 08:24, edited 1 time in total.

Post Reply