If...Then Not Working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MikeInThe901
Posts: 6
Joined: 24 Apr 2023 11:13

If...Then Not Working

#1 Post by MikeInThe901 » 30 Jul 2023 10:29

The code below works to set a variable CLetter based on the first letter of a new customer last name, input from the keyboard, if the first letter is 'A'. I need it to compare for A-J, not just A.

@echo off
setlocal ENABLEDELAYEDEXPANSION

set /p CName="Please enter new customer as LastName, FirstName: "
set CLetter=%CName:~0,1%

if "%CLetter%"=="A" (
set CGroup=Client Files A-J
) else (
set CGroup=Client Files K-Z
)

echo %CGroup%
pause

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: If...Then Not Working

#2 Post by Batcher » 30 Jul 2023 20:50

test-1.bat

Code: Select all

@echo off
setlocal enabledelayedexpansion

set "StrAJ=ABCDEFGHIJ"
set /p CName="Please enter new customer as LastName, FirstName: "
set "CLetter=%CName:~0,1%"

if "!StrAJ:%CLetter%=!" neq "!StrAJ!" (
    set "CGroup=Client Files A-J"
) else (
    set "CGroup=Client Files K-Z"
)

echo,%CGroup%
pause

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: If...Then Not Working

#3 Post by Batcher » 30 Jul 2023 20:54

test-2.bat

Code: Select all

@echo off
set "StrAJ=ABCDEFGHIJ"
set /p CName="Please enter new customer as LastName, FirstName: "
set "CLetter=%CName:~0,1%"

echo,%StrAJ% | findstr /i "%CLetter%" >nul
if not errorlevel 1 (
    set "CGroup=Client Files A-J"
) else (
    set "CGroup=Client Files K-Z"
)

echo,%CGroup%
pause

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

Re: If...Then Not Working

#4 Post by miskox » 30 Jul 2023 23:46

Code: Select all

@echo off
set "StrAJ=ABCDEFGHIJ"
set /p CName="Please enter new customer as LastName, FirstName: "
set "CLetter=%CName:~0,1%"

set "CGroup=Client Files K-Z"
(echo,%StrAJ% | findstr /i "%CLetter%" >nul)&&set "CGroup=Client Files A-J"
echo,%CGroup%
pause

MikeInThe901
Posts: 6
Joined: 24 Apr 2023 11:13

Re: If...Then Not Working

#5 Post by MikeInThe901 » 31 Jul 2023 08:02

Awesome! Thank you Batcher!

Post Reply