1

I have this bit of code where the string I want it to read is thisisaname:21841569874215468432 The thing is the part before the colon is variable. It changes length based on user input. So I found a function to get string length and a function to get the count of the characters before the colon. The next step in my plan is to skip past the thisisaname: to the first number in that sequence and record it. The bit of code that is supposed to do this is, SET PH1=!PH:~%len%,1! where it skips the length of the name and the colon to record the first variable. This is where my issue arises. It always gets the first letter of the string, not the number after the colon. It would seem that the variable tracking the character length is not being read correctly when I do a substring search. I also want to use the length for other substring searches which is why you see the len variable in the later for loops as well. Here is the code I'm referring to:

@ECHO off
SETLOCAL EnableDelayedExpansion
SET count=0
FOR /F %%a IN ('FINDSTR /C:":" saves.txt') DO (
    SET /a count=!count!+1
    SET PH=%%a
    ECHO !PH!
    FOR /f "delims=:" %%b IN ("!PH!") DO SET part=%%b
    ECHO !part!
    CALL :strLen len part
    ECHO !len!
    SET /A len=!len!+1
    ECHO !len!
    SET PH1=!PH:~%len%,1!
    ECHO !PH1!
    SET /A len=!len!+4
    ECHO !len!
    FOR /F "delims=" %%d IN ("!PH1!") do SET money=!PH:~%len%,%%d!
    SET PH2=!PH:~11,1!
    FOR /F "delims=" %%e IN ("!PH2!") do SET EXP=!PH:~12,%%e!
    ECHO ^|Save !count!: Money = !money! EXP = !EXP!
)
PAUSE
:strLen <resultVar> <stringVar>
(   
    SET "s=!%~2!#"
    SET "len=0"
    FOR %%P IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (
        IF "!s:~%%P,1!" NEQ "" ( 
            SET /a "len+=%%P"
            SET "s=!s:~%%P!"
        )
    )
)
( 
    SET "%~1=%len%"
    EXIT /b
)

I know this may seem a little confusing as to why I am doing this so I'm happy to clear anything up! Anything helps, and any advice too! Thanks!

EDIT Adding a token and delimiter to the first for loop makes this process a lot easier since it splits the string into two parts.

FOR /F "tokens=1* delims=:" %%a IN ('FINDSTR /C:":" saves.txt') DO (

This makes it so a lot of the issues that arise in the original code are not even present. I'm still curious why the original snippet doesn't work though, so I will leave that part be.

Pitdroid
  • 57
  • 5
  • 4
    You are using the for /f with the default parameters `"tokens=1delims= "`, it would be much easier if you'd use `"tokens=1* delims=:"` which splits the currently read line at the `:` into two parts, stored in the used for variable `%%a` and the following one `%%b`. Another point you should insert a `goto :eof`in front of the called sub to avoid running code flow through to the sub. –  Jul 06 '18 at 20:12
  • Your suggestion actually makes it a ton easier! Thank you! I will add an edited version with your suggestions to the snippet. – Pitdroid Jul 06 '18 at 20:18
  • In one of my answers to a previous question of yours, I showed you how to use FOR /F with the DELIMS option. – Squashman Jul 06 '18 at 22:04

1 Answers1

0

as you already know, you can use a for /f loop to split your string:

set "var=thisisaname:21841569874215468432"
for /f "tokens=1,2 delims=:" %%a in ("%var%") do 
  set "first=%%a"
  set "last=%%b"
)

or if you don't need the first part, you can use substring substituation with a wildcard:

set "var=thisisaname:21841569874215468432"
set "var=%var:*:=%
echo %var%

(Remember: inside a code block you have to replace %var% with !var!)

Note: Wildcard substituation works only to remove/replace the first part until (including) a given string. Removing or replacing another part isn't possible with wildcard (*)

Stephan
  • 53,940
  • 10
  • 58
  • 91