I am trying to use the powershell command from here in a batch file. I want it to loop through a folder, printing each folder's name and size. What am I missing in my code from getting it to work?
I have tried using quotes all over the place. I have also tried using escape characters. Perhaps it's because I am new to writing batch files, but I can't figure it out.
@echo off
set "my_path=%~dp0"
set file_size_command=switch((ls -r|measure -s Length).Sum) {^
{$_ -gt 1GB} {'{0:0.0} GiB' -f ($_/1GB)^
break}^
{$_ -gt 1MB} {'{0:0.0} MiB' -f ($_/1MB)^
break}^
{$_ -gt 1KB} {'{0:0.0} KiB' -f ($_/1KB)^
break}^
default { "$_ bytes" }^
}
for /d %%a in ("%my_path%\*") do (
echo Folder name: %%~nxa
PowerShell.exe -noexit -command %file_size_command%
)
pause
rem Let the user view this
It's supposed to just print each folder's name and then the size of the folder. Currently, it interprets the {$_ as a command.