The utility du estimates file space usage, which is a commonly-used command on Linux OS. Similar to touch, the following implements the basics of the du command by using Windows Batch Programming only. However, it only supports the basic switches -h (which prints the human friendly sizes) and -c (which prints the summary size). See the following sample usages (Assuming there is only a file – sample.txt in the current directory).
# du
1024 sample.txt
# du -h
1K sample.txt
# du -hc
1K sample.txt
1K total
To get the size of a file using batch only, you can echo the %~z1 variable. For instance,
@echo off
echo %~z1
If you know the file and want to retrieve the size inside the batch (without calling any other scripts or tools), you can use for
for %%i in (file.txt) do (echo %%~zi)
To sum up the sizes (-c) we can use
set /a total=!total!+%%~zi
Of course, you would need to use
setlocal enabledelayedexpansion
to delay the variable expansion.
A sub-routine H, is marked as a label, which is called when a human-friendly size is required.
:H
:: this subroutine prints the human-friendly size (-h)
:: %1 is file size in bytes; %2 is the file name or string
setlocal enabledelayedexpansion
set /a totalK=%1/1024
set /a totalM=%1/1024/1024
set /a totalG=%1/1024/1024/1024
if !totalG! gtr 0 (
echo !totalG!G %sep% %2
) else (
if !totalM! gtr 0 (
echo !totalM!M %sep% %2
) else (
if !totalK! gtr 0 (
echo !totalK!K %sep% %2
) else (
echo %1 %sep% %2
)
)
)
endlocal
The full source code of the implementation is uploaded to github and also given below:
@echo off
:: A simple du implementation using Windows Batch
:: supports -h -c switches only
:: https://helloacm.com
setlocal enabledelayedexpansion
set switch_c=False
set switch_h=False
set anyfiles=False
set /a total=0
set /a totalK=0
set /a totalG=0
set /a totalM=0
set sep=%TAB% %TAB% %TAB% %TAB% %TAB% %TAB%
:start
if %1.==. goto :end
if "%1"=="-c" (
set switch_c=True
shift
goto :start
)
if "%1"=="-h" (
set switch_h=True
shift
goto :start
)
if "%1"=="-ch" (
set switch_h=True
set switch_c=True
shift
goto :start
)
if "%1"=="-hc" (
set switch_h=True
set switch_c=True
shift
goto :start
)
set anyfiles=True
if not exist "%1" (
echo %0: cannot access ‘%1’: No such file or directory
shift
goto :start
)
:: check each given input files
for %%i in (%1) do (
set /a total=!total!+%%~zi
if %switch_h%==True (
call :H %%~zi %%i
) else (
echo %%~zi %sep% %%i
)
)
shift
goto :start
:end
:: if no filesnames are given, use all files in the current directory
if %anyfiles%==False (
for /f %%i in ('dir * /b') do (
set /a total=!total!+%%~zi
if %switch_h%==True (
call :H %%~zi %%i
) else (
echo %%~zi %sep% %%i
)
)
)
:: switch -c - total size (human friendly)
if %switch_c%%switch_h%==TrueTrue (
call :H !total! total
)
:: switch -c - total size
if %switch_c%%switch_h%==TrueFalse (
echo !total! %sep% total
)
goto :eof
:H
:: this subroutine prints the human-friendly size (-h)
setlocal enabledelayedexpansion
set /a totalK=%1/1024
set /a totalM=%1/1024/1024
set /a totalG=%1/1024/1024/1024
if !totalG! gtr 0 (
echo !totalG!G %sep% %2
) else (
if !totalM! gtr 0 (
echo !totalM!M %sep% %2
) else (
if !totalK! gtr 0 (
echo !totalK!K %sep% %2
) else (
echo %1 %sep% %2
)
)
)
endlocal
goto :eof
endlocal
–eof–
651 wordsLast Post: JQuery - How to Animate Scrolling to the Top and Scrolling to a Div ?
Next Post: Adsense Earning Statistics (2015)
