T3RRYT3RR0R

Batch String and Integer comparison macro

Sep 22nd, 2020
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.16 KB | None | 0 0
  1. @Echo Off & Setlocal DISABLEdelayedexpansion
  2. rem // Pads string with 125 characters to prevent false results when string lengths differ. If comparing strings exceeding 125 characters in length,
  3. rem // increase length of the padding and adjust s1 and s2 Substring modification length to match the number of padding characters.
  4.  Set "PAD=00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  5.  Set "Compare=Set "$RV="&For %%n in (1 2)Do if %%n==2 (For /F "Tokens=1,2 Delims={}" %%G in ("!Strings!")Do (Set "s1=!PAD!%%~G"&Set "s2=!PAD!%%~H"&Set "s1=!s1:~-125!"&Set "s2=!s2:~-125!"&(If "!s1!" LSS "!s2!" (Set "$RV=LSS") Else If "!s1!" EQU "!s2!" (Set "$RV=EQU") Else If "!s1!" GTR "!s2!" (Set "$RV=GTR"))&Echo/%%~G !$RV! %%~H&Set "s1="&Set "s2=")) Else Set Strings="
  6.  Setlocal ENABLEdelayedexpansion
  7. rem // example usages
  8. For %%A in (one two three)Do for %%B in (three two one)Do  %Compare:$RV=Ex[0]%{%%A}{%%B}
  9.  %Compare:$RV=Ex[1]%{one}{oneb}
  10.  %Compare:$RV=Ex[2]%{one}{one}
  11.  %Compare:$RV=Ex[3]%{oneb}{one}
  12.  %Compare:$RV=Ex[4]%{13}{2}
  13.  %Compare:$RV=Ex[5]%{2}{13}
  14.  Set Ex[
  15. Endlocal & Endlocal & Goto :Eof
  16.  
Advertisement
Add Comment
Please, Sign In to add comment