Findstr : not getting the correct output - for-loop

Here is my batch file:
cmdkey.exe /list > "%TEMP%\List.txt"
findstr.exe Adobe "%TEMP%\List.txt" > "%TEMP%\adobeonly.txt"
for /f "tokens=2-3 delims= " %%G in ("%TEMP%\adobeaonly.txt" ^| findstr "Adobe Photoshop Subscription") do Set AdobeSub1=1
for /f "tokens=2 delims= " %%G in ("%TEMP%\adobeonly.txt" ^| findstr "Adobe Photoshop Subscription #2") do Set AdobeSub2=1
Text file looks like (adobeonly.txt):
Target: LegacyGeneric:target=Adobe Photoshop Subscription #2
Target: LegacyGeneric:target=Adobe Photoshop Subscription
the findstr command gets always the two lines I cant make difference between the first line and the second line.
Can somebody helpme with that issue.
PS: Sorry for my english.

Related

How do you set a specific part of the output of a CMD batch command as a variable? or alternatively, just echo it?

I'm writing a simple little batch file that gets the password of a saved Wi-Fi network, but I want to grab the Key Content, then paste it on its own. Here's the current code:
#echo off
set /p name=Enter Wi-Fi Name:
cls
echo %name%
netsh wlan show profile name="%name%" key=clear
cmd /k
This gives me a long list of data, but the line I'm looking for is the "Key Content" line. What I essentially want to do is grab the "Key Content" line, clear all the lines, then echo the "Key Content" line. Is this possible without any plugins on Windows 11?
I'm new to the site and coding as a whole, by the way, so what may seem like something completely obvious to you is something I have a 95 percent chance not to know. Thank you!
Here's a batch-file snippet, based upon you having received and properly validated the end users input:
#For /F "Tokens=1,* Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%name%" Key^=Clear
2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do #(Set "}=%%I" & SetLocal EnableDelayedExpansion
For %%J In ("!}:~1!") Do #EndLocal & Echo(%%J)
You should always perform robust validation of any end users input, especially when using the Set /P command, which can accept nothing or absolutely anything, (including malicious content). If the target systems are Windows 8 / Server 2012 or above, then I wouldn't waste time asking the end user to self-determine the wireless profile name, and then type it correctly at a prompt. I'd just output all of the profile names, along side their returned keys.
The following examples are untested, and assume that your 'passwords' do not include double-quote characters. They should provide the output in a CSV-like format, ("ProfileName","Password"):
#For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
/NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name
/Format:MOF 2^>NUL') Do #For /F "Tokens=1,* Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name^="%%G" Key^=Clear
2^>NUL ^| %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: "
') Do #(Set "}=%%I" & SetLocal EnableDelayedExpansion
For %%J In ("!}:~1!") Do #EndLocal & Echo("%%G",%%J)
#Pause
As you appear to have used the cmd tag, despite your question being about a batch-file, you could probably do similarly, directly in the Command Prompt, (cmd.exe) too:
For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get Name /Format:MOF 2^>NUL') Do #For /F "Tokens=1,* Delims=:" %H In ('"%SystemRoot%\System32\netsh.exe WLAN Show Profiles Name="%G" Key=Clear 2>NUL | %SystemRoot%\System32\findstr.exe /RIC:"^[ ][ ]*Key Content[ ][ ]*: ""') Do #Set "}=%I" & For /F Delims^=^ EOL^= %J In ('%SystemRoot%\System32\cmd.exe /V /D /C "Echo "%G","!}:~1!""') Do #(Set /P ="%J") 0<NUL & Echo(

Batch scripting, mutlple finds and multple do sets

At the moment I am running the following to get some information from 'systeminfo', however it needs to run systeminfo twice which takes some time. How would I be able to do multple 'Find"XXX" do sets'?
For /f "delims=" %%A IN ('systeminfo ^| Find "OS Name"') DO Set "VarA=%%A"
For /f "delims=" %%A IN ('systeminfo ^| Find "BIOS Version"') DO Set "VarB=%%A"
Any help is greatly appreciated.
Next code snippet could work (note set commands are merely ECHOed for debugging purposes; remove capitalized ECHO no sooner than debugged):
For /f "delims=" %%A IN ('systeminfo') DO (
For /F "delims=" %%G IN ('echo %%A ^| Find /I "OS Name"') Do ECHO Set "VarA=%%A"
For /F "delims=" %%G IN ('echo %%A ^| Find /I "BIOS Version"') DO ECHO Set "VarB=%%A"
)
However, follow Stephan's advice and parse wmic output rather (do not forget /value option). To do that correctly, note great Dave Benham's article WMIC and FOR /F: A fix for the trailing <CR> problem
I too would use WMIC, but there is a simple solution if you want to use SYSTEMINFO - use FINDSTR with two /C:"search strings"
for /f "tokens=1,2*" %A in ('systeminfo ^| findstr /c:"OS Name" /c:"BIOS Version"') do set "%A=%C"
The above will define two variables on an English machine: OS and BIOS.
Thanks for the replies guys.
I wanted to set the bit version windows the batch file would run on, I figured it out ages ago but thought I would post it here for those that came across it:
echo %PROCESSOR_ARCHITECTURE% | find "64" > NUL
If %ERRORLEVEL% equ 0 (Set bit=64) else (Set bit=32)
Thanks!

windows 7 not generating txt report

I want to generate a txt file with my dns history. Although the batch script executes just fine on windows 8, when i run it on windows 7 it simply creates a blank txt file. Does anyone knows why this is happening?
Here's the batch script
#echo off
setlocal enableextensions
set "baseName=dnshistory"
set "count=0"
for /f "delims=%baseName%." %%a in (
'dir /b /o-d "%baseName%*.txt" 2^>nul'
) do ( set /a "count=%%a+1" & goto saveData )
:saveData
ipconfig /displaydns | find "Record Name" > "%baseName%%count%.txt"
Is you Windows 7 version in English too ?
Open a CMD windows and test just the command :
ipconfig /displaydns | find /i "Record Name"
and look if something is displayed.
If not, try just the command :
ipconfig /displaydns
and look the language used Then correct your code with the correct words.
IE in Portuguese it will be :
ipconfig /displaydns | find /i "Nome do Registro"
try this for a language independent solution:
:saveData
(for /f "tokens=2 delims=:" %%a in ('ipconfig /displaydns') do (
echo %%a| find "." |findstr /v /r "[0-9]$"
))>file.txt
(take every line, filter those, that have a . after a : (second token) and filter out all lines that end with a number)
EDIT another approach (because the above gives some unwanted lines):
find the first line after every ----------------- line:
#echo off
setlocal enabledelayedexpansion
ipconfig /displaydns |findstr /n "^" >a.txt
for /f "tokens=1 delims=:" %%a in ('findstr /c:" --------------" a.txt') do (
set /a line=%%a+1
for /f "tokens=1,2,* delims=:" %%i in ('findstr /B "!line!:" a.txt') do echo(%%k
)

Printing a specific line of a file in windows cmd

I am trying to print specific lines of a text file stored on my computer by using the Windows cmd.
For instance if I want to output line 17 of the textfile in the cmd, how is this possible? I have been using quite some time on this and I am running out of ideas.
Could you please help?
Regards
Magnus
#echo off
for /f "tokens=1,* delims=]" %%a in ('find /n /v "" ^< "myfile.txt" ^| findstr "^\[17\]" ') do set "variable=%%b"
echo "%variable%"

The ECHO command echo only show the last line of my file instead of shows each line

I want to show out of the console each line from a file. I try this:
findstr /v /b /c:" " <%1>toto
for /f "tokens=*" %%a in (toto) do set co=%%a
echo. %co%
Also this one:
findstr /v /b /c:" " <%1>toto
for /f "tokens=*" %%a in (toto) do (set co=%%a
echo. %co%
)
But the first way shows me only the last line of file and the second one doesn't show me anything.
Someone can explain me what's wrong in these latter and how to get in the 'co' variable each line printed on the console?
Thanks
you override co. to append to it try
set co=%co% %%a
Try this:
for /f "tokens=1*delims=:" %%a in ('findstr /n "^" "toto"') do echo %%b

Resources