I need to fix variable ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
G.SYLVAIN
Posts: 6
Joined: 30 Oct 2018 19:52

I need to fix variable ?

#1 Post by G.SYLVAIN » 03 Nov 2018 20:07

I need to keep variable %_DATE% fixed. Bot the variable user %USERNAME% can be change in other computers.
Keep aligned text.

Code: Select all

...
::
:MENU
::
	SETLOCAL ENABLEDELAYEDEXPANSION
	CLS & COLOR 0F
	MODE CON: COLS=84 LINES=37
	TITLE === WINDOWS_MAINTENANCE_HELPER ===
::
    	CALL :FUNC_WINVER  _WVER & REM FUNCTION TO DETERMINE WINDOWS VERSION
    	CALL :FUNC_DATE      _DATE & REM SET DATE
	CALL :FUNC_TIME      _HOUR & REM SET TIME
::	SETUP VARIABLES
	SET M=;C=
	SET REV=REV. 3.2
::
ECHO   [42mHI %USERNAME% ^^![0m                                DADE: [42m %_DATE% [0m
ECHO   [42m%_WVER%[0m                                          TIME: [42m %_HOUR% [0m
...

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: I need to fix variable ?

#2 Post by aGerman » 04 Nov 2018 10:52

Sounds like you want to pad strings with spaces.

Code: Select all

@echo off &setlocal
set "spcs=                                                                                "

set "s1=abc"
set "s2=abcde"
set "n1=123"
set "n2=12345"

:: pad right
REM conacatenate the string and the spaces
set "s=%s1%%spcs%"
REM output the first 20 characters
echo %s:~0,20%end
set "s=%s2%%spcs%"
echo %s:~0,20%end

echo(

:: pad left
REM conacatenate the spaces and the string
set "n=%spcs%%n1%"
REM output the last 20 characters
echo begin%n:~-20%
set "n=%spcs%%n2%"
echo begin%n:~-20%

pause
Steffen

G.SYLVAIN
Posts: 6
Joined: 30 Oct 2018 19:52

Re: I need to fix variable ?

#3 Post by G.SYLVAIN » 04 Nov 2018 13:59

I undestand you say, but your variable you have set at the top never change.
How to calculate lent or referance like "10" characters, if %username% it <10 add pad or >10 remove pad ?
Because the username is never same in other account

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: I need to fix variable ?

#4 Post by aGerman » 04 Nov 2018 15:03

It doesn't matter if the values change or not. I tried to explain it by setting values with different lengths. As you can see the way how to manipulate the values is always the same regardless of their initial length. The only constant variable that you need is spcs that contains a number of spaces that will always suffice to pad the strings.

You'll certainly agree that %random% doesn't expand to values with constant length ...

Code: Select all

echo off &setlocal
set "spcs=                                                                                "


:loop

set "rand=%random%"
set "a=%rand%%spcs%"
set "b=%spcs%%rand%"

echo %a:~0,10%#%b:~-10%
>nul timeout /t 1 /nobreak

goto loop
Steffen

G.SYLVAIN
Posts: 6
Joined: 30 Oct 2018 19:52

Re: I need to fix variable ?

#5 Post by G.SYLVAIN » 04 Nov 2018 16:35

you're right, but I do not know how to apply it. [0m, for ESC character for color in windows 10.
at botom the script. I have 2 variables %USERNAME% And %DATE% on same line.
I work whit COLS=84

Code: Select all

::
:MENU
::
	SETLOCAL ENABLEDELAYEDEXPANSION
	CLS & COLOR 0F
	MODE CON: COLS=84 LINES=37
	TITLE === WINDOWS_MAINTENANCE_HELPER ===
::
        CALL :FUNC_WINVER  _WVER & REM FUNCTION TO DETERMINE WINDOWS VERSION
        CALL :FUNC_DATE    _DATE & REM SET DATE
	CALL :FUNC_TIME    _HOUR & REM SET TIME
::	SETUP VARIABLES
	SET M=;C=
	SET REV=REV. 3.2
::  TEXT ILINGMENT
	SET "SPC=                                                                                                           "	
	SET "UN=[42m HI %USERNAME% ^^![0m %SPC%
	SET "DN=DADE: [42m %_DATE% [0m %SPC%
::
ECHO %UN:~0,42%%DN:~-42% & rem  notwoking ?

ECHO   [42m%_WVER%[0m              TIME: [42m %_HOUR% [0m

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: I need to fix variable ?

#6 Post by aGerman » 04 Nov 2018 18:43

There is no reason to manipulate the second half of the line in your case. But for the first half you need to consider the escape character is an additional character, the left bracket is an additional character, etc. for the string saved in UN.

Code: Select all

@echo off &setlocal EnableDelayedExpansion
for /f %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
set "spcs=                                                                                "

set "UN=!ESC![42m HI %username% ^! !ESC![0m%spcs%"
echo   !UN:~0,49!DATE: !ESC![42m %date% !ESC![0m

pause
Steffen

G.SYLVAIN
Posts: 6
Joined: 30 Oct 2018 19:52

Re: I need to fix variable ?

#7 Post by G.SYLVAIN » 04 Nov 2018 20:31

thank you very much, you solved my script ! :D

Post Reply