Home > database >  batch script that writes another batch script having for loop in its code
batch script that writes another batch script having for loop in its code

Time:07-10

Below is my batch script Batch1.bat in usb

setlocal enabledelayedexpansion
for /f "tokens=2delims=:" %%a in ('netsh wlan show profile ^|findstr ":"') do (set "ssid=%%~a" & call :getpwd "%%ssid:~1%%")
:getpwd
set "ssid=%*"
for /f "tokens=2delims=:" %%i in ('netsh wlan show profile name^="%ssid:"=%" key^=clear ^| findstr /C:"Key Content"') do echo ssid: %ssid% pass: %%i >> c:\Wifi.txt

I am writing another batch script that uses echo to write above batch script as give below:

@echo off
echo setlocal enabledelayedexpansion >>c:\batch2.txt
echo for /f "tokens=2delims=:" %%%%a in ('netsh wlan show profile ^^^|findstr ":"') do (set "ssid=%%%%~a" ^& call :getpwd "%%%%ssid:~1%%%%") >>c:\batch2.txt
echo set "ssid=%%*" >>c:\batch2.txt
echo for /f "tokens=2delims=:" %%%%i in ('netsh wlan show profile name^^="%%ssid:"=%%" key^=clear ^^^| findstr /C:"Key Content"') do echo ssid: %%ssid%% pass: %%%%i >> c:\Wifi.txt >>c:\batch2.txt

But 5th line is not being echoed and written. Tried all kinds of escaping. Please anyone help.

echo for /f "tokens=2delims=:" %%%%i in ('netsh wlan show profile name^^="%%ssid:"=%%" key^=clear ^| findstr /C:"Key Content"') do echo ssid: %%ssid%% pass: %%%%i >> c:\Wifi.txt >>batch2.txt
pause

normally echo works but while redirecting it to file batch2.txt does'nt work

also tried ^>^> c:\Wifi.txt

But nothing working

CodePudding user response:

Here's one example using your echo methodology, no explanation provided:

@(  Echo @(For /F "Tokens=2 Delims=:" %%%%G In ('%%SystemRoot%%\System32\netsh.exe WLAN Show
    Echo  Profiles 2^^^>NUL ^^^| %%SystemRoot%%\System32\find.exe ":"'^) Do @(Set "Profile=%%%%G"
    Echo    Call :GetPwd "%%%%Profile:~1%%%%"^)^) 1^>"C:\WiFi.txt"
    Echo @GoTo :EOF
    Echo(
    Echo :GetPwd
    Echo @For /F "Tokens=2 Delims=:" %%%%G In ('%%SystemRoot%%\System32\netsh.exe WLAN Show
    Echo  Profiles Name^^^="%%~1" Key^^^=clear ^^^| %%SystemRoot%%\System32\find.exe "Key Content"
    Echo  '^) Do @Echo Profile: %%~1 Pwd:%%%%G) 1>"C:\batch2.txt"

Please note that I have left the output file names and paths as per your example. However, for your chosen location it may be necessary for you to run your script elevated or as a user with non standard permissions. Standard users do not generally have write access to the root of the C: drive, C:\. Those two filepaths, in between the doublequotes at the end of lines 3 and 9 are the only things you should optionally change before testing the above code. I will not provide answer support for any other changes whatsoever.

CodePudding user response:

The fifth line in the batch file in the question does not work as expected for multiple reasons. But the command line would not work even if all special characters would be escaped correct to get the command line written into c:\batch2.txt.

The following batch file code could be used to create %Temp%\Batch2.cmd

@echo off
(
echo @echo off
echo setlocal EnableExtensions DisableDelayedExpansion
echo for /F "tokens=1* delims=:" %%%%G in ('%%SystemRoot%%\System32\netsh.exe wlan show profile 2^^^>nul ^^^| %%SystemRoot%%\System32\find.exe ":"'^) do for /F "tokens=*" %%%%I in ("%%%%H"^) do for /F "tokens=3*" %%%%J in ('%%SystemRoot%%\System32\netsh.exe wlan show profile name^^="%%%%I" key^^=clear ^^^| %%SystemRoot%%\System32\find.exe "Key Content"'^) do echo ssid: %%%%I pass: %%%%K^>^>"%~dp0\Wifi.txt"
echo del "%%~f0" ^& exit
) >"%Temp%\batch2.cmd"
endlocal

The created batch file %Temp%\batch2.cmd must be run with elevated privileges of a local administrator which is most likely the reason why this batch file should create one more batch file.

The created batch file %Temp%\batch2.cmd contains the following lines if C:\Temp is the directory containing the batch file above:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1* delims=:" %%G in ('%SystemRoot%\System32\netsh.exe wlan show profile 2^>nul ^| %SystemRoot%\System32\find.exe ":"') do for /F "tokens=*" %%I in ("%%H") do for /F "tokens=3*" %%J in ('%SystemRoot%\System32\netsh.exe wlan show profile name^="%%I" key^=clear ^| %SystemRoot%\System32\find.exe "Key Content"') do echo ssid: %%I pass: %%K>>"C:\Temp\Wifi.txt"
del "%~f0" & exit

This batch file gets for each stored WLAN the SSID name and the WLAN password and writes in into file Wifi.txt in the directory of the batch file above. This second batch file deletes itself with the last command line. That code is much better than what is used in the question as it works even for SSID names or WLAN passwords containing an exclamation mark.

The creation of one more batch file from within a batch file just to run it as administrator would not be needed at all. There is in real only one batch file necessary which prompts the user for elevation if not executed in an environment with elevated privileges of a local administrator and then runs the command line to get the WLAN data of interest written into the text file. See my answer on Can't run as Admin.

  • Related