How to extraxt files list with the File Size

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

How to extraxt files list with the File Size

#1 Post by Asley Arvind » 30 Jul 2013 00:27

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extraxt files list with the File Size

#2 Post by foxidrive » 30 Jul 2013 01:07

@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

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: How to extraxt files list with the File Size

#3 Post by Asley Arvind » 30 Jul 2013 01:54

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to extraxt files list with the File Size

#4 Post by foxidrive » 30 Jul 2013 02:13

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 >

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: How to extraxt files list with the File Size

#5 Post by Asley Arvind » 30 Jul 2013 04:51

Thanks a lot ... It worked.. Cheerssss!!!!!!!!!!!!!!!!!!

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: How to extraxt files list with the File Size

#6 Post by Asley Arvind » 30 Jul 2013 09:37

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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to extraxt files list with the File Size

#7 Post by Squashman » 30 Jul 2013 10:52

I do not see a single SET /A command in your code.
That is the only way to do math in batch.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to extraxt files list with the File Size

#8 Post by Dragokas » 30 Jul 2013 17:17

Hello, Asley Arvind.

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.

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: How to extraxt files list with the File Size

#9 Post by Asley Arvind » 30 Jul 2013 22:56

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

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: How to extraxt files list with the File Size

#10 Post by Asley Arvind » 31 Jul 2013 01:46

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

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to extraxt files list with the File Size

#11 Post by Dragokas » 31 Jul 2013 02:05

Asley Arvind,
to divide the numbers with big dividend you can use my function :DivLim. 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.

Dragokas
Posts: 43
Joined: 30 Jul 2013 09:42
Location: Ukraine, USSR
Contact:

Re: How to extraxt files list with the File Size

#12 Post by Dragokas » 31 Jul 2013 02:38

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.

Asley Arvind
Posts: 11
Joined: 29 Apr 2013 22:54

Re: How to extraxt files list with the File Size

#13 Post by Asley Arvind » 01 Aug 2013 04:48

Hello All,

Thanks for sharing your ideas.. but i dont know how i will use it in my code :cry: 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

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to extraxt files list with the File Size

#14 Post by Squashman » 01 Aug 2013 07:24

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.

Post Reply