Home > Mobile >  I am trying to filter a text file based on a var in a text file (batch)
I am trying to filter a text file based on a var in a text file (batch)

Time:08-15

For example in my file (loginusers.txt)
if string in my file between { and } is
"MostRecent" "1"
then grab the string "76561199048621574" outside of the brackets and assign to a variable without quotes
and ignore entire other {} if "MostRecent" is "0"

"users"
{
    "76561199048621574"
    {
        "AccountName"       "acc1_internalname"
        "PersonaName"       "acc1_displayname"
        "RememberPassword"      "1"
        "WantsOfflineMode"      "0"
        "SkipOfflineModeWarning"        "0"
        "AllowAutoLogin"        "1"
        "MostRecent"        "1"
        "Timestamp"     "1660477659"
    }
    "76561198832716662"
    {
        "AccountName"       "acc2_internalname"
        "PersonaName"       "acc2_displayname"
        "RememberPassword"      "0"
        "WantsOfflineMode"      "0"
        "SkipOfflineModeWarning"        "0"
        "AllowAutoLogin"        "0"
        "MostRecent"        "0"
        "Timestamp"     "1650761551"
    }
}

CodePudding user response:

Something like this would probably work for you:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "vdf=%ProgramFiles(x86)%\Steam\config\loginusers.vdf"

Set "usr="
For /F "Tokens=1,2" %%G In ('%SystemRoot%\System32\findstr.exe /R
 "\<\"[0123456789][0123456789]*\"\>" "%vdf%" 2^>NUL') Do (
    If Not Defined usr If "%%~H" == "" Set "usr=%%~G"
    If Defined usr If /I "%%~G" == "MostRecent" If "%%~H" == "1" (GoTo :Next
    ) Else Set "usr=")

:Next
If Not Defined usr GoTo :EOF
Echo %%usr%% = %usr%
Pause

However, as this site is not technically a code request and receive service, I will not be explaining any of the above, or modifying it for additional or modified requirements moving forward. Please study the code yourself to determine what it is doing and how. Thank you.

CodePudding user response:

@ECHO Off
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q73352699.txt"

SET "data="

FOR /f "usebackqtokens=1,2" %%b IN ("%filename1%") DO (
 IF "%%~c"=="" (
  IF %%b=="%%~b" (SET "data=%%~b") ELSE (IF "%%b"=="}" SET "data=")
 ) ELSE (
  IF "%%~b"=="MostRecent" IF "%%~c"=="1" GOTO done
 )
)

:done
IF DEFINED data (ECHO Required data:            
  • Related