<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
		<link>http://forum.script-coding.com/viewtopic.php?id=14923</link>
		<atom:link href="http://forum.script-coding.com/extern.php?action=feed&amp;tid=14923&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «CMD/BAT: Вывод размера файла с приставками и разделителями тысяч».]]></description>
		<lastBuildDate>Thu, 13 Feb 2025 00:54:10 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=161981#p161981</link>
			<description><![CDATA[<p><strong>Rumata</strong>, привет. Что за горы кода тут наделали, я даже не понял ничего <img src="//forum.script-coding.com/img/smilies/lol.png" width="15" height="15" /><br />Некромантирую немного, но мне понадобилось выводить на экран размер файла в МБ или ГБ, а файлы потенциально больше 4 ГБ.</p><div class="codebox"><pre><code>
set wallpaper_path=C:\Users\Admin\Pictures\Wallpapers\Sony\!Original
set wallpaper_file=Sony-xperia-z-experience-flow-black.jpg

for %%i in (&quot;%wallpaper_path%\%wallpaper_file%&quot;) do set size=%%~zi

rem Размер: Байты / КБайты / Мегабайты
set /a final_size=size / 1024 / 1024
echo %wallpaper_file%: %final_size% Байт
</code></pre></div><p>Может кто-нибудь решил уже на чистом бате проблему с размером файла больше ~2 ГБ?</p>]]></description>
			<author><![CDATA[null@example.com (biffick)]]></author>
			<pubDate>Thu, 13 Feb 2025 00:54:10 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=161981#p161981</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135906#p135906</link>
			<description><![CDATA[<p>А вот этот код</p><div class="codebox"><pre><code>
if                         1 gtr 9 echo:1e00 is greater
if                        10 gtr 9 echo:1e01 is greater
if                      1000 gtr 9 echo:1e03 is greater (K)
if                   1000000 gtr 9 echo:1e06 is greater (M)
if                1000000000 gtr 9 echo:1e09 is greater (G)
if             1000000000000 gtr 9 echo:1e12 is greater (T)
if          1000000000000000 gtr 9 echo:1e15 is greater (P)
if       1000000000000000000 gtr 9 echo:1e18 is greater (E)
if    1000000000000000000000 gtr 9 echo:1e21 is greater (Z)
if 1000000000000000000000000 gtr 9 echo:1e24 is greater (Y)
</code></pre></div><p>логично выводит</p><div class="codebox"><pre><code>
1e01 is greater
1e03 is greater (K)
1e06 is greater (M)
1e09 is greater (G)
1e12 is greater (T)
1e15 is greater (P)
1e18 is greater (E)
1e21 is greater (Z)
1e24 is greater (Y)
</code></pre></div><p>Потому что сказано:<br /></p><div class="quotebox"><cite>IF /? пишет:</cite><blockquote><p>&lt;...&gt;.&nbsp; These comparisons are generic, in that if both string1 and<br />string2 are both comprised of all numeric digits, then the strings are<br />converted to numbers and a numeric comparison is performed.</p></blockquote></div><p>Хотя два последних сравнения в контексте задачи лишние, так как ограничение на размер файла в NTFS - 16 эксбибайт - 1 кибибайт. </p><p>Используя эту возможность команды IF, можно обойти выше упомянутое ограничение.</p><p><strong>update</strong>:</p><p>Наверно можно. Но получается как-то ужасно.</p>]]></description>
			<author><![CDATA[null@example.com (Rumata)]]></author>
			<pubDate>Fri, 30 Aug 2019 20:06:18 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135906#p135906</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135867#p135867</link>
			<description><![CDATA[<p>Давайте я пока оставлю обновленную версию с улучшением от <strong>wisgest</strong>, пусть &quot;дозреет&quot;. А потом можно будет перенести в Коллекцию</p><div class="codebox"><pre><code>
@echo off

setlocal

set &quot;DIGIT_GROUPING=,&quot;

call :fsize &quot;%COMSPEC%&quot;
call :fsize &quot;%COMSPEC%&quot; KiB
call :fsize &quot;%COMSPEC%&quot; KB

endlocal
goto :EOF

:: SYNOPSIS
::
:: call :fsize [/v VARNAME] FILENAME [UNIT]
::
:: DESCRIPTION
::
:: Estimate the file size and scale it by UNIT.
:: If the option /v VARNAME is specified the result will be stored to
:: the variable VARNAME instead of printing.
::
:: UNIT is an optional unit prefix to represent big numbers in the
:: human-friendly format.
:: Units are of power 1000: KB, MB, GB
:: Units are of power 1024: KiB, MiB, GiB
::
:: ENVIRONMENT
::
:: DIGIT_GROUPING a character to be used as a delimiter of thousands.
:fsize
setlocal

set &quot;fs_var=&quot;

if /i &quot;%~1&quot; == &quot;/v&quot; (
	set &quot;fs_var=%~2&quot;
	shift /1
	shift /1
)

set /a &quot;fs_KiB=1024&quot;
set /a &quot;fs_MiB=fs_KiB * 1024&quot;
set /a &quot;fs_GiB=fs_MiB * 1024&quot;

set /a &quot;fs_KB=1000&quot;
set /a &quot;fs_MB=fs_KB * 1000&quot;
set /a &quot;fs_GB=fs_MB * 1000&quot;

if not &quot;%~2&quot; == &quot;&quot; (
	if not defined fs_%~2 (
		echo:Unknown unit: &quot;%~2&quot;&gt;&amp;2
		exit /b 1
	)
	rem The whitespace after &quot;=&quot; is mandatory
	set    &quot;fs_name= %~2&quot;
	set /a &quot;fs_unit=fs_%~2&quot;
)

set /a &quot;fs_size=%~z1&quot;

if defined fs_unit set /a &quot;fs_size /= fs_unit&quot;

if not defined DIGIT_GROUPING goto :fsize_2

set &quot;fs_result=&quot;

:fsize_1
set &quot;fs_result=%fs_size:~-3%%DIGIT_GROUPING:~0,1%%fs_result%&quot;
set &quot;fs_size=%fs_size:~0,-3%&quot;
if defined fs_size goto :fsize_1

set &quot;fs_size=%fs_result:~0,-1%&quot;

:fsize_2

if not defined fs_var (
	echo:%fs_size%%fs_name%
	goto :EOF
)

endlocal &amp;&amp; set &quot;%fs_var%=%fs_size%%fs_name%&quot;
goto :EOF
</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Rumata)]]></author>
			<pubDate>Sun, 25 Aug 2019 06:21:25 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135867#p135867</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135866#p135866</link>
			<description><![CDATA[<p>Выглядит хорошо. А как оно работает для групп чисел менее трех цифр? Например, 12, 1234, 12345.</p><p><strong>Update</strong></p><p>Сам спросил, сам ответил. Нормально работает. Сработал стереотип: если работаем с числовой информацией, то и применяем числовые операции. Спасибо, коллега, за подсказку.</p>]]></description>
			<author><![CDATA[null@example.com (Rumata)]]></author>
			<pubDate>Sun, 25 Aug 2019 02:39:35 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135866#p135866</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135865#p135865</link>
			<description><![CDATA[<div class="quotebox"><cite>Rumata пишет:</cite><blockquote><p>без арифметики совсем не обойтись</p></blockquote></div><p>Только в одном месте, где деление на fs_unit.<br /></p><div class="quotebox"><cite>Rumata пишет:</cite><blockquote><p>А если конкретно про разделение по тысячам... А будет ли от простого откусывания код проще?</p></blockquote></div><p>Будет немного проще, понятнее и быстрее.<br />Вот это:<br /></p><div class="codebox"><pre><code>if %fs_size% lss 1000 goto :fsize_2

set &quot;fs_result=&quot;
set /a &quot;fs_dividend=fs_size&quot;

:fsize_1
set /a &quot;fs_remainder=fs_dividend %% 1000&quot;
set /a &quot;fs_dividend=fs_dividend / 1000&quot;

set &quot;fs_remainder=000%fs_remainder%&quot;
set &quot;fs_result=%fs_remainder:~-3%%DIGIT_GROUPING:~0,1%%fs_result%&quot;

if %fs_dividend% gtr 1000 goto :fsize_1

if %fs_dividend% gtr 0 (
	set &quot;fs_result=%fs_dividend%%DIGIT_GROUPING:~0,1%%fs_result%&quot;
)
</code></pre></div><p>по-моему, можно заменить на:<br /></p><div class="codebox"><pre><code>set &quot;fs_result=&quot;

:fsize_1
	set &quot;fs_result=%fs_size:~-3%%DIGIT_GROUPING:~0,1%%fs_result%&quot;
	set &quot;fs_size=%fs_size:~0,-3%&quot;
if defined fs_size goto :fsize_1
</code></pre></div><p>Или я что-то не учёл?</p>]]></description>
			<author><![CDATA[null@example.com (wisgest)]]></author>
			<pubDate>Sat, 24 Aug 2019 23:31:02 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135865#p135865</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135863#p135863</link>
			<description><![CDATA[<p><strong>wisgest</strong>, без арифметики совсем не обойтись - надо переводить в кратные байты. А если конкретно про разделение по тысячам... А будет ли от простого откусывания код проще?</p>]]></description>
			<author><![CDATA[null@example.com (Rumata)]]></author>
			<pubDate>Sat, 24 Aug 2019 19:50:47 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135863#p135863</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135860#p135860</link>
			<description><![CDATA[<p>Для отделения трёх последних цифр находится остаток от деления на 1000, потом к нему слева приписываются нули (т.к. он может быть меньше 100) и затем от него откусывается справа три символа. Почему не откусывать по три символа справа от исходной строки без всякой арифметики?</p>]]></description>
			<author><![CDATA[null@example.com (wisgest)]]></author>
			<pubDate>Sat, 24 Aug 2019 15:58:10 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135860#p135860</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135845#p135845</link>
			<description><![CDATA[<p>Ну, и про это упомянуть тоже.</p>]]></description>
			<author><![CDATA[null@example.com (alexii)]]></author>
			<pubDate>Sat, 24 Aug 2019 10:25:35 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135845#p135845</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135840#p135840</link>
			<description><![CDATA[<p><strong>alexii</strong>, Вы так считаете? Но ведь есть существенное ограничение в 2**31-1, о котором я говорил в п.2.</p>]]></description>
			<author><![CDATA[null@example.com (Rumata)]]></author>
			<pubDate>Sat, 24 Aug 2019 05:55:19 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135840#p135840</guid>
		</item>
		<item>
			<title><![CDATA[Re: CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135797#p135797</link>
			<description><![CDATA[<p>В Коллекцию, полагаю.</p>]]></description>
			<author><![CDATA[null@example.com (alexii)]]></author>
			<pubDate>Wed, 21 Aug 2019 09:45:07 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135797#p135797</guid>
		</item>
		<item>
			<title><![CDATA[CMD/BAT: Вывод размера файла с приставками и разделителями тысяч]]></title>
			<link>http://forum.script-coding.com/viewtopic.php?pid=135617#p135617</link>
			<description><![CDATA[<p>В соседнем для меня форуме один товарищ <a href="https://www.dostips.com/forum/viewtopic.php?f=3&amp;t=9225">спросил о возможности</a> реализовать следующий функционал:</p><div class="quotebox"><blockquote><p>[1] Вывод размера в килобайтах<br />[2] Использовать разделитель тысяч</p><p>функционал требовалось реализовать исключительно на батниках.</p></blockquote></div><p>Я решил размяться и попробовать свои силы. Кое-что получилось, но решение не полноценное, так как решение:</p><p>1. несколько громоздко<br />2. ограничено размером файлов не более 2 гигабайта<br />3. попытки улучшить, чтобы вычислять размеры любых файлов, скорее всего приведет к существенному усложнению скрипта</p><p>Публикую здесь скорее как курьез.</p><p>Вот сам скрипт. Он вычисляет размер cmd.exe и выводит его в байтах, кибибайтах и килобайтах. </p><div class="codebox"><pre><code>
@echo off

setlocal

set &quot;DIGIT_GROUPING=,&quot;

call :fsize &quot;%COMSPEC%&quot;
call :fsize &quot;%COMSPEC%&quot; KiB
call :fsize &quot;%COMSPEC%&quot; KB

endlocal
goto :EOF

:: SYNOPSIS
::
:: call :fsize [/v VARNAME] FILENAME [UNIT]
::
:: DESCRIPTION
::
:: Estimate the file size and scale it by UNIT.
:: If the option /v VARNAME is specified the result will be stored to
:: the variable VARNAME instead of printing.
::
:: UNIT is an optional unit prefix to represent big numbers in the
:: human-friendly format.
:: Units are of power 1000: KB, MB, GB
:: Units are of power 1024: KiB, MiB, GiB
::
:: ENVIRONMENT
::
:: DIGIT_GROUPING a character to be used as a delimiter of thousands.
:fsize
setlocal

set &quot;fs_var=&quot;

if /i &quot;%~1&quot; == &quot;/v&quot; (
	set &quot;fs_var=%~2&quot;
	shift /1
	shift /1
)

set /a &quot;fs_KiB=1024&quot;
set /a &quot;fs_MiB=fs_KiB * 1024&quot;
set /a &quot;fs_GiB=fs_MiB * 1024&quot;

set /a &quot;fs_KB=1000&quot;
set /a &quot;fs_MB=fs_KB * 1000&quot;
set /a &quot;fs_GB=fs_MB * 1000&quot;

if not &quot;%~2&quot; == &quot;&quot; (
	if not defined fs_%~2 (
		echo:Unknown unit: &quot;%~2&quot;&gt;&amp;2
		exit /b 1
	)
	rem The whitespace after &quot;=&quot; is mandatory
	set    &quot;fs_name= %~2&quot;
	set /a &quot;fs_unit=fs_%~2&quot;
)

set /a &quot;fs_size=%~z1&quot;
if defined fs_unit set /a &quot;fs_size /= fs_unit&quot;

if not defined DIGIT_GROUPING goto :fsize_2
if %fs_size% lss 1000 goto :fsize_2

set &quot;fs_result=&quot;
set /a &quot;fs_dividend=fs_size&quot;

:fsize_1
set /a &quot;fs_remainder=fs_dividend %% 1000&quot;
set /a &quot;fs_dividend=fs_dividend / 1000&quot;

set &quot;fs_remainder=000%fs_remainder%&quot;
set &quot;fs_result=%fs_remainder:~-3%%DIGIT_GROUPING:~0,1%%fs_result%&quot;

if %fs_dividend% gtr 1000 goto :fsize_1

if %fs_dividend% gtr 0 (
	set &quot;fs_result=%fs_dividend%%DIGIT_GROUPING:~0,1%%fs_result%&quot;
)

set &quot;fs_size=%fs_result:~0,-1%&quot;

:fsize_2

if not defined fs_var (
	echo:%fs_size%%fs_name%
	goto :EOF
)

endlocal &amp;&amp; set &quot;%fs_var%=%fs_size%%fs_name%&quot;
goto :EOF
</code></pre></div><p>Ниже в сообщениие <a href="http://forum.script-coding.com/viewtopic.php?pid=135867#p135867">#9</a> приведен улучшенный вариант.</p>]]></description>
			<author><![CDATA[null@example.com (Rumata)]]></author>
			<pubDate>Wed, 14 Aug 2019 23:21:41 +0000</pubDate>
			<guid>http://forum.script-coding.com/viewtopic.php?pid=135617#p135617</guid>
		</item>
	</channel>
</rss>
