Page 1 of 1

get File Size batch script.

Posted: 19 Sep 2019 04:26
by Rajnishjc_27
HI

Which command to get or read size of the file from particular folder.
like its kb or mb.

Re: get File Size batch script.

Posted: 21 Sep 2019 01:29
by Hackoo
Hi :wink:

Code: Select all

@echo off
Title Get size file from a batch file
SETLOCAL enabledelayedexpansion
Set "MyBatchFile=%0"
Set "MyFile=C:\temp\test.txt"
echo(
echo This batch file !MyBatchFile! has a size of %~z0 bytes
echo(
for %%A in ("%MyFile%") do (
	set "Size=%%~zA"
	echo The file "!MyFile!" has a size of !size! bytes
)
pause

Re: get File Size batch script.

Posted: 22 Sep 2019 07:42
by Hackoo
Hi :wink:
Here is another code using a small vbscript to convert size to KB,MB and GB

Code: Select all

@echo off
Title Get size of file or folder from a batch file by Hackoo 2019
SetLocal enabledelayedexpansion
Set "MyBatchFile=%0"
Set "MyFolder=%Temp%"
Set "LogFile=%~dp0SizeLog.txt"
If Exist "%LogFile%" Del "%LogFile%"
echo(
echo This batch file !MyBatchFile! has a size of %~z0 bytes
Call :GetSize %~z0
(
	echo This batch file !MyBatchFile! has a size of %~z0 bytes
	Call :GetSize %~z0
)>"!LogFile!"

for /f "delims=" %%A in ('Dir /s /b "%MyFolder%"') do (
	set "Size=%%~zA"
	set "PathName=%%~dpnxA"
	Call :FolderOrFile "!PathName!"
	echo has a size of !size! bytes
	Call :GetSize !size!
	(
		Call :FolderOrFile "!PathName!"
		echo has a size of !size! bytes
		Call :GetSize !size!
	)>>"!LogFile!"
)
If Exist "!LogFile!" Start "" "!LogFile!" & Exit
Rem ----------------------------------------------------------------------------
:GetSize <object>
(
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024, 2^)^& " KB"
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024/1024, 5^)^& " MB"
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024/1024/1024, 6^)^& " GB"
	echo wsh.echo String("100","-"^)
)>"%temp%\tmp.vbs"
cscript //nologo "%temp%\tmp.vbs"
del "%temp%\tmp.vbs"
Exit /b 
Rem ----------------------------------------------------------------------------
:FolderOrFile <Object>
REM How to test if a path is a file or directory in Windows batch file?
REM https://stackoverflow.com/questions/8666225/how-to-test-if-a-path-is-a-file-or-directory-in-windows-batch-file?answertab=active#tab-top
for /f "tokens=1,2 delims=d" %%A in ("-%~a1") do if "%%B" neq "" (
  echo The folder %1 
) else if "%%A" neq "-" (
  echo The file %1
) else (
  echo %1 does not exist
)
Exit /b 
Rem ----------------------------------------------------------------------------

Re: get File Size batch script.

Posted: 09 Feb 2023 00:39
by xiro
Hackoo wrote:
22 Sep 2019 07:42
Hi :wink:
Here is another code using a small vbscript to convert size to KB,MB and GB

Code: Select all

@echo off
Title Get size of file or folder from a batch file by Hackoo 2019
SetLocal enabledelayedexpansion
Set "MyBatchFile=%0"
Set "MyFolder=%Temp%"
Set "LogFile=%~dp0SizeLog.txt"
If Exist "%LogFile%" Del "%LogFile%"
echo(
echo This batch file !MyBatchFile! has a size of %~z0 bytes
Call :GetSize %~z0
(
	echo This batch file !MyBatchFile! has a size of %~z0 bytes
	Call :GetSize %~z0
)>"!LogFile!"

for /f "delims=" %%A in ('Dir /s /b "%MyFolder%"') do (
	set "Size=%%~zA"
	set "PathName=%%~dpnxA"
	Call :FolderOrFile "!PathName!"
	echo has a size of !size! bytes
	Call :GetSize !size!
	(
		Call :FolderOrFile "!PathName!"
		echo has a size of !size! bytes
		Call :GetSize !size!
	)>>"!LogFile!"
)
If Exist "!LogFile!" Start "" "!LogFile!" & Exit
Rem ----------------------------------------------------------------------------
:GetSize <object>
(
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024, 2^)^& " KB"
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024/1024, 5^)^& " MB"
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024/1024/1024, 6^)^& " GB"
	echo wsh.echo String("100","-"^)
)>"%temp%\tmp.vbs"
cscript //nologo "%temp%\tmp.vbs"
del "%temp%\tmp.vbs"
Exit /b 
Rem ----------------------------------------------------------------------------
:FolderOrFile <Object>
REM How to test if a path is a file or directory in Windows batch file?
REM https://stackoverflow.com/questions/8666225/how-to-test-if-a-path-is-a-file-or-directory-in-windows-batch-file?answertab=active#tab-top
for /f "tokens=1,2 delims=d" %%A in ("-%~a1") do if "%%B" neq "" (
  echo The folder %1 
) else if "%%A" neq "-" (
  echo The file %1
) else (
  echo %1 does not exist
)
Exit /b 
Rem ----------------------------------------------------------------------------
How to make this run and check only two files which I specify where their specific location is

Re: get File Size batch script.

Posted: 09 Feb 2023 10:34
by Hackoo
@xiro
Give a try for this modification and tell me the results : You should replace the path of File1 and File2 in your case :

Code: Select all

@echo off
Title Get size of file or folder from a batch file

Set "LogFile=%~dp0SizeLog.txt"
If Exist "%LogFile%" Del "%LogFile%"

Set "FileExplorer=%Windir%\Explorer.exe"
Set "File1=C:\full\path\to\file1.ext"
Set "File2=C:\full\path\to\file2.ext"
SetLocal enabledelayedexpansion

::---------------------------------------------------------
Rem Here you can retrieve the size of the file Explorer.exe as an example
Call :Process "%FileExplorer%"
::---------------------------------------------------------
@FOR %%A IN ("%File1%" "%File2%") DO (
	Call :Process "%%~A"
)
pause
If Exist "!LogFile!" Start "" "!LogFile!" & Exit
::---------------------------------------------------------

:Process <File>
@FOR /F "tokens=*" %%A IN ("%~1") DO (
	set "Size=%%~zA"
	set "PathName=%%~dpnxA"
	Call :FolderOrFile "!PathName!"
	Call :GetSize !Size!
	(
		Call :FolderOrFile "!PathName!"
		Call :GetSize !Size!
	) >> "!LogFile!"
)
Exit /b
Rem --------------------------------------------------------
:GetSize <object>
(
	echo On Error Resume Next
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024, 2^)^& " KB"
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024/1024, 5^)^& " MB"
	echo wsh.echo FormatNumber^(cdbl^("%~1"^)/1024/1024/1024, 6^)^& " GB"
	echo wsh.echo String("100","-"^)
)>"%temp%\tmp.vbs"
cscript //nologo "%temp%\tmp.vbs"
del "%temp%\tmp.vbs"
Exit /b 
Rem ----------------------------------------------------------------------------

:FolderOrFile <Object>
REM How to test if a path is a file or directory in Windows batch file?
REM https://stackoverflow.com/questions/8666225/how-to-test-if-a-path-is-a-file-or-directory-in-windows-batch-file?answertab=active#tab-top
for /f "tokens=1,2 delims=d" %%A in ("-%~a1") do if "%%B" neq "" (
	echo The folder %1
) else if "%%A" neq "-" (
	echo The file %1
) else (
	echo %1 does not exist
)
Exit /b
Rem ----------------------------------------------------------------------------