How to extraxt files list with the File Size
Moderator: DosItHelp
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
How to extraxt files list with the File Size
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
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
@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
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
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: How to extraxt files list with the File Size
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
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
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 >
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: How to extraxt files list with the File Size
Thanks a lot ... It worked.. Cheerssss!!!!!!!!!!!!!!!!!!
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: How to extraxt files list with the File Size
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
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
I do not see a single SET /A command in your code.
That is the only way to do math in batch.
That is the only way to do math in batch.
Re: How to extraxt files list with the File Size
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.
Like Squashman said, you can use:
Code: Select all
set /A size=%%~zi/^(1024*1024^)
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.
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: How to extraxt files list with the File Size
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
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
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: How to extraxt files list with the File Size
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
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
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.
Best regards, Alex.
to divide the numbers with big dividend you can use my function

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
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:
Please, do not forget to write "SetLocal EnableDelayedExpansion" directive when you will use this functions in own code.
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.
-
- Posts: 11
- Joined: 29 Apr 2013 22:54
Re: How to extraxt files list with the File Size
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.
Thanks for sharing your ideas.. but i dont know how i will use it in my 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
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.