Page 1 of 1
Request: download a file if and only if its size changes
Posted: 09 Dec 2014 13:28
by transitionality
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.
Re: Request: download a file if and only if its size changes
Posted: 09 Dec 2014 19:57
by foxidrive
Your question could be improved with further details.
See here:
viewtopic.php?f=3&t=6108
Re: Request: download a file if and only if its size changes
Posted: 09 Dec 2014 22:13
by transitionality
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.
Re: Request: download a file if and only if its size changes
Posted: 10 Dec 2014 02:09
by foxidrive
Which part do you need help with.
At least you have revealed that it's HTTP and not FTP.
Re: Request: download a file if and only if its size changes
Posted: 10 Dec 2014 04:41
by ShadowThief
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.
Re: Request: download a file if and only if its size changes
Posted: 10 Dec 2014 12:26
by transitionality
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.
Re: Request: download a file if and only if its size changes
Posted: 10 Dec 2014 13:28
by Squashman
Code: Select all
set "url=http://www.example.com/target.txt"
Re: Request: download a file if and only if its size changes
Posted: 11 Dec 2014 01:17
by AiroNG
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
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
Re: Request: download a file if and only if its size changes
Posted: 11 Dec 2014 11:30
by transitionality
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.