0

Trying to make my batch compatible across various windows versions including Windows NT.

I need to return the last character of a variable. Normally I would use %foo:~-1% to return although this does not work on Windows NT.

I have read WinNT version of set /? to no avail.

Using %foo:~x,1% where x is the position of last character won't work for me as the variable length will change.

I thought of running a script to check str length with one of many methods, then running !foo:~%x%,1! but to my knowledge the reason I cant get this to work is lack of delayed expansion in WinNT and a lot of coding for something I'm hoping to be a simple fix.

Any ideas how to tackle this?

Thanks

aschipfl
  • 33,626
  • 12
  • 54
  • 99
ryan
  • 51
  • 2
  • 10
  • 3
    repeatedly remove a single character from the start of the string. When the string is empty, the last character removed was the last in the string. OR do the reverse. Try to get the 99th character of the string. If you come up dry, add a character to the start until you succeed. – Magoo Apr 07 '17 at 02:31
  • Is `findstr` available on NT? Can you run .vbs scripts on NT? Is there any other language that would be installed on the machine such as Perl, Python, etc.? If you can put your batch script onto the machine, can you also put a .exe on with it? – lit Apr 07 '17 at 02:40
  • Hi @lit I have no experience with perl python or vbs. what was your train of thought with findstr, as it is available on NT. – ryan Apr 07 '17 at 02:43
  • The `findstr` command is somewhat new. I do not know if it is on or can be put on Windows NT. If NT can run .vbs scripts, that would be easy enough to code up to get the last character of a string. – lit Apr 07 '17 at 02:48
  • @lit findstr is available on windows NT – ryan Apr 07 '17 at 02:52
  • Guessing something like .. echo %foo% | findstr /e . But can't get that to echo just the last character it's echoing whole str – ryan Apr 07 '17 at 02:58
  • Use the regex capability in findstr. I am not at a machine now. Will answer later if someone does not answer first. – lit Apr 07 '17 at 03:26
  • @lit thanks, I'm not familiar with that, I'll do some research and post if I get it to work otherwise look forward to hearing from you – ryan Apr 07 '17 at 03:27
  • 2
    Do you know if the `call set "char=%%foo:~%x%,1%%"` trick works in Win NT? If so, then it may be used instead of delayed expansion to calculate the length of the string in a simple way... – Aacini Apr 07 '17 at 04:17

2 Answers2

1
@ECHO Off
SETLOCAL

SET string=abcd
:loop1
SET lastchar=%string:~0,1%
SET string=%string:~1%
IF NOT "%string%"=="" GOTO loop1

ECHO last char=%lastchar%

SET string=wxyz
:loop2
SET lastchar=%string:~99,1%
SET string=q%string%
IF "%lastchar%"=="" GOTO loop2

ECHO last char=%lastchar%

GOTO :EOF

I can no longer remember whether the set "var=value" syntax worked on NT. If it does, that syntax is preferred to skirt the invisible-trailing-spaces problem.

and the findstr approach:

set string=pqrs
for %%a in (a b c d ... o p q r s ...z A ... Z etc.) do echo %string%|findstr /e /L /C:"%%a">nul do if not errorlevel 1 set lastchar=%%a

Naturally, if you apply /i to the findstr then you can omit one case of alpha-characters.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • This is brilliant thanks @Magoo however I have come across and issue with the loop code when string contains an equal sign = Windows NT doesn't accept set string== 1 - however will accept set string=^^= 1 any further ideas? WinNT is doing my head in. thanks again – ryan Apr 07 '17 at 09:10
0

@Magoo

Think I have it sorted

set string= = 1 

without error I have modified code as below. I am getting an output although a "The syntax of the command is incorrect" error when loop tries

set lastchar== 1

that's the reason I have added 2>nul to suppress error

@ECHO Off
SETLOCAL

SET string=abcd = 1
:loop1
SET lastchar=%string:~1,1% 2>nul
SET string= %string:~2%
IF NOT "%string%"=="" GOTO loop1

ECHO last char=%lastchar%
ryan
  • 51
  • 2
  • 10