Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#1
Post
by tinfanide » 28 Feb 2012 23:09
test.txt
[UserData]
FullName = "Your User Name"
OrgName = "Your Organization Name"
ComputerName = *
ProductKey= "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX"
How could I return a specific string position in a text file?
I know there's string manipulation commands (e.g. Left, Mid...)
But I want to search for a string and return its position.
Thanks in advance.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 29 Feb 2012 02:43
Tell us exactly what you want to do and you will get an appropriate answer.
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#3
Post
by tinfanide » 29 Feb 2012 04:11
Code: Select all
@ECHO OFF
FOR /F "tokens=*" %%C IN (test.txt) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
SET "old=%%C"
:: ProductKey= (the equal sign "=" is not escaped)
SET new=!old:ProductKey==!
IF DEFINED new (
:: ECHO) = ECHO.
ECHO.!new!
)
ENDLOCAL
)
PAUSE
Originally I wanted to extract the product key.
I thought of finding the exact position to extract the key but I was also thinking of using the string substitution method.
But I'm stuck at how to escape the equal sign.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 29 Feb 2012 04:21
Try this:
Code: Select all
@echo off
for /f "tokens=2 delims==" %%a in ('type "test.txt" ^|find "ProductKey="') do set PK=%%a
echo %PK%
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#5
Post
by tinfanide » 02 Mar 2012 10:35
foxidrive wrote:Try this:
Code: Select all
@echo off
for /f "tokens=2 delims==" %%a in ('type "test.txt" ^|find "ProductKey="') do set PK=%%a
echo %PK%
Yes, this works. Thanks.