CMD Prompt / PS / Batch - Find Unique Values in a .TXT File

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 456
    Windows 10
       #11

    Yep I got it a bit better now but it still dosn't get all the variables on some batch that I tested...
      My Computer


  2. Posts : 456
    Windows 10
       #12

    Test this one it's better than the one I made before but still not perfect, get's up to 7 variables on each line. If you see Wolfsblut in the end the batch made it to the end and did not get broken somewhere...

    Code:
    @echo off
    chcp 1252 > Nul
    if exist "%~1" set "File=%~1"
    
    set Result=%~n1_Variables.txt
    
    if exist "%Result%" del /q "%Result%"
    echo/|set /p=Normal           : >>"%Result%"
    set VT=N
    for /f "delims=" %%a in ('type "%File%" ^|findstr /i "%%.*%%"') do for /f tokens^=2^,^3^,^4^,^5^,^6^,^7^,^8^,^9^,^10^,^11^,^12^,^13^,^14^,^15^,^16^ delims^=%% %%b in ("%%a") do call :Normal "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j" "%%k" "%%l" "%%m" "%%n" "%%o" "%%p"
    echo.>>"%Result%"
    
    echo/|set /p=Delayed Expansion: >>"%Result%"
    set VT=E
    for /f "delims=" %%a in ('type "%File%" ^|findstr /i "!.*!"') do for /f tokens^=2^,^3^,^4^,^5^,^6^,^7^,^8^,^9^,^10^,^11^,^12^,^13^,^14^,^15^,^16^ delims^=! %%b in ("%%a") do Call :Normal "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j" "%%k" "%%l" "%%m" "%%n" "%%o" "%%p"
    
    echo Wolfsblut
    pause
    exit
    
    :Normal
    set "Var=%~1"
    set Contador=0
    :Repeat
    set /a Repe+=1
    IF not "%~1"==" " IF not "%~1"=="" (
                                        set /a Contador+=1
                                        call :FindVar%VT% "%~1"
                                       )
    shift
    If not %Repe% GEQ 14 goto :Repeat
    goto :EOF
    pause
    
    :FindVarN
    echo "%~1" |findstr "\\ / ~ @ ( ) : \ & | - dp0 dpnx" 1> nul 2>nul || find "%~1" "%Result%" 1> nul 2> nul || echo/ |set /p=%%%~1%% >> "%Result%"
    goto :EOF
    
    :FindVarE
    echo "%~1" |findstr "\\ / ~ @ ( ) : \ & | - dp0 dpnx" 1> nul 2>nul|| find "%~1" "%Result%" 1> nul 2> nul || echo/ |set /p=!%~1! >> "%Result%"
    goto :EOF
      My Computer


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #13

    Hello @ricardobohner,

    Thank you, but unfortunately, it only produced the following . . .

    Code:
    
    Normal           : %Title% %Author% Sets the default console foreground and background colors.
    
    COLOR [attr]
    
      attr        Specifies color attribute of console output
    
    Color attributes are specified by TWO hex digits -- the first
    corresponds to the background; the second the foreground.  Each digit
    can be any of the following values:
    
        0 = Black       8 = Gray
        1 = Blue        9 = Light Blue
        2 = Green       A = Light Green
        3 = Aqua        B = Light Aqua
        4 = Red         C = Light Red
        5 = Purple      D = Light Purple
        6 = Yellow      E = Light Yellow
        7 = White       F = Bright White
    
    If no argument is given, this command restores the color to what it was
    when CMD.EXE started.  This value either comes from the current console
    window, the /T command line switch or from the DefaultColor registry
    value.
    
    The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
    the COLOR command with a foreground and background color that are the
    same.
    
    Example: "COLOR fc" produces light red on bright white
    

    . . . which is the first TWO variable on the first line that had Variables.
      My Computer


  4. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #14

    Hello @ricardobohner,

    I have been trying to achieve this using PowerShell [ I have spent hours on it ] as you are having a go using Batch. I do NOT know PowerShell, but I thought I would have a go along with my friend Google. Unfortunately, I am NOT having much success [ had some though ] with it, YET.

      My Computer


  5. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #15

    UPDATE:

    I have cracked it using PowerShell, although a brute force approach . . .

    Code:
    
    $Source_File = Get-Content 'C:\Users\System-Admin\Desktop\System_Reports.bat'
    $Destination_File = 'C:\Users\System-Admin\Desktop\AAA.txt'
    
    if (Test-Path $Destination_File) 
    {
      Remove-Item $Destination_File
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%??%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%???%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%?????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%??????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%???????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%?????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%??????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%???????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%?????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%??????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%???????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%????????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%????????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%?????????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%??????????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    ForEach ($Line In $Source_File) { 
      If ($Line -Like "%???????????????????%") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }
    
    $Source_File = "C:\Users\System-Admin\Desktop\AAA.txt"
    $Destination_File = 'C:\Users\System-Admin\Desktop\BBB.txt'
    
    if (Test-Path $Destination_File) 
    {
      Remove-Item $Destination_File
    }
    
    $Unique_Values = $( ForEach ($Line in Get-Content $Source_File) {
      $Line.Split(" ")
    }) | Sort-Object | Get-Unique
    
    $Unique_Values | Out-File $Destination_File -Append
    $Unique_Values.Count | Out-File $Destination_File -Append

    • It EXTRACTS ALL the Variables ONLY into AAA.txt.
    • It then SORTS that file ALPHABETICALLY and creates BBB.txt which also includes the Total Unique Variables.

    I will spend some time tidying up the code tomorrow if I get time.

      My Computer


  6. Posts : 456
    Windows 10
       #16

    Cool, happy you made it. What do you need the variables of the batch file for?
      My Computer


  7. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #17

    Hello @ricardobohner,

    ricardobohner said:
    Cool, happy you made it. What do you need the variables of the batch file for?
    First of all, THANK YOU for your time and effort on trying to resolve this, it is appreciated.



    It is mainly for restructuring the Variables if needed. I have already written the Script for that and tested it anyway.

    Thanks again.
      My Computer


  8. Posts : 3,453
       #18

    Not sure exactly the format you have - but suggest using regex to get string patterns

    https://docs.microsoft.com/en-us/pow...powershell-7.1
      My Computer


  9. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
    Thread Starter
       #19

    Thanks for the reply @Superfly,

    Superfly said:
    Not sure exactly the format you have - but suggest using regex to get string patterns.

    Funny you should say that, because that is what I initially tried using. I am NOT up with PowerShell [ too long in the tooth now ], so I adopted an alternative brute force approach to get the data that I wanted.

      My Computer


  10. Posts : 3,274
    Win10
       #20

    Paul, this is a simple PS script to get all variables within two % signs, provided they contain no spaces or other symbols; this is just to see if others can try and make it more viable for your use case.

    Code:
    $Sourcefile = 'C:\test2\testfile.txt'
    $findthis='\%~{0,1}\w{1,18}~{0,1}\%'
    $findalso='\!~{0,1}\w{1,18}~{0,1}\!'
    
    Select-String -Path $Sourcefile -Pattern $findthis -AllMatches | % { $_.Matches } | % { $_.Value } | Sort | Unique | Out-file -FilePath 'C:\test2\RESULTtestfile.txt'
    
    Select-String -Path $Sourcefile -Pattern $findalso -AllMatches | % { $_.Matches } | % { $_.Value } | Sort | Unique | Out-file -FilePath 'C:\test2\RESULTtestfile.txt' -Append
    The Regex pattern should capture patterns only for upto 18 alphanumeric characters as in your post, such as
    %abc%
    %~1abc%
    %123456789012345678%
    !~2a!
    etc
    Hopefully more knowledgeable members may refine it.
      My Computers


 

  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 15:08.
Find Us




Windows 10 Forums