-1

I want to get the same result on cmd prompt as I right-click to a folder and click on properties.here an example. When i try with dir command i'll get more directories than i expect.

  • Open a [command prompt](https://www.howtogeek.com/235101/), run `dir C:\CST2107Lab4 /A` and look on the last two lines which contain the information of the number of files in the directory and their total size in bytes as well as the number of subdirectories. The three data can be assigned to environment variables on using a `for /F` loop to process the output of `dir`. Example for English Windows: `for /F "skip=7 tokens=1-3" %%I in ('dir "C:\CST2107Lab4" /A 2^>nul') do if "%%J" == "File(s)" (set "NumberFiles=%%I" & set "TotalFileSize=%%K") else if "%%J" == "Dir(s)" set /A NumberFolders=%%I-2` – Mofi Mar 31 '21 at 07:05

1 Answers1

0

A little searching goes a long ways.

Run this from cmd.exe by putting the script below into a file such as Get-DirResults.ps1 and use the following command.

powershell -NoLogo -NoProfile -File DirResults.ps1

=== Get-DirResults.ps1

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [ValidateScript({
        if( -Not ($_ | Test-Path -PathType Container) ){
            throw "File or folder does not exist"
        }
        return $true
    })]
    [System.Io.FileInfo]$Path
)

# Format-ByteSize from https://stackoverflow.com/a/57535324/447901
$Shlwapi = Add-Type -MemberDefinition '
    [DllImport("Shlwapi.dll", CharSet=CharSet.Auto)]public static extern int StrFormatByteSize(long fileSize, System.Text.StringBuilder pwszBuff, int cchBuff);
' -Name "ShlwapiFunctions" -namespace ShlwapiFunctions -PassThru

Function Format-ByteSize([Long]$Size) {
    $Bytes = New-Object Text.StringBuilder 20
    $Return = $Shlwapi::StrFormatByteSize($Size, $Bytes, $Bytes.Capacity)
    If ($Return) {$Bytes.ToString()}
}

$DirInfo = Get-ChildItem -Recurse -Directory -Path $Path | Measure-Object
$FileInfo = Get-ChildItem -Recurse -File -Path $Path | Measure-Object -Sum -Property Length

"Directory: {0}" -f @(Resolve-Path -Path $Path)
"Size:      {0} ({1:N0} bytes)" -f @((Format-ByteSize $FileInfo.Sum), $FileInfo.Sum)
"Contains:  {0:N0} Files, {1:N0} Folders" -f @($FileInfo.Count, $DirInfo.Count)
lit
  • 14,456
  • 10
  • 65
  • 119
  • Thank you for your answer is there any way to use cmd prompt instead of power shell – ali kemal Aydin Mar 31 '21 at 18:13
  • Yes, that is what I wrote. In cmd.exe, use the command `powershell -NoLogo -NoProfile -File Get-DirResults.ps1`. Doing this without any executable other than cmd.exe would be difficult and likely unmaintainable. Does it produce the results you wanted? – lit Mar 31 '21 at 18:42