Hi, I have log files I'd like to rename using the ProductKeyID which is found in each file. The Log file look like this: Code: <HardwareVerificationReport version="1.0"> <Result> <p n="Pass">true</p> </Result> <Key inputId="1"> <ProductKeyID>2586002108880</ProductKeyID> <ProductKeyState>0</ProductKeyState> </Key> </HardwareVerificationReport> All I need is to retrieve the value in blue. I think the fact there's <>/" characters is making it difficult to use find or findstr. Here's some garbage I've tried: Code: ::for /f "tokens=4 delims==;" %%a in ( :: 'find "ProductKeyID" ^< X_LOG.txt' :: ) do set "ProductKeyID=%%a" ::FINDSTR "ProductKeyID" X_LOG.txt ::del Test2.txt ::type X_LOG.txt | findstr /I /C:"ProductKeyID" FOR /F "tokens=2 delims=: " %%A in ('type X_LOG.txt ^| findstr /i "ProductKeyID"') do echo %%A Any help would be fantastic!
Will this do? Code: @echo off pushd "%~dp0" :-------------------------------------- FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (set KEY=%%A) echo %KEY% pause Code: 2586002108880 Press any key to continue . . .
Yes, Perfect! I have some bigger logs than the sample I posted & noticed a few more instances of ProductKeyID. It still gave me the correct result! I'm pretty new to the Tokens command. This helps me out heaps. Thanks very much!
But this syntax displays the last detected one only if there are more. For example, if you have two products installed, there should be two ProductKeyIDs like 1111111111111 and 2222222222222. TXT Code: <HardwareVerificationReport version="1.0"> <Result> <p n="Pass">true</p> </Result> <Key inputId="1"> <ProductKeyID>1111111111111</ProductKeyID> <ProductKeyState>0</ProductKeyState> </Key> <Result> <p n="Pass">true</p> </Result> <Key inputId="1"> <ProductKeyID>2222222222222</ProductKeyID> <ProductKeyState>0</ProductKeyState> </Key> </HardwareVerificationReport> COMMAND Code: FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (set KEY=%%A) echo %KEY% pause RESULT Code: 2222222222222 Press any key to continue . . . How to display all of them? COMMAND Code: @echo off pushd "%~dp0" :-------------------------------------- FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (echo %%A) pause RESULT Code: 1111111111111 2222222222222 Press any key to continue . . . Cheers
Thanks but I realised that last night when I changed it to: Code: FOR /F "tokens=3 delims=<>" %%A in (' "type X_LOG.txt | findstr /i ProductKeyID" ') do (echo %%A) Code: true 2586002108880 2586002108880 Press any key to continue . . . I've checked & all the Log files I'll scan will give me the correct result. Each file will have it's own unique ID. Thanks again buddy!