Fille spaces from the end of a string via "FOR" command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Fille spaces from the end of a string via "FOR" command

#1 Post by darioit » 11 Apr 2016 09:44

Hello I find this example:

Code: Select all

set str=15 Trailing Spaces to truncate               &rem
echo."%str%"
for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
echo."%str%"


and this is the output

"15 Trailing Spaces to truncate space space space "
"15 Trailing Spaces to truncate"

I have a variable string lenght to put in a fix 40 chars, as this example

"40 Trailing Spaces to fille"
"40 Trailing Spaces to fille space space space "

Space=blank

How can I achive this result?

Thank you in advance

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Fille spaces from the end of a string via "FOR" command

#2 Post by Squashman » 11 Apr 2016 11:42

Make a variable with 40 spaces or a 100 spaces. Really does not matter. Combine the variables together. Then substring the combined variable.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Fille spaces from the end of a string via "FOR" command

#3 Post by darioit » 11 Apr 2016 22:41

good, like this:

Code: Select all

for %%a in ('command') do (
set MYVAR=                                        %MYVAR%
set MYVAR=%a%%MYVAR%
set MYVAR=%MYVAR:~-40%
)

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Fille spaces from the end of a string via "FOR" command

#4 Post by Thor » 14 Apr 2016 23:17

I've tried to fill blank spaces to the end of all filenames in a directory to make all of them with a 50 characters long.

Here is my batch file:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set i=0
for /f %%a in ('dir /b') do (
   set /a i+=1
   set "line[!i!]=%%a"
   strlen line[!i!]
   set /a fs=50-!errorlevel!
   set /a x[!i!]=!fs!
)
:: space var contains 50 spaces...
set "space=                                                  "
for /L %%f in (1 1 !i!) do (
   set "MYVAR=[!line[%%f]!!space:~0,!x[%%f]!!]"
   echo !MYVAR!
)
endlocal


But I've just got the following output:
[demo.batx[1]]
[FirstOne.batx[2]]
[Second.batx[3]]
[Third.batx[4]]
[FillRightSpace.batx[5]]
[XYZ.batx[6]]

What's wrong with it?
Last edited by Thor on 15 Apr 2016 09:40, edited 1 time in total.

miskox
Posts: 555
Joined: 28 Jun 2010 03:46

Re: Fille spaces from the end of a string via "FOR" command

#5 Post by miskox » 15 Apr 2016 00:08

You could use somethinig like this:

Code: Select all

@echo off
set textvar=some text
set textvar=%textvar%                                                  &rem 50 spaces
set textvar=%textvar:~0,50%

echo length            1         2         3         4         5         6_
echo length  _123456789012345678901234567890123456789012345678901234567890_
echo textvar=_%textvar%_


Output

Code: Select all

c:\
length            1         2         3         4         5         6_
length  _123456789012345678901234567890123456789012345678901234567890_
textvar=_some text                                         _
c:\


Saso

penpen
Expert
Posts: 1996
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Fille spaces from the end of a string via "FOR" command

#6 Post by penpen » 15 Apr 2016 02:43

Thor wrote:What's wrong with it?

The issue is located in this line:

Code: Select all

   set "MYVAR=[!line[%%f]!!space:~0,!x[%%f]!!]"

You cannot use delayed expanded values within the same delayed expansion step,
because the exclamation marks ('!') are used to define these variables.
In your case the processor is reading these variables (without doublequotes):

Code: Select all

"line[%%f]"
"space:~0,"
""

So you have to compute "x[%%f]" in an earlier expansion step; you could use a for variable:

Code: Select all

   for %%x in ("!x[%%f]!") do set "MYVAR=[!line[%%f]!!space:~0,%%~x!]"


penpen

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Fille spaces from the end of a string via "FOR" command

#7 Post by Thor » 15 Apr 2016 09:29

miskox wrote:You could use somethinig like this:

Code: Select all

@echo off
set textvar=some text
set textvar=%textvar%                                                  &rem 50 spaces
set textvar=%textvar:~0,50%

echo length            1         2         3         4         5         6_
echo length  _123456789012345678901234567890123456789012345678901234567890_
echo textvar=_%textvar%_


Output

Code: Select all

c:\
length            1         2         3         4         5         6_
length  _123456789012345678901234567890123456789012345678901234567890_
textvar=_some text                                         _
c:\


Saso


Thanks miskox your method works just fine! :)

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: Fille spaces from the end of a string via "FOR" command

#8 Post by Thor » 15 Apr 2016 09:31

penpen wrote:
Thor wrote:What's wrong with it?

The issue is located in this line:

Code: Select all

   set "MYVAR=[!line[%%f]!!space:~0,!x[%%f]!!]"

You cannot use delayed expanded values within the same delayed expansion step,
because the exclamation marks ('!') are used to define these variables.
In your case the processor is reading these variables (without doublequotes):

Code: Select all

"line[%%f]"
"space:~0,"
""

So you have to compute "x[%%f]" in an earlier expansion step; you could use a for variable:

Code: Select all

   for %%x in ("!x[%%f]!") do set "MYVAR=[!line[%%f]!!space:~0,%%~x!]"


penpen


Thanks penpen for a pinpoint my mistake about the expansion of delayedexpansion.

Here is my final working batch file:

Code: Select all

@echo off
setlocal enabledelayedexpansion
set i=0
for /f %%a in ('dir /b') do (
   set /a i+=1
   set "line[!i!]=%%a"
   strlen line[!i!]
   set /a fs=50-!errorlevel!
   set /a x[!i!]=!fs!
)
:: space var contains 50 spaces...
set "space=                                                  "
for /L %%f in (1 1 !i!) do (
   for %%k in ("!x[%%f]!") do (
      set "MYVAR=[!line[%%f]!!space:~0,%%~k!]"
      echo !MYVAR!
   )
)
endlocal

Post Reply