ohade
ohade

Reputation: 161

remove spaces from start of variable batch

i'm doing some maintenence script and want to get defragment status like this:

@echo off
setlocal EnableExtensions
defrag c: -a>%TEMP%\defrag_c_fixed.txt
for /f "skip=4 delims=,(  tokens=4" %%a in (%TEMP%\defrag_c_fixed.txt) do set fragment=%%a
for /f "skip=5 tokens=*" %%a in (%TEMP%\defrag_c_fixed.txt) do set recommend=%%a
echo !fragment!
echo !recommend!
pause

I'm getting fragment status with spaces in the start of the numbers:

  20% Fragmented
You should defragment this volume.
Press any key to continue . . .

How can i delete this from script?

Upvotes: 0

Views: 213

Answers (2)

SachaDee
SachaDee

Reputation: 9545

Like this :

for 1 space:

echo !fragment:~1!

Upvotes: 0

dbenham
dbenham

Reputation: 130919

You can use FOR /F to strip leading spaces. Using TOKENS=* will strip leadiing delimiters, and the default delimiters are space and tab.

for /f "tokens=*" %%A in ("!fragment!") do echo(%%A

Upvotes: 3

Related Questions