Page 1 of 1
How to extraxt files list with the File Size
Posted: 30 Jul 2013 00:27
by Asley Arvind
HiTeam,
Good Morning.
I am already using the below code for monitoring the shared folder. This code will return me files list and also empty folder list inside the specified folder. Now i need to know the file size of the each file inside the given folder. Please let us know how to get the files size.
@echo off & setlocal
set "startfolder=C:\Hosting"
set "file=CleanUpList.csv"
pushd "%startfolder%"
(for /r %%i in (*) do echo(%%~fi
for /r /d %%i in (*) do (
set "empty="
for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
if not defined empty echo(%%~fi\
))>"%file%"
popd
Thanks and Regards,
Arvind
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 01:07
by foxidrive
@echo off & setlocal
set "startfolder=C:\Hosting"
set "file=CleanUpList.csv"
pushd "%startfolder%"
(for /r %%i in (*) do echo(%%~fi %%~zi
for /r /d %%i in (*) do (
set "empty="
for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
if not defined empty echo(%%~fi\
))>"%file%"
popd
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 01:54
by Asley Arvind
Hi,
Thanks for the quick response. It works fine.
Could you please tell me how to set header for these ouput as Filename , Filesize.
@echo off & setlocal
set "startfolder=C:\IMSHosting"
set "file=sizeofcleanup3.csv"
pushd "%startfolder%"
(for /r %%i in (*) do echo(%%~fi , %%~zi
for /r /d %%i in (*) do (
set "empty="
for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
if not defined empty echo(%%~fi\
))>"%file%"
popd
Thanks and Regards,
Arvind
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 02:13
by foxidrive
Asley Arvind wrote:Could you please tell me how to set header for these ouput as Filename , Filesize.
You want a CSV header?
You'll need to echo them into the output file after the Pushd and before the loop, and then append the rest of it to the file using >> instead of >
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 04:51
by Asley Arvind
Thanks a lot ... It worked.. Cheerssss!!!!!!!!!!!!!!!!!!
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 09:37
by Asley Arvind
Hi ,
Again I need your help, Is there a way to get the file size in MB. I have tried (%%~zi)/(1024*1024) which results in error.
Could you please help me on this
@echo off & setlocal
set "startfolder=C:\Hosting"
set "file=sizeofcleanup.csv"
pushd "%startfolder%"
(echo FileName, FileSize, DateModified
(for /r %%i in (*) do echo(%%~fi , %%~zi, %%~ti
for /r /d %%i in (*) do (
set "empty="
for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
if not defined empty echo(%%~fi\
)))>>"%file%"
popd
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 10:52
by Squashman
I do not see a single SET /A command in your code.
That is the only way to do math in batch.
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 17:17
by Dragokas
Hello, Asley Arvind.
Like Squashman said, you can use:
to calculate MB. But be carefull: if the size of file will bigger than 2147483647 bytes, it will cause an error (Max value for the number type of CMD arithmetic).
Please, ask if you need a solution to get around this limitation.
Best regards, Alex.
Re: How to extraxt files list with the File Size
Posted: 30 Jul 2013 22:56
by Asley Arvind
Hi Dragokas,
Thanks for your information. Sure the file size in my shared drive will be having file with more than 2gb and in few cases i have seen 100gb file also.. so I have to so work around for this. Please do favour on this.
Thanks and Regards,
Arvind
Re: How to extraxt files list with the File Size
Posted: 31 Jul 2013 01:46
by Asley Arvind
Hi ,
Thanks for the quick solution. Could you please help me where i need to do change my code to get file size in MB or GB accordingly.
As I am a newbie dos commands, Please highlight the changes in below code so that i can learn it.
@echo off & setlocal
set "startfolder=C:\Hosting"
set "file=sizeofcleanup.csv"
pushd "%startfolder%"
(echo FileName, FileSize, DateModified
(for /r %%i in (*) do echo(%%~fi , %%~zi, %%~ti
for /r /d %%i in (*) do (
set "empty="
for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
if not defined empty echo(%%~fi\
)))>>"%file%"
popd
Thanks,
Arvind
Re: How to extraxt files list with the File Size
Posted: 31 Jul 2013 02:05
by Dragokas
Asley Arvind,
to divide the numbers with big dividend you can use my function

ivLim. Here is an example.
It also shows a numbers after 0.
The code is not optimized and writed in bad "goto"-style, just no time for a new revision.
But it must works good.
Code: Select all
@echo off
SetLocal EnableDelayedExpansion
:: Example of using
call :DivLim 16261316608 1024 2 ret
echo %ret%
call :DivLim 16261316608 1025 2 ret
echo %ret%
pause
goto :eof
:DivLim %1-dividend %2-divisor %3-Accuracy.after.zero %4-variable.where.save.a.result
:: This function divides dividend number without restrictions of CMD arithmetics
set Num1=%~1
set Num2=%~2
set MaxZ=%~3
set /a Num1_pos=-1
:AddNum1
set /a Num1_pos+=1
::если строка закончилась
if "!Num1:~%Num1_pos%,1!"=="" Goto AfterZiro
Set Num1_part=%Num1_part%!Num1:~%Num1_pos%,1!
if %Num1_part%==00 Set Num1_part=0
Call :TryDiv
Goto AddNum1
:AfterZiro
if %Num1_part%==0 goto Div_End
if %MaxZ% neq 0 Set ResultAll=%ResultAll%.
Set Zero_Marker=true
:AddZiro
Set /A MaxZ-=1
if %MaxZ% LSS 0 Goto Div_End
if %Num1_part%==0 goto Div_End
Set Num1_part=%Num1_part%0
Call :TryDiv
Goto AddZiro
:Div_End
set "%~4=%ResultAll%"
Exit /B
:TryDiv
Set /a Result=Num1_part / Num2
rem echo %Num1_part%-%result%
if %Result% neq 0 (
Set ResultAll=%ResultAll%%Result%
Set /A Num1_part=Num1_part - Result * Num2
) else (if "%ResultAll%" neq "" if not Defined Zero_Marker Set ResultAll=%ResultAll%0)
Exit /B
Best regards, Alex.
Re: How to extraxt files list with the File Size
Posted: 31 Jul 2013 02:38
by Dragokas
But if you are ready to sacrifice accuracy of ~ 10 Mb for 10 Gb file, you can use much easy function - it just trims decades before devision the size of files bigger than 999.999.999 bytes:
Code: Select all
@echo off
SetLocal EnableDelayedExpansion EnableExtensions
:: Example of using
call :B_to_MB 16261316608 ret
echo %ret%
pause
goto :eof
:B_to_MB
:: %1 - Size in Bytes
:: %2 - Variable to save number in MBytes
:: remark: default 32-bit Lim for CMD up to 2147483647 (9 digits + 1)
set dividend=%~1
set digit=0
:Trim_10
if "%dividend:~9,1%" neq "" (set dividend=%dividend:~0,-1%& set /A digit+=1& goto Trim_10)
set /A %~2=dividend / 1048576 &:: 1024*1024 = 1048576
for /L %%C in (1,1,%digit%) do set %~2=!%~2!0
Exit /B
Please, do not forget to write "SetLocal EnableDelayedExpansion" directive when you will use this functions in own code.
Re: How to extraxt files list with the File Size
Posted: 01 Aug 2013 04:48
by Asley Arvind
Hello All,
Thanks for sharing your ideas.. but i dont know how i will use it in my code

Please help me how to get it done in my below code.
My requirement is I have to monitor a shared drive where i will receive 1000's of file and folders in it. So i have to extract file name, file size, date modified and if posssible author of the file.
Thanks in advance for the help.
Code: Select all
@echo off & setlocal
set "startfolder=\\PLYSSQL357p.customer.rxcorp.com\IMSHosting"
set "file=sizeofcleanup.csv"
pushd "%startfolder%"
(echo FileName, FileSize in Bytes, DateModified
(for /r %%i in (*) do echo(%%~fi , %%~zi, %%~ti
for /r /d %%i in (*) do (
set "empty="
for /f %%a in ('dir /b /a "%%~i"') do set "empty=%%~a"
if not defined empty echo(%%~fi\
)))>>"%file%"
popd
Re: How to extraxt files list with the File Size
Posted: 01 Aug 2013 07:24
by Squashman
In order to get the Owner of the file you will need to use the DIR command without the /B switch and use the /Q switch.