Cannot find matching string in text file with for /f


  1. Posts : 425
    Windows 10
       #1

    Cannot find matching string in text file with for /f


    This line is part of an 11,000 line script. It fails to load the matching text from the file

    Code:
    for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="%USERNAME%" set GamePath=%%b
    This one works;

    Code:
    for /F "Usebackq Tokens=1* delims==" %%a in ("%SRC%\PostInstall\%tFile%") do IF /I "%%a"=="%USERNAME%" set uWord=%%b
    The delimiter is not the issue as I've tried both , and =.

    I have literally hundreds of these and they all work;

    Code:
    for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="Hoyle Card Games" set GamePath=%%b
    for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="Hearthstone" set GamePath=%%b
    For the life of me I can't see why the first one won't work but hundreds of others do.
    I thought it might be because I was comparing two variables, but as I show above, the one setting uWord works.

    The line being searched for exists in folders.txt and the user name is spelled correctly.

    Code:
    Among us,D:\STEAM\steamapps\common\Among Us
    Barotrauma,D:\STEAM\steamapps\common\Barotrauma
    BloonsTD6,D:\STEAM\steamapps\common\BloonsTD6
    Cinderella Phenomonen,D:\STEAM\steamapps\common\Cinderella Phenomenon - OtomeVisual Novel
    Divinity Original Sin 2,D:\STEAM\steamapps\common\Divinity Original Sin 2
    FINAL FANTASY XIV ONLINE,D:\GAMES\FF14 2\SquareEnix
    Hakuoki Edo Blossoms,D:\STEAM\steamapps\common\Hakuoki Edo Blossoms
    Hakuoki Kyoto Winds,D:\STEAM\steamapps\common\Hakuoki Kyoto Winds
    Nightshade,D:\STEAM\steamapps\common\Nightshade
    Origin,	C:\Program Files (x86)\Origin
    SpeedRunners,D:\STEAM\steamapps\common\SpeedRunners
    Stardew Valley,D:\STEAM\steamapps\common\Stardew Valley
    The Sims 4,D:\Program Files (x86)\Origin Games\The Sims 4
    The Sims 4 Snowy Escape,D:\Program Files (x86)\Origin Games\The Sims 4
    Ubisoft,C:\Program Files (x86)\Ubisoft\Ubisoft Game Launcher
    CMTools,D:\CMTools
    FFBard,D:\FFBardMusicPlayer_1.4
    Nexus,C:\Program Files\NMM
    Gabrielle,XXXXX-XXXXX-XXXXX-XXXXX
    It's the last line

    Anyone see what's wrong with the first for command above?
    %srcpath% = I:\PostInstall\Gabrielle
    I don't believe this is a delayedexpansion issue as this line is not within and IF (..) block.
    It works fine from a command line (substituting %% for % of course).
      My Computer


  2. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    I assume you've already checked your username using
    Code:
    echo %UserName%
    Try using that command*** in a batch file of its own with a pause at the end so you can see what it is reading and deciding.
    {*** Include setting the SrcPath variable}
    Code:
    Set SrcPath=Whatever
    echo Stage 1
    for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do echo "%%a"-"%%b"
    Pause
    echo Stage 2
    for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do IF /I "%%a"=="%USERNAME%" echo "%%b"
    Pause



    Hold on, you are only retrieving the first token. There is no %b. I don't see how it was working in a command line.
    Is there just a repeated typo in your post about the tokens or have I gone loopy?


    Best of luck,
    Denis
      My Computer


  3. Posts : 425
    Windows 10
    Thread Starter
       #3

    Thanks for your reply.

    %USERNAME% is a system environment variable created by the OS. Yes, it is present and used in ltterally hundreds of places in the script.

    CORRECTION:
    Tokens=1* means the first and everything thereafter. I could have used Tokens=1-2. Just habit using 1*
    So if the file was George,Paul,Ringo,John
    1* would return %%a=George, %%b=Paul
    %%c=Ringo and %%d=John would not be returned because 1* stops tokenization after the first token.
    Using tokens=* returns all four.
    At least that's how I interpret it from here; windows - Batch file FOR /f tokens - Stack Overflow

    So, sorry, no typo.

    The script actually writes a bunch of stuff to a log file at the start, which shows the values of many variables. Here's a snippet;

    Code:
     Echo %time% PostInstall installation script starting: Version %sVersion% %sDate% >>%LogFile% 2>&1
        Echo %time% Running for user: %UserName% >>%LogFile% 2>&1
        ECho %time% Running script %~fn0 >>%LogFile% 2>&1
        Echo %time% Windows version is 10.0 - %WinVer% >>%LogFile% 2>&1
    	Echo %time% Windows product key: %prodkey% >>%LogFile% 2>&1
        Echo %time% Current system name: %COMPUTERNAME%. Will be renamed to %USERNAME%-PC >>%LogFile% 2>&1
        Echo %time% Default temp paths: Temp: %temp%  TMP: %tmp% >>%LogFile% 2>&1
        Echo %time% AppData is at %AppData% >>%LogFile% 2>&1
        Echo %time% Program files: %ProgramFiles% >>%LogFile% 2>&1
        Echo %time% Program Files (X86): %PROGRAMFILES(X86)% >>%LogFile% 2>&1
        Echo %time% Users SID: %MySiD% >>%LogFile% 2>&1
        Echo %time% CPU ID: %CPUID% >>%LogFile% 2>&1
    As you can see, %username% is one of the variables written to the log file, so I knows it's present and correct.

    As expected, echo "%%b" returns "".

    This is a good idea...
    for /F "Usebackq Tokens=1* delims=," %%a in ("%SrcPath%\folders.txt") do echo "%%a"-"%%b"
    This way I will see every line in the file.

    I'll let you know what that shows.
    Last edited by Wobitancrawfodi; 11 Dec 2022 at 20:32.
      My Computer


  4. Posts : 16,950
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    I didn't see the *
    I only saw the Tokens=1
    Sorry.

    I never use Tokens=1*
    If I wanted that I would use Tokens=* and I don't know what Tokens=1* would achieve.


    Denis
    Last edited by Try3; 11 Dec 2022 at 12:29.
      My Computer


  5. Posts : 425
    Windows 10
    Thread Starter
       #5

    Try3 said:

    I never use Tokens=1*
    If I wanted that I would use Tokens=* and I don't know what Tokens=1* would achieve.
    Using tokens=* will split the line after the delimiter is encountered and tokenization continues.
    using tokens=1* splits the line after the delimiter is encountered and stops tokenization after the first token.

    Well, I set up a separate script that echo'd each line, and I set one up with the "%%a"=="%USERNAME%" comparison and they both work. Damn it!

    It's only when it's in my main script it doesn't work.
    This one's got me beat.
    Thanks for your assistance.
      My Computer


  6. Posts : 776
    Windows 7
       #6

    When I have parsing issues, in most cases it's not a program error -- but making incorrect assumptions of the source data.

    For example -- have you noticed "Origin, C:\Program Files (x86)\Origin" is the ONLY entry with a space after the comma separator? What makes that line unique?

    Unless you go over the data chain, you can't figure out how these errors get introduced. Are there special characters, different quoting for paths with spaces in them?
      My Computer


  7. Posts : 425
    Windows 10
    Thread Starter
       #7

    garlin said:
    When I have parsing issues, in most cases it's not a program error -- but making incorrect assumptions of the source data.

    For example -- have you noticed "Origin, C:\Program Files (x86)\Origin" is the ONLY entry with a space after the comma separator? What makes that line unique?

    Unless you go over the data chain, you can't figure out how these errors get introduced. Are there special characters, different quoting for paths with spaces in them?
    Nothing. It was a mistake on my part which has since been corrected.

    Programs/games that install on the C:\ drive don't have the token checked for validity, since the path cannot possibly exist at this point in time. If I got a match for this one then the install will be attempted.

    If the path is not C:\.... then the full path is verified as it should already exist (only C:\ drive is formatted for the Windows install).

    For MalwareBytes, the string being captured is the only one with hyphens in it (eg -). So I took them out but that didn't fix the issue, not that I would have expected it to. I had to mask it when posting here as it is a license key.

    The code used is identical to hundreds of others. If I was a good programmer I would have made the whole thing a single subroutine, but I'm just an average programmer.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 07:22.
Find Us




Windows 10 Forums