Request: download a file if and only if its size changes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Request: download a file if and only if its size changes

#1 Post by transitionality » 09 Dec 2014 13:28

I'd like a batch file to do the following:

* Read the expected file size of the target file (in bytes) from a save file.
* Connect to the server hosting the target file and query its file size (in bytes).
* If the two values match, quit. If they don't, download the target file to a specific local path (overwriting the current instance) and update the file size value in the save file.

I use curl for command-line downloading, but I can probably adapt solutions using other tools.

Thanks.

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

Re: Request: download a file if and only if its size changes

#2 Post by foxidrive » 09 Dec 2014 19:57

Your question could be improved with further details.

See here: viewtopic.php?f=3&t=6108

transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Re: Request: download a file if and only if its size changes

#3 Post by transitionality » 09 Dec 2014 22:13

No Non-Latin characters are involved.

For the purposes of this exercise, assume that the save file is located at C:\save.txt, the target file at C:\target.txt, and on the internet at http://www.example.com/target.txt.

Assume curl.exe is included in the PATH, so calling curl suffices.

Thank you.

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

Re: Request: download a file if and only if its size changes

#4 Post by foxidrive » 10 Dec 2014 02:09

Which part do you need help with.

At least you have revealed that it's HTTP and not FTP.

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

Re: Request: download a file if and only if its size changes

#5 Post by ShadowThief » 10 Dec 2014 04:41

The fact that you're using cURL makes this much easier. I recommend looking at the official documentation. It sounds like the -I flag is exactly what you need to make this work.

transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Re: Request: download a file if and only if its size changes

#6 Post by transitionality » 10 Dec 2014 12:26

I'm beginner-intermediate, so I can probably script each individual task, but the branching structure puzzles me. I'd also like to be able to set the file paths and the URL to variables (so I can quickly change them at the top), and I'm not sure of the exact syntax for that. Thanks.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Request: download a file if and only if its size changes

#7 Post by Squashman » 10 Dec 2014 13:28

Code: Select all

set "url=http://www.example.com/target.txt"

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: Request: download a file if and only if its size changes

#8 Post by AiroNG » 11 Dec 2014 01:17

Alternatively you could try this: (wget is required though):

Code: Select all

:update
SET new=%tmp%\%name%\update.bat
SET old=update.bat

rem get file
echo:
echo:  connecting to server and aquiring file...
 wget.exe -q -O %tmp%\%name%%ext% %url%%name%%ext% 2>nul

rem looks for filesize and puts them into variables
 FOR %%A IN (%old%) DO ( SET /a "oldsize"="%%~zA" )
 FOR %%B IN (%new%) DO ( SET /a "newsize"="%%~zB" )

rem checking filesize
 IF "%newsize%" gtr "%oldsize%" GOTO update_yes
GOTO :eof

:update_yes
CLS
echo:
echo: an update is aviable!
echo:
echo: current filesize: %oldsize% bytes
echo:
echo: filesize on the server: %newsize% bytes
echo:
pause


This only checks for updates. It doesn't install/apply them.



The code below is my current version of an update script. It
needs wget.exe and 7z.exe to work and will install the update
if necessary or wanted (forced update). I'm too lazy right now
to translate it into english so you've got to deal with it's german comments :mrgreen:

Code: Select all

@echo off

set url=http://wherever.com/whatever/
set name=update
set ext=.rar
set olddir=%cd%
set versionupdate=
set versionprog=

if "%1"=="-f" goto forceupdate
if "%1"=="/f" goto forceupdate
if "%1"=="-n" goto normal
if "%1"=="/n" goto normal
if "%1"=="" goto info


:info
echo:
echo:     Updatesystem fuer das USB-Stick-Tool
echo:
echo: Benutzung: update [-option] oder [/option]
echo:
echo: -f und /f    Erzwingt Update ohne Versionskontrolle
echo: -n und /n    Startet ein regulaeres Update
echo:
goto eof

:normal
:: startet das Updatesystem
cls

:: Überprüfung ob Update-Ordner bereits existiert und ggf löschung
 if exist %tmp%\%name%\ (del /Q %tmp%\%name%\)

:: Paket holen
 wget.exe -q -O %tmp%\%name%%ext% %url%%name%%ext% 2>nul

:: Entpacken des Update-Paketes
 7z.exe x -y %tmp%\%name%.rar -o%tmp%\%name% 2>nul
 
:: setzt Variabel für Updateversion
find "rem" %name%%ext%>%tmp%\tmp1.txt
for /f "tokens=1-3 delims= " %%a in (%tmp%\tmp1.txt) do (
   if /i "%%a"=="rem" set versionprog=%%c
        )
find "rem" %tmp%\%name%\%name%%ext%>%tmp%\tmp2.txt
for /f "tokens=1-3 delims= " %%a in (%tmp%\tmp2.txt) do (
   if /i "%%a"=="rem" set versionupdate=%%c
        )
 
:: Versions-Check
 if "%versionupdate%" gtr "%versionprog%" goto jop
goto nope

:jop
:: "installiert" neue Dateien und löscht Updateordner und Updatepacket
 move /y %tmp%\%name%\*.* %olddir%\
 rmdir /s /q %tmp%\%name%\
 del /q %tmp%\%name%%ext%
 echo:
 echo:         Update erfolgreich durchgefuehrt...
 echo:
  if exist %tmp%\tmp1.txt (del /q %tmp%\tmp1.txt)
  if exist %tmp%\tmp2.txt (del /q %tmp%\tmp2.txt)
  pause
goto ende

:nope
:: Update nicht nötig
 rmdir /s /q %tmp%\%name%\
 del /q %tmp%\%name%%ext%
 echo:
 echo:         Update nicht noetig, keine neuere Version vorhanden...
 echo:
  if exist %tmp%\tmp1.txt (del /q %tmp%\tmp1.txt)
  if exist %tmp%\tmp2.txt (del /q %tmp%\tmp2.txt)
  pause
goto ende

:forceupdate
:: Erzwungenes Update, falls Dateien fehlen oder man die zieldatei geDAUt hat...
 wget.exe -q -O %tmp%\%name%%ext% %url%%name%%ext% 2>nul
 7z.exe x -y %tmp%\%name%.rar -o%tmp%\%name% 2>nul
 move /y %tmp%\%name%\*.* %olddir%\
 rmdir /s /q %tmp%\%name%\
 del /q %tmp%\%name%%ext%
 echo:
 echo:         Erzwungenes Update durchgefuehrt...
 echo:
  if exist %tmp%\tmp1.txt (del /q %tmp%\tmp1.txt)
  if exist %tmp%\tmp2.txt (del /q %tmp%\tmp2.txt)
 pause
goto ende


:ENDE


edit: corrected little mistake

transitionality
Posts: 10
Joined: 09 Jul 2014 03:53

Re: Request: download a file if and only if its size changes

#9 Post by transitionality » 11 Dec 2014 11:30

No installation is required, the file in question is a text file, and simply needs to be downloaded.

I should be able to adapt the code you provided.

Thank you.

Post Reply