Page 1 of 1

Filesize

Posted: 06 Aug 2013 04:19
by doscode
Hello,
I am back after few years trying to familiarize with some codes we made here in past. I am running one script which should detect file size at its start. It looks like this:

Code: Select all

@echo off 
REM         REQUIREMENTS: CURL, GREP
Setlocal EnableDelayedExpansion
SET proxy_1=proxy_samai_ru_1.htm
SET source_1=http://www.samair.ru/proxy/

CHCP 1250 > NUL

for /f "delims=" %%x in (delimiter.ini) do set TAB=%%x

if not exist !proxy_1! (
  echo File not found. Dounloading from !source_1!
  curl -o !proxy_1! !source_1! -H user_agent="User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8"
  pause
  exit
)
echo off
for %%I in (!proxy_1!) do SET filesize=%%~zI
echo Filesize !filesize! B
if !filesize! LSS 2000 (
 echo File is too small, suspected server denied to send data. Download manually from !source_1!
 pause
 )

echo reading !proxy_1!


What it prints to me is:

Filesize 11208 B
File is too small, suspected server denied to send data. Download manually from
http://www.samair.ru/proxy/
press any key...


Which is not correct becaue filesize 11208 should be greater then 2000, so the error should not happen. What's wrong?

Re: Filesize

Posted: 06 Aug 2013 05:12
by foxidrive
Try it yourself. It works here (I only changed the three lines at the top) so there is another reason for your issue.

Code: Select all

@echo off
setlocal enabledelayedexpansion

SET filesize=11208
echo Filesize !filesize! B
if !filesize! LSS 2000 (
 echo File is too small, suspected server denied to send data. Download manually from !source_1!
 pause
 )
pause

Re: Filesize

Posted: 06 Aug 2013 05:19
by penpen
This code line ends on a space char:

Code: Select all

for %%I in (!proxy_1!) do SET filesize=%%~zI 
The token !filesize! (%filesize% would be the same) then has a space at the end.
The command line interpreter then compares "11208 " and "2000" as strings (without the doublequotes) and not as int32 numbers.

Delete the space above and it should work.
Recommended in most cases is the use of set with double quotes, so in this example it looks like this:

Code: Select all

for %%I in (!proxy_1!) do SET "filesize=%%~zI" 
Then spaces after the last doublequote are ignored.

Btw, your algorithm prints:
Filesize 11208<SPACE><SPACE>B
that cannot be seen as multiple spaces are merged to one in html.

penpen

Re: Filesize

Posted: 06 Aug 2013 06:14
by Squashman
Not sure why you are using Delayed Expansion for all those variables. No need to do that.

Re: Filesize

Posted: 06 Aug 2013 07:50
by doscode
penpen:
Thanks for reply. Your solution works.


How can I find topics created by me in this forum?

Re: Filesize

Posted: 06 Aug 2013 08:52
by penpen
Click Search at the top menubar just 2 items left of the login/logout.
Then use "doscode" with search for "author" text input, and check "First post of topics only" at "Search within: " and the search.

penpen