1.

Solve : Batch program: Compare substring of argument with another string?

Answer»

In my batch program I WOULD like to compare two strings.

I have used the below code
SET VAR=%~1
SET var2=%var:~-1%
and then compare using IF %var2%="d" (echo. Matched record)

I would like to simplify this and have this in a single step as below.

IF %~1:~-1%="d" (echo. Matched record)

but this doesnt work

I want to compare the substring of 1ST argument with the string.

Could some one let me know where am GOING wrong?
1. You need double equal signs.

2. Quotes are part of the comparison so you if you have them on one side you need them on the other.

3. The notation for substrings is for variables e.g. %var% not parameter variables e.g. %1, so you cannot "simplify" as you suggest. You must copy a parameter into a variable before you can extract substrings.

Code: [Select]if "%var2%"=="d" (echo. Matched record)









Discussion

No Comment Found