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

Page 1 of 3 123 LastLast

  1. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #1

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


    I am trying to find ALL the UNIQUE values in a .TXT file [ C:\Users\System-Admin\Desktop\Test_File.txt ] that are between TWO Percentage Signs [ % ]. Basically, there are MANY lines in a file in the format %nnn% for example, MANY of which are duplicates. I would like to output just the UNIQUE ones with the Percentage Sign [ % ] included at the beginning and the end, which is exactly how they appear in the file.

    I do NOT want the whole lines just the Percentage Signs [ % ] around what is inside them.

    This produces DUPLICATES of EVERYTHING including the WHOLE line that has the criteria I want . . .

    Code:
    
    findstr /i "%*%" C:\Users\System-Admin\Desktop\Master.bat

    Some of what I have tried using is for /f "tokens=1 delims=," %i in . . . etc, Find and FindSTR but without much success.

    Thanks.
    Last edited by Paul Black; 19 Sep 2021 at 16:24.
      My Computer


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

    I have had a re-think on this.

    My main objective is to extract ALL the Variables used in a Batch file[s] that I specify. Then I want to Amend those Variables.

    I got a LOT of excellent help in another thread from das10 with regard to PowerShell, which has inspired me [ if that is the correct phrase ] to try out a PowerShell option.

    I have setup and tested a PowerShell Script that will make the required changes [ below as an example ] once I have EXTRACTED ALL the Variables . . .

    Code:
    
    $Original_File = 'C:\Users\System-Admin\Desktop\Master.bat'
    $Destination_File = 'C:\Users\System-Admin\Desktop\Replaced_Output.bat'
    
    (Get-Content $Original_File) | ForEach-Object {
        $_ -replace ' %ENE% ', ' "%ENE%" ' `
           -replace ' %LOG% ', ' "%LOG%" ' `
           -replace ' %OFN% ', ' "%OFN%" ' `
           -replace ' %POW% ', ' "%POW%" ' `
           -replace ' %SLE% ', ' "%SLE%" ' `
        } | Set-Content $Destination_File

    With this in mind, I am going to try and EXTRACT ALL the Variables needed that will be used in the above using PowerShell. I want to keep the two files separate as I think this will be easier to test and apply.

    NO luck so far.
    Last edited by Paul Black; 19 Sep 2021 at 15:46.
      My Computer


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

    OK, I have sort of got something usable . . .

    Code:
    
    $Original_File = Get-Content 'C:\Users\System-Admin\Desktop\Master.bat'
    $Destination_File = 'C:\Users\System-Admin\Desktop\Extracted_Output.txt'
    
    ForEach ($Line In $Original_File) { 
      If ($Line -Like "*%???%*") {
        $Line | Out-File -FilePath $Destination_File -Append
      }
    }

    . . . other than I have WHOLE lines and DUPLICATES, the same as in Post #1.

    If I remove the *'s, I only end up with ONE entry.
    Last edited by Paul Black; 19 Sep 2021 at 16:45.
      My Computer


  4. Posts : 456
    Windows 10
       #4

    Can you tell what you expect as result? A file like: %var1% %var2% %var3% %var4% and non should be repeated? And the variables that use DelayedExpansion too? !var1! !var2! !var3! !var4!
      My Computer


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

    Hello @ricardobohner.

    Thanks for the reply, it is appreciated.

    ricardobohner said:
    Can you tell what you expect as result?
    A file like: %var1% %var2% %var3% %var4% and none should be repeated?
    And the variables that use DelayedExpansion too? !var1! !var2! !var3! !var4!
    Yes, that is exactly what I would like in a list.
      My Computer


  6. Posts : 456
    Windows 10
       #6

    What is the propose of this batch file? Drag the batch file to this batch file. It should create a new file called Batchname_Variables.txt with the variables inside...

    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%"
    for /f "delims=" %%a in ('type "%File%" ^|findstr /i "%%.*%%" ^|find /v "%%%%" ^|find /v "%%~1"') do for /f tokens^=2^ delims^=%% %%b in ("%%a") do find "%%%%b%%" "%Result%" 1> nul 2> nul || echo/ |set /p=%%%%b%% >> "%Result%"
    echo.>>"%Result%"
    echo/|set /p=Delayed Expansion: >>"%Result%"
    for /f "delims=" %%a in ('type "%File%" ^|findstr /i "!.*!"') do for /f tokens^=2^ delims^=! %%b in ("%%a") do find "!%%b!" "%Result%" 1> nul 2> nul || echo/ |set /p=!%%b! >> "%Result%"
    echo.
      My Computer


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

    Hello @ricardobohner.

    Thank you for your time and effort to put that together for me, it is appreciated.

    Everything works EXCEPT this line in the .BAT file . . .

    Code:
    cd /d "%~dp0" && ( if exist "%Temp%\getadmin.vbs" del "%Temp%\getadmin.vbs") && fsutil dirty query %systemdrive% 1>nul 2>nul || ( echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%Temp%\getadmin.vbs" && "%Temp%\getadmin.vbs" && Exit /b)

    Which outputs %~dp0" && ( if exist "% instead of %Temp%.

    Also, %params% and %systemdrive% does NOT show in the OUTPUT anywhere.

    This is NOT a problem as I know which block they are in and they are ALWAYS at the top of the code.

      My Computer


  8. Posts : 456
    Windows 10
       #8

    I think it does what it's supposed to do here "%~dp0"&&(if exist "% get text between %...I'm going to try to do an exclusion for %~dp. Also I noticed a but bug where the batch gets at most 1 variable for each line....fixing soon
      My Computer


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

    ricardobohner said:
    I think it does what it's supposed to do here "%~dp0" && get text between %...I'm going to try to do an exclusion for %~dp. Also I noticed a but bug where the batch gets at most 1 variable for each line....fixing soon.
    Thank you.
      My Computer


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

    Hello @ricardobohner.

    ricardobohner said:
    I think it does what it's supposed to do here "%~dp0" && get text between %...I'm going to try to do an exclusion for %~dp. Also I noticed a but bug where the batch gets at most 1 variable for each line....fixing soon.

    Did you have ANY luck with this?
      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 14:53.
Find Us




Windows 10 Forums