Link to home
Start Free TrialLog in
Avatar of ReneGe
ReneGeFlag for Canada

asked on

Batch File: Remove spaces in variable

Hi there,

I am looking for a way to remove the spaces in a variable with less code as below.

For example, insted of     :  SET A=%%A && SET A=!A: =!
It would be something like :  SET A=!%%A: =!

Thanks for your help,
Rene



WORKING SCRIPT:
***************************************************************
@ECHO OFF

setlocal enabledelayedexpansion
set String=This      ,is ,    a     ,big sentence.

for /f "tokens=1-4 delims=," %%A in ('echo !String!') do (
  echo BEFORE: %%A %%B %%C %%D
  SET A=%%A && SET A=!A: =!
  SET B=%%B && SET B=!B: =!
  SET C=%%C && SET C=!C: =!
  SET D=%%D
)
echo AFTER: !A! !B! !C! !D!
pause


WANT TO DO SOMETHING LIKE THIS (BUT OF CORSE, IT DOES NOT WORK)
***************************************************************
setlocal enabledelayedexpansion
set String=This      ,is ,    a     ,big sentence.

for /f "tokens=1-4 delims=," %%A in ('echo !String!') do (
  echo BEFORE: %%A %%B %%C %%D
  SET A=!%%A: =!
  SET B=!%%B: =!
  SET C=!%%C: =!
  SET D=%%D
)
echo AFTER: !A! !B! !C! !D!
pause
Avatar of Bill Prew
Bill Prew

Do you really just want leading and trailing spaces removed, or do you want what you seem to have now, which is ALL spaces removed.  In your example, "big sentence." will become "bigsentence."  That doesn't feel like what you would want, but want to check.

~bp
Avatar of ReneGe

ASKER

Hi billprew,

You presumed right.

The result should be: This is a big sentence.

Thanks,
Rene
Well, not sure exactly what you are trying to do, but this is about the simplest approach:

for /f "tokens=1-5 delims=, " %%A in ('echo !String!') do (
  echo AFTER : %%A %%B %%C %%D %%E
)

If you really need the variables assigned separately, then this would work.

for /f "tokens=1-5 delims=, " %%A in ('echo !String!') do (
  echo BEFORE: %%A %%B %%C %%D %%E
  SET A=%%A
  SET B=%%B
  SET C=%%C
  SET D=%%D
  SET E=%%E
)
echo AFTER : %A% %B% %C% %D% %E%

~bp
Avatar of ReneGe

ASKER

But that still does not remove the spaces from A, B, C and D.

Is:  SET A=%%A && SET A=!A: =!    the only way to go.

Or there might be a nicer way to do it, something like: SET A=!%%A: =!


Thanks,
Rene
Avatar of ReneGe

ASKER

And by the way, all variables must absolutly be trimmed separatly.

Thanks,
Rene
==> But that still does not remove the spaces from A, B, C and D.

Actually it does, notice I added a space to the delims= parm of the FOR.  So the For command actually does the trimming.  If you run my code you will see it yields the desired results.  But that is just based on the example code you posed  If I better understood what you need to accomplish I might be able to get closer to what you want.

Are you concerned about the execution speed of the code, and therefore trying to remove lines?  Or just the way it looks and reads?

~bp
Avatar of ReneGe

ASKER

Well you got me by surprise.
Your script works indeed and does exactly what I need to do, with minimum scripting.

Just by curiaucity, do you think there is a way to do this with my suggested approach?

With something like: SET A=!%%A: =!

Thanks,
Rene
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneGe

ASKER

Nice!!

This was very educative.

Have yourself a good one.

Thanks,
Rene
Welcome, thank you.

~bp