How do I check if the three last characters of a variable match another variable?
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I have %var1%
and %var2%
var1="UserInput"
var2=file.log
If a user types %var1%
as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%
's three last characters or file format?
Another method that does the same thing is also welcome (But the user-input variable must be there)
windows-7 command-line batch script environment-variables
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
4
down vote
favorite
I have %var1%
and %var2%
var1="UserInput"
var2=file.log
If a user types %var1%
as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%
's three last characters or file format?
Another method that does the same thing is also welcome (But the user-input variable must be there)
windows-7 command-line batch script environment-variables
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have %var1%
and %var2%
var1="UserInput"
var2=file.log
If a user types %var1%
as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%
's three last characters or file format?
Another method that does the same thing is also welcome (But the user-input variable must be there)
windows-7 command-line batch script environment-variables
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I have %var1%
and %var2%
var1="UserInput"
var2=file.log
If a user types %var1%
as "file.txt", how can I (if possible) check only the three last characters (Or the file format if that's possible) to see if it matches %var2%
's three last characters or file format?
Another method that does the same thing is also welcome (But the user-input variable must be there)
windows-7 command-line batch script environment-variables
windows-7 command-line batch script environment-variables
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 19 mins ago


phuclv
8,47663488
8,47663488
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 12 hours ago


Tom
235
235
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Tom is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
How do I check if the three last characters of a variable match another variable?
Use variable substring syntax to extract the last 3 characters of each variable.
Example batch file:
@echo off
setlocal
set /p var1="UserInput: "
set var1last3=%var1:~-3%
set var2=file.log
set var2last3=%var2:~-3%
if %var1last3% == %var2last3% (
echo Match
) else (
echo No Match
)
endlocal
Further Reading
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- If - Conditionally perform command - Windows CMD - SS64.com
- Set - Environment Variable - Windows CMD - SS64.com
- variable substring - Windows CMD - SS64.com
It doesn't affect your answer's functionality, but why have you setenabledelayedexpansion
when you don't use any!var!
expansions?
â AFH
12 hours ago
@AFH Force of habit. I've removed it to avoid any confusion:)
â DavidPostillâ¦
10 hours ago
add a comment |Â
up vote
3
down vote
If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".
But the best option is to get the actual extension, regardless the length:
@echo off
setlocal
set "var1=file.Log"
set "var2=file.myLog"
:: Option 1 using FOR loops
for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
echo Match
) else (
echo No Match
)
: Option 2 using CALL parameters
call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
exit /b
:matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
if "%~x1"=="%~x2" exit /b 0
exit /b 1
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
How do I check if the three last characters of a variable match another variable?
Use variable substring syntax to extract the last 3 characters of each variable.
Example batch file:
@echo off
setlocal
set /p var1="UserInput: "
set var1last3=%var1:~-3%
set var2=file.log
set var2last3=%var2:~-3%
if %var1last3% == %var2last3% (
echo Match
) else (
echo No Match
)
endlocal
Further Reading
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- If - Conditionally perform command - Windows CMD - SS64.com
- Set - Environment Variable - Windows CMD - SS64.com
- variable substring - Windows CMD - SS64.com
It doesn't affect your answer's functionality, but why have you setenabledelayedexpansion
when you don't use any!var!
expansions?
â AFH
12 hours ago
@AFH Force of habit. I've removed it to avoid any confusion:)
â DavidPostillâ¦
10 hours ago
add a comment |Â
up vote
5
down vote
How do I check if the three last characters of a variable match another variable?
Use variable substring syntax to extract the last 3 characters of each variable.
Example batch file:
@echo off
setlocal
set /p var1="UserInput: "
set var1last3=%var1:~-3%
set var2=file.log
set var2last3=%var2:~-3%
if %var1last3% == %var2last3% (
echo Match
) else (
echo No Match
)
endlocal
Further Reading
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- If - Conditionally perform command - Windows CMD - SS64.com
- Set - Environment Variable - Windows CMD - SS64.com
- variable substring - Windows CMD - SS64.com
It doesn't affect your answer's functionality, but why have you setenabledelayedexpansion
when you don't use any!var!
expansions?
â AFH
12 hours ago
@AFH Force of habit. I've removed it to avoid any confusion:)
â DavidPostillâ¦
10 hours ago
add a comment |Â
up vote
5
down vote
up vote
5
down vote
How do I check if the three last characters of a variable match another variable?
Use variable substring syntax to extract the last 3 characters of each variable.
Example batch file:
@echo off
setlocal
set /p var1="UserInput: "
set var1last3=%var1:~-3%
set var2=file.log
set var2last3=%var2:~-3%
if %var1last3% == %var2last3% (
echo Match
) else (
echo No Match
)
endlocal
Further Reading
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- If - Conditionally perform command - Windows CMD - SS64.com
- Set - Environment Variable - Windows CMD - SS64.com
- variable substring - Windows CMD - SS64.com
How do I check if the three last characters of a variable match another variable?
Use variable substring syntax to extract the last 3 characters of each variable.
Example batch file:
@echo off
setlocal
set /p var1="UserInput: "
set var1last3=%var1:~-3%
set var2=file.log
set var2last3=%var2:~-3%
if %var1last3% == %var2last3% (
echo Match
) else (
echo No Match
)
endlocal
Further Reading
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- If - Conditionally perform command - Windows CMD - SS64.com
- Set - Environment Variable - Windows CMD - SS64.com
- variable substring - Windows CMD - SS64.com
edited 10 hours ago
answered 12 hours ago


DavidPostillâ¦
101k25213249
101k25213249
It doesn't affect your answer's functionality, but why have you setenabledelayedexpansion
when you don't use any!var!
expansions?
â AFH
12 hours ago
@AFH Force of habit. I've removed it to avoid any confusion:)
â DavidPostillâ¦
10 hours ago
add a comment |Â
It doesn't affect your answer's functionality, but why have you setenabledelayedexpansion
when you don't use any!var!
expansions?
â AFH
12 hours ago
@AFH Force of habit. I've removed it to avoid any confusion:)
â DavidPostillâ¦
10 hours ago
It doesn't affect your answer's functionality, but why have you set
enabledelayedexpansion
when you don't use any !var!
expansions?â AFH
12 hours ago
It doesn't affect your answer's functionality, but why have you set
enabledelayedexpansion
when you don't use any !var!
expansions?â AFH
12 hours ago
@AFH Force of habit. I've removed it to avoid any confusion:)
â DavidPostillâ¦
10 hours ago
@AFH Force of habit. I've removed it to avoid any confusion:)
â DavidPostillâ¦
10 hours ago
add a comment |Â
up vote
3
down vote
If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".
But the best option is to get the actual extension, regardless the length:
@echo off
setlocal
set "var1=file.Log"
set "var2=file.myLog"
:: Option 1 using FOR loops
for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
echo Match
) else (
echo No Match
)
: Option 2 using CALL parameters
call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
exit /b
:matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
if "%~x1"=="%~x2" exit /b 0
exit /b 1
add a comment |Â
up vote
3
down vote
If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".
But the best option is to get the actual extension, regardless the length:
@echo off
setlocal
set "var1=file.Log"
set "var2=file.myLog"
:: Option 1 using FOR loops
for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
echo Match
) else (
echo No Match
)
: Option 2 using CALL parameters
call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
exit /b
:matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
if "%~x1"=="%~x2" exit /b 0
exit /b 1
add a comment |Â
up vote
3
down vote
up vote
3
down vote
If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".
But the best option is to get the actual extension, regardless the length:
@echo off
setlocal
set "var1=file.Log"
set "var2=file.myLog"
:: Option 1 using FOR loops
for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
echo Match
) else (
echo No Match
)
: Option 2 using CALL parameters
call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
exit /b
:matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
if "%~x1"=="%~x2" exit /b 0
exit /b 1
If you are going to check a fixed number of characters, then you should check the last 4, else "file.Log" will match "file.myLog".
But the best option is to get the actual extension, regardless the length:
@echo off
setlocal
set "var1=file.Log"
set "var2=file.myLog"
:: Option 1 using FOR loops
for %%A in ("%var1%") do for %%B in ("%var2%") do if "%%~xA"=="%%~xB" (
echo Match
) else (
echo No Match
)
: Option 2 using CALL parameters
call :matchExtension "%var1%" "%var2%" &&echo Match||echo No Match
exit /b
:matchExtension file1 fil2 --> returns 0 (success) if match, 1 (error) if no match
if "%~x1"=="%~x2" exit /b 0
exit /b 1
answered 12 hours ago


dbenham
7,47631828
7,47631828
add a comment |Â
add a comment |Â
Tom is a new contributor. Be nice, and check out our Code of Conduct.
Tom is a new contributor. Be nice, and check out our Code of Conduct.
Tom is a new contributor. Be nice, and check out our Code of Conduct.
Tom is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1368721%2fhow-do-i-check-if-the-three-last-characters-of-a-variable-match-another-variable%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password