Page 1 of 1

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

Posted: 11 Apr 2016 09:44
by darioit
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

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

Posted: 11 Apr 2016 11:42
by Squashman
Make a variable with 40 spaces or a 100 spaces. Really does not matter. Combine the variables together. Then substring the combined variable.

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

Posted: 11 Apr 2016 22:41
by darioit
good, like this:

Code: Select all

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

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

Posted: 14 Apr 2016 23:17
by Thor
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?

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

Posted: 15 Apr 2016 00:08
by miskox
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

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

Posted: 15 Apr 2016 02:43
by penpen
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

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

Posted: 15 Apr 2016 09:29
by Thor
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! :)

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

Posted: 15 Apr 2016 09:31
by Thor
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