Filesize

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Filesize

#1 Post by doscode » 06 Aug 2013 04:19

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?

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

Re: Filesize

#2 Post by foxidrive » 06 Aug 2013 05:12

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Filesize

#3 Post by penpen » 06 Aug 2013 05:19

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

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

Re: Filesize

#4 Post by Squashman » 06 Aug 2013 06:14

Not sure why you are using Delayed Expansion for all those variables. No need to do that.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: Filesize

#5 Post by doscode » 06 Aug 2013 07:50

penpen:
Thanks for reply. Your solution works.


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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Filesize

#6 Post by penpen » 06 Aug 2013 08:52

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

Post Reply