Batch to read out strings from txt with special characters

62 views Asked by At

I am trying to get all known WiFi SSIDs including the Password.

When I use the following script, I can't get it to work to set the SSIDs in doublequotes, (so blanks and so on are correctly used).

Also when I've got special characters like exclamation marks, they dissapear.

Can someone please help me?

@ECHO OFF
setlocal EnableExtensions DisableDelayedExpansion
color 02
netsh wlan show profiles | findstr /R /C:"[ ]:[ ]" > temp.txt
echo @echo off >> helper.bat
echo setlocal enabledelayedexpansion >> helper.bat
echo for /f "tokens=5*" %%%%i in (temp.txt) do ( set val=%%%%i %%%%j >> helper.bat
echo if "!val:~-1!" == " " set val=!val:~2,-1! >> helper.bat
echo echo !val! ^>^> final.txt) >> helper.bat
echo for /f "tokens=*" %%%%i in (final.txt) do @echo SSID: %%%%i ^>^> others_%computername%.txt ^& echo # ================================================ ^>^> others_%computername%.txt ^& netsh wlan show profiles name=%%%%i key=clear ^| findstr /N /R /C:"[ ]:[ ]" ^| findstr 33 ^>^> others_%computername%.txt ^& echo # ================================================ ^>^> others_%computername%.txt ^& echo # Key content is the password of your target SSID. ^>^> others_%computername%.txt ^& echo # ================================================ ^>^> others_%computername%.txt >> helper.bat
REM echo del /f /q temp.txt final.txt >> helper.bat
echo exit >> helper.bat
start "" /wait helper.bat
exit

Output is for example: Original (temp.txt):

Profil fr alle Benutzer : 1FRITZ!Box Fon WLAN 7390 

Output in final.txt:

1FRITZBox Fon WLAN 7390 

What it should look like in final.txt:

"1FRITZ!Box Fon WLAN 7390"
1

There are 1 answers

0
Compo On

Here is your original code complete with unnecessary things removed, fixes, and improvements. It no longer writes additional files, or defines variables. This should mean that echoing of some characters won't affect your output, and therefore doublequoting the passphrase is unnecessary.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
(
    For /F "Tokens=4,*" %%G In (
        '%SystemRoot%\System32\netsh.exe WLAN Show Profiles
         ^| %SystemRoot%\System32\findstr.exe /R "\<:\>"'
    ) Do (
        Echo Profile: %%H
        Echo # ======================================================
        %SystemRoot%\System32\netsh.exe WLAN Show Profiles Name="%%H" Key=Clear^
         | %SystemRoot%\System32\findstr.exe /N /R "\<:\>"^
         | %SystemRoot%\System32\findstr.exe /R "^33:"
        Echo # ======================================================
        Echo # Key content is the passphrase for your target Profile.
        Echo # ======================================================
    )
) 1>"others_%ComputerName%.txt"

Please note, I do not recommend that others should use this methodology, as proof would be needed that netsh.exe will always output the target string, on the same line on all systems.