Downloading files and unzipping them

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kobryan
Posts: 6
Joined: 19 Sep 2012 20:18

Downloading files and unzipping them

#1 Post by kobryan » 19 Sep 2012 20:33

I have never tried this before but looking up stuff online for batch files seemed to be really interesting. However with that being said Im having trouble getting it to work the way I want it to.

What im trying to get my batch file to do is go to a link that will download a zip'd folder. Then I want the folder downloaded to be extracted to a usb flash drive to be usable and not in the zip format (G: Drive).

Hopefully this is possible and someone can help me out with it. I'm also looking for a resource for learning more about writing batch files, and/or books.

Thanks

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

Re: Batch file help

#2 Post by abc0502 » 19 Sep 2012 23:06

@kobryan downloading files from internet can't be done using batch, but you can use a tool like wget
or use a vbscript code along with the batch.

This is for using the wget tool with a batch file: This is NOT Tested

Code: Select all

@Echo Off & Cls & Color 0E & Title kobryan

:: set variables
set "wget=C:\Wget\wget.exe"
set "7zip=C:\Program Files\7-Zip\7z.exe"
set "url=http://www.website.com/file.zip"
set "zipFile_name=file.zip"

:: download & extract file
"%wget%" -c -b %url% /P %temp%
"%7zip%" -e "%temp%\%zipFile_name%" -o "G:\"

Exit /B


Using vbscript with Batch: EDITED this is tested and working
All What you have to do is to set the 7zip location, url, and where to extract the compressed file after downloading.
The extention of the compressed file is extracted from the URL based on the last 3 letters, you can set it by yourself
or just use it as it is.

Code: Select all

@Echo Off & Cls & Color 0E & Title kobryan & Mode 60,6

:: All settings is done here
:: File Extension is extracted from the URL, "last 3 letters" you can set it yourself or just use the code as it.
set "zip=C:\Program Files\7-Zip\7z.exe"
set "url=http://www.website.com/file.zip"
set "Destination=G:\"
set "FileExt=%url:~-3%"

:: Main Code
:: Remove previous files
IF Exist "%temp%\DownloadFolder" RMDIR /S /Q "%temp%\DownloadFolder" >nul
IF Exist "%temp%\download.vbs" Del /F /Q "%temp%\download.vbs"

:: Create Temporary Folder to download into
If not exist "%temp%\DownloadFolder" MD "%temp%\DownloadFolder"

:: Download File
Echo.&Echo.&Echo       Downloading ...
Call :Download "%url%" "%temp%\DownloadFolder\file.%FileExt%"
"%temp%\download.vbs"

:: Extract File into a USB
Echo.&Echo.&Echo       Extracting ...
"%zip%" x "%temp%\DownloadFolder\*.%FileExt%" -o"%Destination%" >nul

ClS & Echo.&Echo.&Echo       All Done.
Pause>nul
Exit /B


:: this is vbscript download script
:Download
set "in=%~1"
set "out=%~2"
(
echo Dim oXMLHTTP
echo Dim oStream
echo Set oXMLHTTP = CreateObject^("MSXML2.XMLHTTP.3.0"^)
echo oXMLHTTP.Open "GET", "%in%", False
echo oXMLHTTP.Send
echo If oXMLHTTP.Status = 200 Then
echo Set oStream = CreateObject^("ADODB.Stream"^)
echo oStream.Open
echo oStream.Type = 1
echo oStream.Write oXMLHTTP.responseBody
echo oStream.SaveToFile "%out%"
echo oStream.Close
echo End If
)>>"%temp%\download.vbs"
goto :eof
Last edited by abc0502 on 20 Sep 2012 18:18, edited 2 times in total.

kobryan
Posts: 6
Joined: 19 Sep 2012 20:18

Re: Batch file help

#3 Post by kobryan » 20 Sep 2012 07:15

thanks for the help! I will try them out and see if they work ok and let you know.

kobryan
Posts: 6
Joined: 19 Sep 2012 20:18

Re: Batch file help

#4 Post by kobryan » 20 Sep 2012 15:20

I tried to use the one with vbscript. the first thing I got was the error with the "goto eof" I had to remove this and after I did this it would flash up on the screen the cmd with the following message

Code: Select all

'"ziptempzipFile_name\"' is not recognized as an internal or external command, operable program or batch file.
.

The areas I changed with the file is:
delete the EOF part in the VBScript
Set the url variable
set the zipFile_name variable


Is there anyway to use the default extraction tool installed on the windows 7 computer, without having to install the 7zip? Some of the users that Im trying to make this file for will not have this installed.

Thanks again

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

Re: Batch file help

#5 Post by abc0502 » 20 Sep 2012 17:55

Sorry for the mistakes you had, i wrote that code directly without testing,

I Edited the 2nd Code up, and i tested it it's now working. :)

kobryan
Posts: 6
Joined: 19 Sep 2012 20:18

Re: Batch file help

#6 Post by kobryan » 20 Sep 2012 20:24

abc0502 wrote:Sorry for the mistakes you had, i wrote that code directly without testing,

I Edited the 2nd Code up, and i tested it it's now working. :)


Not a problem what so ever. Thanks a lot for the follow up it worked great! Plus this also lets me learn a lot more about the batch files so i can do more on my own next time.

THANKS AGAIN!

kobryan
Posts: 6
Joined: 19 Sep 2012 20:18

Re: Batch file help

#7 Post by kobryan » 26 Sep 2012 23:00

If i wanted to add another step to this would it be a large challenge to get it to see if 7zip is installed and if not download it then install it?

Something like this after downloading the main file.

Code: Select all

IF 7zip is installed to C:\Program Files\7-Zip\7z.exe
Continue with other code
ELSE
Download 7 zip 64 bit(http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920-x64.msi?r=http%3A%2F%2Fwww.7-zip.org%2F&ts=1348720932&use_mirror=iweb)
Install 7 zip to C:\Program Files\7-Zip\
Continue with other code


I have attempted to try some of it myself but im not sure how to even begin to check for a program or how to install it

Code: Select all

@Echo Off & Cls & Color 0E & Title kobryan & Mode 60,6

:: All settings is done here
:: File Extension is extracted from the URL, "last 3 letters" you can set it yourself or just use the code as it.
set "zip=C:\Program Files\7-Zip\7z.exe"
set "url=http://www.website.com/file.zip"
set "Destination=G:\"
set "FileExt=%url:~-3%"

:: 7 Zip settings
set "7zipurl=http://downloads.sourceforge.net/project/sevenzip/7-Zip/9.20/7z920-x64.msi?r=http%3A%2F%2Fwww.7-zip.org%2F&ts=1348720932&use_mirror=iweb"
set "7zipFileExt=%url:~-3%"

:: Main Code
:: Remove previous files
IF Exist "%temp%\DownloadFolder" RMDIR /S /Q "%temp%\DownloadFolder" >nul
IF Exist "%temp%\download.vbs" Del /F /Q "%temp%\download.vbs"

:: Create Temporary Folder to download into
If not exist "%temp%\DownloadFolder" MD "%temp%\DownloadFolder"

:: Download File
Echo.&Echo.&Echo       Downloading Zip File...
Call :Download "%url%" "%temp%\DownloadFolder\file.%FileExt%"
"%temp%\download.vbs"

:: Check for 7 zip
:: I'm not sure how to do this part


:: Download 7 zip
Echo.&Echo.&Echo       Downloading 7Zip...
Call :Download "%7zipurl%" "%temp%\DownloadFolder\file.%7zipFileExt%"
"%temp%\download.vbs"

:: Install 7 zip
:: I'm not sure how to do this part

:: Extract File into a USB
Echo.&Echo.&Echo       Extracting ...
"%zip%" x "%temp%\DownloadFolder\*.%FileExt%" -o"%Destination%" >nul

ClS & Echo.&Echo.&Echo       All Done.
Pause>nul
Exit /B


:: this is vbscript download script
:Download
set "in=%~1"
set "out=%~2"
(
echo Dim oXMLHTTP
echo Dim oStream
echo Set oXMLHTTP = CreateObject^("MSXML2.XMLHTTP.3.0"^)
echo oXMLHTTP.Open "GET", "%in%", False
echo oXMLHTTP.Send
echo If oXMLHTTP.Status = 200 Then
echo Set oStream = CreateObject^("ADODB.Stream"^)
echo oStream.Open
echo oStream.Type = 1
echo oStream.Write oXMLHTTP.responseBody
echo oStream.SaveToFile "%out%"
echo oStream.Close
echo End If
)>>"%temp%\download.vbs"
goto :eof


Thanks

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

Re: Batch file help

#8 Post by foxidrive » 26 Sep 2012 23:20

:: Check for 7 zip
if exist "%programfiles%\7-zip\" goto :SKIPINSTALL

:: Download 7 zip
Echo.&Echo.&Echo Downloading 7Zip...
Call :Download "%7zipurl%" "%temp%\DownloadFolder\file.%7zipFileExt%"
"%temp%\download.vbs"

:: Install 7 zip
:: this part requires the filename and path and you will need to see if it can do a silent install, without prompting
start "" /w "downloadfolder\7zip.msi"


:SKIPINSTALL

kobryan
Posts: 6
Joined: 19 Sep 2012 20:18

Re: Batch file help

#9 Post by kobryan » 28 Sep 2012 15:05

Awesome thanks for the help. I had a little trouble getting it to do a passive install but after playing with it for a while I was able to complete that.

Code: Select all

:: Install 7 zip
"%temp%\DownloadedFolder\file.%zipExt%" /passive

Post Reply