Reputation: 59
I'm running this as a batch file
/k
cd c:\
for /f "tokens=1-3" %a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"') do @echo wsh.echo "%b" ^& " free=" ^& FormatNumber^(cdbl^(%a^)/1024/1024/1024, 2^)^& " GiB"^& " size=" ^& FormatNumber^(cdbl^(%c^)/1024/1024/1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%c"=="" @echo( & @cscript //nologo %temp%\tmp.vbs & del %temp%\tmp.vbs
pause
When I run the for loop in CMD on its own, it works just fine. But when I attempt to run it in a batch file it closes CMD before I can get the information. It also fails to actually change the directory to c:\, but only once the for loop is added to the batch file. Do I need to pre-fix the loop with something so that CMD knows how to handle it?
Upvotes: 1
Views: 217
Reputation: 18827
You should do like this with a batch file :
When you ran it as batch file you should add the sign percent % to the variable to escape it so %a
must be %%a
and son on ...
@echo off
for /f "tokens=1-3" %%a in ('WMIC LOGICALDISK GET FreeSpace^,Name^,Size ^|FINDSTR /I /V "Name"') do @echo wsh.echo "%%b" ^& " free=" ^& FormatNumber^(cdbl^(%%a^)/1024/1024/1024, 2^)^& " GiB"^& " size=" ^& FormatNumber^(cdbl^(%%c^)/1024/1024/1024, 2^)^& " GiB" > %temp%\tmp.vbs & @if not "%%c"=="" @echo( & @cscript //nologo %temp%\tmp.vbs & del %temp%\tmp.vbs
pause
Upvotes: 1