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).
1 2 3 4 5 6 7 | # du 1024 sample.txt # du -h 1K sample.txt # du -hc 1K sample.txt 1K total |
# 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,
1 2 | @echo off echo %~z1 |
@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
1 | for %%i in (file.txt) do (echo %%~zi) |
for %%i in (file.txt) do (echo %%~zi)
To sum up the sizes (-c) we can use
1 | set /a total=!total!+%%~zi |
set /a total=!total!+%%~zi
Of course, you would need to use
1 | setlocal enabledelayedexpansion |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | :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 |
: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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | @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 |
@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–
loading...
Last Post: JQuery - How to Animate Scrolling to the Top and Scrolling to a Div ?
Next Post: 2015 - Adsense Earning Statistics