Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do i get the next letter in alphabet?

1,530 views
Skip to first unread message

Carsten Beckermann

unread,
Feb 12, 2006, 3:23:19 PM2/12/06
to
Hello

I want to print out the next letter in alphabet, if i input some letter.

=============================================
set last=f

...

call :lastest a b c d e f g h i j k l m n o p q r s t u v w x y z

goto :EOF

:lastest
if "%1" == "%last%" goto output
shift
goto :lastest

:output
echo After %last% comes %2
============================================

It think that way is too much complicated.
How can i make it easier?


Greetings
Carsten


RazTK

unread,
Feb 12, 2006, 3:40:27 PM2/12/06
to
What do you think about this one:

@echo off
set /p last=
if not defined last goto :eof
for %%? in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if
"!x!"=="1" (echo.
echo After %last% comes %%?.
echo.
pause
goto :eof) else if /i "%last%"=="%%?" set /a x+=1
goto :eof

Dr John Stockton

unread,
Feb 13, 2006, 8:49:48 AM2/13/06
to
JRS: In article <dso5fp$chu$03$1...@news.t-online.com>, dated Sun, 12 Feb
2006 21:23:19 remote, seen in news:alt.msdos.batch.nt, Carsten
Beckermann <carstenbec...@web.de> posted :

>
>I want to print out the next letter in alphabet, if i input some letter.

Here's a demonstration of a generalisable look-up mechanism :-

set WOT=d
echo abcdefghijklmnopqrstuvwxyza | mtr -xo .*%WOT%(\w).* = \1
-> e

Presumably SED could be used in place of MiniTrue (mtr).

set WOT=Mon
echo Sun Mon Tue Wed Thu Fri Sat Sun | mtr -xo .*%WOT%.(\w+).* = \1
-> Tue

set WOT=Mon
echo Sun Sonntag Mon Montag Tue Dienstag ... | mtr -xo ".*%WOT% (\w+).*" = \1
-> Montag

That'll also work in DOS (using mt), and in UNIX.
Type can be used instead of echo.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/>

Harlan Grove

unread,
Feb 13, 2006, 3:56:23 PM2/13/06
to
Dr John Stockton wrote...

>JRS: In article <dso5fp$chu$03$1...@news.t-online.com>, dated Sun, 12 Feb
>2006 21:23:19 remote, seen in news:alt.msdos.batch.nt, Carsten
>Beckermann <carstenbec...@web.de> posted :
>>
>>I want to print out the next letter in alphabet, if i input some letter.
>
>Here's a demonstration of a generalisable look-up mechanism :-
>
> set WOT=d
> echo abcdefghijklmnopqrstuvwxyza | mtr -xo .*%WOT%(\w).* -> e

>
>Presumably SED could be used in place of MiniTrue (mtr).
...

It could, but tr would be apter.

echo %letter% | tr a-z b-za

> set WOT=Mon
> echo Sun Mon Tue Wed Thu Fri Sat Sun | mtr -xo .*%WOT%.(\w+).* -> Tue
...

Outside .exe's unnecessary for this.

:: -- begin batch file --
@echo off & setlocal enabledelayedexpansion
if "%~1" == "" (echo Usage: %0 item "alternatives" [before] [after]&
goto :EOF)
if "%~2" == "" (echo Usage: %0 item "alternatives" [before] [after]&
goto :EOF)
set getnext=%1
for %%a in (%~2) do if %%a == %1 (
if "!getnext!" == "%1" set getnext=
) else (
if "!getnext!" == "" set getnext=%%a
)
call echo %~3%%getnext%%%~4
:: -- end batch file --

Name it foo.bat, then

foo Wed "Mon Tue Wed Thu Fri Sat Sun Mon"
-> Thu

foo Wed "Mon Tue Wed Thu Fri Sat Sun Mon" "%1 followed by " "."
-> Wed followed by Thu.

Clay Calvert

unread,
Feb 13, 2006, 6:28:44 PM2/13/06
to
On Sun, 12 Feb 2006 21:23:19 +0100, "Carsten Beckermann"
<carstenbec...@web.de> wrote:

>I want to print out the next letter in alphabet, if i input some letter.

With a seldom used capability of the "set" command.

set alpha=abcdefghijklmnopqrstuvwxyz
call set beta=%%alpha:*%1=%%
echo After %1 comes %beta:~,1%

It is NOT case sensitive. Is that OK?

Cheers,
Clay Calvert
CCal...@Zanguru.com
Replace "Z" with "L"

Clay Calvert

unread,
Feb 13, 2006, 11:21:21 PM2/13/06
to
On Mon, 13 Feb 2006 18:28:44 -0500, Clay Calvert
<ccal...@Zanguru.com> wrote:

>On Sun, 12 Feb 2006 21:23:19 +0100, "Carsten Beckermann"
>

>>I want to print out the next letter in alphabet, if i input some letter.
>
>With a seldom used capability of the "set" command.
>
>set alpha=abcdefghijklmnopqrstuvwxyz
>call set beta=%%alpha:*%1=%%
>echo After %1 comes %beta:~,1%
>
>It is NOT case sensitive. Is that OK?

Here is the routine fleshed out a little more, and different usages of
this technique to follow.

=============================================

set letter=%1

set alpha=abcdefghijklmnopqrstuvwxyza

call set beta=%%alpha:*%letter%=%%

Set last=%beta:~,1%

echo After %letter% comes %last%

=============================================

The ":*" feature of SET will reverse truncate, for lack of a better
term, up to the first match.

C:\set test=acdefg

Without the star the "c" gets removed below.
C:\>echo %test:c=%
adefg

With the star, everything up to, and including "c" gets removed.
C:\>echo %test:*c=%
defg

Then all that is needed is to grab just the first charcter of the
remaining string with the ":~1" construct.

Some of you may have noticed the extra "a" at the end of %alpha% in
this repost. It is there in case one wanted to loop back again after
reaching the end of the alphabet. Neither "a" nor "z" throw an error
as built, because Set's substitution stops at the first match.
Entering "a" in this example will stop at the first character, not the
last.

Here is another use that is more applicable for looping; Week Days, as
in Harlan Grove's example.

=============================================

set Day=%1

set week=SunMonTueWedThuFriSatSun

call set SubWeek=%%Week:*%day%=%%

Set NextDay=%SubWeek:~,3%

echo After %Day% comes %NextDay%

=============================================

Examples below.

C:\>getnextday sun
After sun comes Mon

C:\>getnextday mon
After mon comes Tue

C:\>getnextday fri
After fri comes Sat

C:\>getnextday sat
After sat comes Sun


This could be used for Months and other 'looping' sequences as well,
as long as non-repeating substrings of uniform size are used. Even
hours could be handled if single digit values are preceded by a zero.
Examples.

set Month=JanFebMarAprMayJunJulAugSepOctNovDecJan

set Hour=01020304050607080910111201

set Mil=0001020304050607080910111213141516171819202122232401

In the above example, entering "00" or "24" will get "01" for an
answer in military time.

Single digit Hex values, Musical Notes, etc., could be handled with
this technique.

Clay Calvert

unread,
Feb 13, 2006, 11:44:27 PM2/13/06
to
On Mon, 13 Feb 2006 23:21:21 -0500, Clay Calvert
<ccal...@Zanguru.com> wrote:

>Even >hours could be handled if single digit values are preceded by a zero.
>Examples.

After clicking "send" this came to mind for handling hours without
padding with zeroes

=============================================

set Hour=%1

if "%Hour:~,2%"=="" Set Hour= %Hour%

set Hours= 1 2 3 4 5 6 7 8 9101112 1

call set SubHours=%%Hours:*%Hour%=%%

Set NextHour=%SubHours:~,2%

echo After %Hour% comes %NextHour%

=============================================

The single digit hours will be padded with spaces but that can be
handy when lining up columns in multiple rows of output.

Timo Salmi

unread,
Feb 14, 2006, 12:00:14 AM2/14/06
to
Clay Calvert wrote:
> set Day=%1
> set week=SunMonTueWedThuFriSatSun
> call set SubWeek=%%Week:*%day%=%%
> Set NextDay=%SubWeek:~,3%
> echo After %Day% comes %NextDay%

That one is exceptionally clever script code. Definitely FAQ material
with a reference to your solution at Google news repository.

Incidentally, in Finnish one would use for the original next letter
task
set letter=%1
set alpha=abcdefghijklmnopqrstuvwxyzهنِa


call set beta=%%alpha:*%letter%=%%
Set last=%beta:~,1%
echo After %letter% comes %last%

I wanted to make sure that beyond 127 ASCII also works. It does.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Message has been deleted

RazTK

unread,
Feb 14, 2006, 9:05:05 AM2/14/06
to
Awesome job, Clay.
It's very bad that a command like this does not work at all: set
x=%x:h*=% for example.
Then, you could get what comes before: echo %x:~-1%.

RazTK

unread,
Feb 14, 2006, 9:24:22 AM2/14/06
to
Oh well, you can do the same thing using FOR loop:

@echo off
set x=abcdefghijklmnopqrstuvwxyz
for /f "tokens=1 delims=%~1" %%? in ("%x%") do set char=%%?
echo Before %~1 comes %char:~-1%.
call set char=%%x:*%~1=%%
call echo After %~1 comes %char:~0,1%.
goto :eof

Clay Calvert

unread,
Feb 14, 2006, 10:46:09 AM2/14/06
to

Very nice Raz. I tried it but had some issues in addition to how it
would act with "a" and "z". So here is a variant of what you posted.

set x=abcdefghijklmnopqrstuvwxyza
for /f "tokens=1,2 delims=%~1" %%a in ("%x%") do (
set PreChar=y%%a&set PostChar=%%bb)
echo %PreChar:~-1% %~1 %PostChar:~0,1%

I had been working on a pre/post day-of-the-week routine. Here it is:

set Day=%1
set week=SunMonTueWedThuFriSatSun
call set SplitWeek=%%Week:%day%= %%
for /f "tokens=1,2" %%a in ("%SplitWeek%") do (
Set PreDay=Sat%%a&set PostDay=%%bMon)
echo %PreDay:~-3,3% %1 %PostDay:~,3%

Cheers,

Message has been deleted
Message has been deleted
Message has been deleted

RazTK

unread,
Feb 14, 2006, 11:33:54 AM2/14/06
to
I'll check what you posted.
As for days:

@echo off
set Week=SunMonTueWedThuFriSat
for %%? in (%*) do call :Get %%?
goto :eof
:Get
set Day=%~1
for %%? in (Sun Mon Tue Wed Thu Fri Sat) do call set
Day=%%Day:%%?=%%?%%
for /f %%? in ("!Week:%~1= !") do set x=%%?
echo Before %Day% comes %x:~-3%.
for /f "tokens=2" %%? in ("!Week:%~1= !") do set x=%%?
echo After %Day% comes %x:~0,3%.
goto :eof

Clay Calvert

unread,
Feb 15, 2006, 8:32:00 PM2/15/06
to
On 14 Feb 2006 08:33:54 -0800, "RazTK" <raz...@gmail.com> wrote:

Raz, these are really nice techniques that you've posted. Thanks for
sharing.

b.ped...@get2net.dk

unread,
Feb 16, 2006, 10:55:32 PM2/16/06
to
No, it dos not work with letters beyond 127 ASCII.
Heres two of my solutions (both working from a...z and 0...9):
If it should handle more letters, I dont think that could be done
without VBS.

@echo off & setlocal enableextensions enabledelayedexpansion
set /p c=Input: &::
if "%c%" == "" goto:eof

call:VBS %c%
echo VBS said: After %c% comes %r%

call:DOS %c%
echo DOS said: After %c% comes %r%
echo. & pause

goto:eof
:VBS
for %%f in ("%tmp%\tmp.vbs") do (
echo > %%f wscript.echo chr^(asc^("%~1"^)+1^)
for /f %%r in ('cscript //nologo %%f') do del %%f & set r=%%r)

goto:eof
:DOS
set r=abcdefghijklmnopqrstuvwxyz 0123456789 .
set r=!r:*%~1=! & set r=!r:~,1!

Timo Salmi skrev:

> Clay Calvert wrote:
> > set Day=%1
> > set week=SunMonTueWedThuFriSatSun
> > call set SubWeek=%%Week:*%day%=%%
> > Set NextDay=%SubWeek:~,3%
> > echo After %Day% comes %NextDay%
>
> That one is exceptionally clever script code. Definitely FAQ material
> with a reference to your solution at Google news repository.
>
> Incidentally, in Finnish one would use for the original next letter
> task
> set letter=%1

> set alpha«cdefghijklmnopqrstuvwxyzåäöa

b.ped...@get2net.dk

unread,
Feb 17, 2006, 1:00:52 AM2/17/06
to
Your prefix to %%a is not needed :-)
Benny Pedersen,
BTW. The extra space that google put after each line
could spoil our work. But since I have used this:
(set PreChr=%%a)
then it can't postfix the variable with an extra space char.
PS. demo:

@echo off & setlocal enableextensions

for %%# in (Blank Overflow) do (
echo.&echo Result Function %%# ^(0...9:^)
for /l %%i in (0,1,9) do (call:#_%%# %%i)
)
pause>nul

(goto:eof
:#_Blank
for /f "tokens=1,2 delims=%~1" %%a in (" 0123456789")do (
(set PreChr=%%a)
(set PstChr=%%b )
)
echo [%PreChr:~-1%] [%~1] [%PstChr:~,1%]
)

(goto:eof
:#_Overflow
for /f "tokens=1,2 delims=%~1" %%a in ("01234567890")do (
(set PreChr=%%a)
(set PstChr=%%b1)
)
echo [%PreChr:~-1%] [%~1] [%PstChr:~,1%]
)

Clay Calvert skrev:

Timo Salmi

unread,
Feb 17, 2006, 1:32:54 AM2/17/06
to
b.ped...@get2net.dk wrote:
> No, it dos not work with letters beyond 127 ASCII.

I wrote earlier:


>>Incidentally, in Finnish one would use for the original next letter
>>task
>> set letter=%1

>> set alpha=abcdefghijklmnopqrstuvwxyzåäöa


>> call set beta=%%alpha:*%letter%=%%
>> Set last=%beta:~,1%
>> echo After %letter% comes %last%
>>I wanted to make sure that beyond 127 ASCII also works. It does.

C:\_D\TEST>cmdfaq å
After å comes ä

C:\_D\TEST>cmdfaq ä
After ä comes ö

C:\_D\TEST>cmdfaq ö
After ö comes a

C:\_D\TEST>cmdfaq Å
After Å comes ä

C:\_D\TEST>cmdfaq Ä
After Ä comes ö

C:\_D\TEST>cmdfaq Ö
After Ö comes a

b.ped...@get2net.dk

unread,
Feb 17, 2006, 3:39:09 AM2/17/06
to
...SNIP...
This better:

@echo off & setlocal enableextensions enabledelayedexpansion


for %%# in (Blank Overflow) do (
echo.&echo Result Function %%# ^(0...9:^)
for /l %%i in (0,1,9) do (
call:#_%%# %%i

echo [!PreChr:~-1!] [%%i] [!PstChr:~,1!]
)
)
pause>nul

goto:eof
:#_Blank
for /f "tokens=1* delims=%1" %%a in (" 0123456789")do (
set PreChr=%%a&(set PstChr=%%b ))
goto:eof
:#_Overflow
for /f "tokens=1* delims=%1" %%a in ("01234567890")do (
set PreChr=%%a&(set PstChr=%%b1))

:EOF
Benny Pedersen,
PS. To compare different solutions, (Sol1, Sol2)
ex. Pad0Left = mid(s & Str, Len(Str), Places)
with Pad0Left = right(String(Places,"0")& Str,Places)
I wrote a little "function PadStrRight(str)" in line numb 9.

BTW. the below DEMO shows that the second solution could
be used instead of the first solution.


@eCHo off
@eCHo.&title=CompareValidateFunctions.CMD by Benny Pedersen.
find /v "@eCHo" < "%~fs0" > "%tmp%\tmp.vbs"
@eCHo.&wscript.exe //nologo "%tmp%\tmp.vbs"
@eCHo.& del "%tmp%\tmp.vbs"
@eCHo.&goto:eof

'Increase length of string.
function PadStrRight(byVal str)
PadStrRight= 1+asc(right("@"& str,1))
PadStrRight= str& chr(PadStrRight+(PadStrRight>90)*26)
end function

dim Result,Arg1,Arg2,i,Sol1,Sol2,LenArg1,Arr1,Arr2
for Arg2 = 0 to 10
Arg1= ""
Sol1= "": Sol2= ""
for LenArg1= 0 to 26
Sol1= Sol1 & Sol1_Pad0Left(Arg1,Arg2) & vbLf
Sol2= Sol2 & Sol2_Pad0Left(Arg1,Arg2) & vbLf
Arg1= PadStrRight(Arg1)
next
Result= "Arg1: (String) " & "[]...[" _
& left(Arg1,len(Arg1)-1) & "]" & vbLf
Result= Result & "Arg2: (Places) " & Arg2 & vbLf
Result= Result & "Validity(1): " & (Sol1=Sol2) & vbLf
Arr1= Split(Sol1,vbLf)
Arr2= Split(Sol2,vbLf)
for i= 0 to uBound(Arr1)-1
Result= Result & vbLf & i & ": "& Arr1(i) & vbTab & Arr2(i)
next
wscript.echo Result
next

function Sol1_Pad0Left(Str,Places)
dim i,s
for i = 1 to Places-1
s = s & "0"
next
on error resume next
Sol1_Pad0Left = Mid(s & Str, Len(Str), Places)
if err then
Sol1_Pad0Left= " [Error] "
elseIf len(Str) > Places then
Sol1_Pad0Left= Sol1_Pad0Left & " [Truncate]"
else Sol1_Pad0Left= Sol1_Pad0Left & " [Ok] "
end if
on error goto 0
end function

function Sol2_Pad0Left(Str,Places)
on error resume next
Sol2_Pad0Left= Right(String(Places, "0")& Str, Places)
if err then
Sol2_Pad0Left= "" & " [Error] "
elseIf len(Str) > Places then
Sol2_Pad0Left= Sol2_Pad0Left & " [Truncate]"
else Sol2_Pad0Left= Sol2_Pad0Left & " [Ok] "
end if
on error goto 0
end function

'Increase length of integer.
function PadIntRight(byVal int)
PadIntRight= 1+asc(right("/"& int,1))
PadIntRight= int& chr(PadIntRight+(PadIntRight>57)*10)
end function

b.ped...@get2net.dk

unread,
Feb 17, 2006, 3:51:21 AM2/17/06
to
Hmmm, then its my computer thats wrong.
All special letters such as ä, said: after ä comes a
after whatever special char comes a...

Benny,

@echo off

RazTK

unread,
Feb 17, 2006, 11:21:52 AM2/17/06
to
The following works from 'a' to 'z' and from '0' to '9':

@echo off
set x=abcdefghijklmnopqrstuvwxyz0123456789


for %%? in (%*) do call :Get %%?
goto :eof
:Get

for /f "delims=%~1" %%? in ("%x%") do set char=%%?


echo Before %~1 comes %char:~-1%.

for /f "tokens=2 delims=%~1" %%? in ("%x%") do set char=%%?


echo After %~1 comes %char:~0,1%.

goto :eof

0 new messages