0

I come to find some guidance on accomplishing the following:

I have a variable with content like this:

varname = asdfiuytgy12$gggsy22.oihbcxew

or

varname = oiujedc$thisisit.oiju

which $ and . are exactly my partters and I need to get what is within them so gggsy22 or thisisit.

I need to use batch to create a simple bat file. I hope someone can provide some guidance.

Edit - (from comment section)

Actually a friend of mine helped and it did work but with a quite amount of lines:

Set "sstr=$"
SET stemp=%nameVar%&SET pos=0

:loop
SET /a pos+=1
echo %stemp%|FINDSTR /b /c:"%sstr%" >NUL
IF ERRORLEVEL 1 (
    SET stemp=%stemp:~1%
    IF DEFINED stemp GOTO loop
    SET pos=0
)
Set "pos1=%pos%"
Set "sstr=."
SET stemp=%nameVar%&SET pos=0

:loop
SET /a pos+=1
echo %stemp%|FINDSTR /b /c:"%sstr%" >NUL
IF ERRORLEVEL 1 (
    SET stemp=%stemp:~1%
    IF DEFINED stemp GOTO loop
    SET pos=0
)
Set "pos2=%pos%"
set /a "pos2=%pos2%-%pos1%-1"
call set env=%%nameVar:~%pos1%,%pos2%%%
6
  • 1
    The Windows command prompt is NOT a DOS prompt! Commented Jul 4, 2017 at 16:49
  • What have you tried so far, where are you stuck? Type set /? and for /? into a command prompt window and read the help texts very carefully; I'm sure you'll find something helpful... Commented Jul 4, 2017 at 16:50
  • Actually a friend of mine helped and it did work but with a quite amount of lines: Commented Jul 4, 2017 at 19:18
  • 2
    Please include the code in your question as it is not readable in a comment... Commented Jul 4, 2017 at 21:09
  • 1
    That code in your Edit cannot possibly work. The logic is wrong, and your use of duplicate :label names will result in unwanted loop cross pollination. Commented Jul 5, 2017 at 15:22

2 Answers 2

3
@echo off
set "varname=asdfiuytgy12$gggsy22.oihbcxew"
for /f "tokens=2 delims=$." %%a in ("%varname%") do set "sub=%%a"
Sign up to request clarification or add additional context in comments.

2 Comments

This would give an erroneous result with a string like abc$123$xyz or abc.123.xyz or abc$123 or abc.123.
This would also fail with a string like abc$.xyz
1

The following works in nearly any situation. The only thing that could break the code is if the string contains a quote " followed by a poison character like &, |, etc.

@echo off
setlocal
set "str=oiujedc$thisisit.oiju"

:: Verify string exists and has the proper format
echo "%str%"|findstr "\$.*\." >nul || (echo Value not found & exit /b)

:: Extract the value
:: The extra "x" is needed in case there is no character between $ and .,
:: in which case the result should be No Value (result variable not defined)
for /f "delims=." %%A in ("x%str:*$=%") do set "val=%%A"
set "val=%val:~1%"

:: Show the result
echo value = "%val%"

A bullet proof variant can be made by incorporating delayed expansion.

4 Comments

Wouldn't the first check be more exact/easier with findstr "\$..*\." >nul or findstr "\$[^$.][^$.]*\." >nul ?
Your comment seems off track as this is about strings with $ and . not files with an underscore. Your above code will return wrong results with (of course construed) strings like "oiujedc$a$thisisit.oiju" , "oiujedc$.a$thisisit..oiju" or "oiujedc$$thisisit..oiju"
@LotPings - Sorry, my head was in the wrong (totally unrelated) Q&A when I wrote my (deleted) comment. I'm back on topic :-) I am assuming that an empty string from xxx$.xxxx is a valid result, but a source string without $ and or . is invalid, which is why I did not use your first regex. I wanted to be able to differentiate between the two scenarios. If no need to differentiate, then i like your first regex. As to whether my results are wrong with your construed strings, that is debatable. The OP did not specify how to handle multiple $ and/or ..
@LotPings - My code consistently handles multiple $ or . in a reliable way. I begin extracting immediately after the first occurrence of $, and stop at the next occurrence of .. My code allows $ to be in the resultant value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.