okay i have this command
netsh wlan show profiles
pretend that the wireless network that appears is named Zask.router
the prompt should appear something like this;
..........................................................
Profiles on interface wifi:
group policy profiles (read only)
<none>
user profiles
All User Profile : Zask.router
..........................................................
is there any way i can get the content "Zask.router" to be placed in a variable?
okay thanks, new question. i changed the code to this;
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "profile=%%A"
now i added this to the next line;
netsh wlan show profiles "%profile%" key=clear
then something that says "key content" (must be administrator) should appear with a wifi password next to it, how do i get that password to also go into a variable?
i tried doing something like this but it just displays the word "such" for some strange reason...
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "profile=%%A
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles "%profile%" key=clear') do set "profile2=%%A"
echo %profile2%
Assuming that the output you want is on the last line...
for /f "tokens=4 delims=: " %%A in ('netsh wlan show profiles') do set "YourVariable=%%A"
Use the "FOR /?" command to see help for FOR command. We are using it to split your lines into tokens separated by either spaces or :
tokens=4 specifies that we want to save the 4th token
delims=:(space) specifies that the characters ":" and " " are delimiters
YourVariable is being set for every line in the output of the NETSH command. In this case, since we only care about the last line it works out without the need to compare text to find the desired line.
Related
I started writing this small batch file, where I get all the wlan data with:
netsh wlan show profile (name) key=clear
This is not a problem but I am asking if there is a variable like for example:
%CurrentWlan%
so I can do:
netsh wlan show profile %CurrentWlan% key=clear >wlan.txt
Here is a possible solution:
#echo off
for /f "eol=B tokens=*" %%A IN ('netsh wlan show interfaces ^| findstr SSID') do (
for /f "delims=: tokens=2" %%A IN ("%%A") do (
for /f "tokens=*" %%A IN ("%%A") do (
netsh wlan show profiles "%%A" key=clear >wlan.txt
)
)
)
Which I am going to break it down:
We first parse the output of the command netsh wlan show interfaces searching for SSID string. As there is also a line containing BSSID we ignore it with eol=B.
Now, we want to parse the value after : symbol, so we set it as delimeter. We can access the network name, now, setting tokens to 2.
We remove all unneded spaces in the result with another for loop specifying tokens option to *.
So, now, we want all the info about currently connected network (%%A). We redirect output to wlan.txt.
What I am trying to do is a backup by cmd commands but the problem is when I am taking the USB backup to another PC to back up the drive names are different.
For example when I do:
XCOPY G:\*.BMP X:\ /h/i/c/k/e/y/r/d
In the other computer the drives will not be G and X.
What I am seeking to do is if it is possible to make a program that I can enter with the keyboard what drive I want to backup and to what drive.
For example:
XCOPY "driver name keyboard input":\*.BMP "driver name keyboard input":/" /h/i/c/k/e/y/r/d
Yes, it can be done using a PowerShell or batch-file script (cmd tag seems to imply Windows OS).
Let's choose the latter. Next batch-file code snippet would do the same as the command in question: XCOPY G:*.BMP X:\ /h/i/c/k/e/y/r/d:
set "DriveIn=G"
set "DriveOu=X"
XCOPY %DriveIn%:*.BMP %DriveOu%:\ /h/i/c/k/e/y/r/d
Instead of hard-coded DriveIn and DriveOu, you can prompt for user input:
set /P "DriveIn=please choose SOURCE drive letter "
set /P "DriveOu=please choose TARGET drive letter "
XCOPY %DriveIn%:*.BMP %DriveOu%:\ /h/i/c/k/e/y/r/d
Hints for (necessary!) validity checks:
:dfromscratch
set "DriveIn="
set "DriveOu="
:dsource
set /P "DriveIn=please choose SOURCE drive letter "
rem basic validity check
if not defined DriveIn goto :dsource
if not exist "%DriveIn%:\" goto :dsource
:dtarget
set /P "DriveOu=please choose TARGET drive letter "
rem basic validity check
if not defined DriveOu goto :dtarget
if not exist "%DriveOu%:\" goto :dtarget
if /I "%DriveIn%"=="%DriveOu%" goto :dfromscratch
rem involve more validity check here!!!
XCOPY %DriveIn%:*.BMP %DriveOu%:\ /h/i/c/k/e/y/r/d
Some hints for (more) validity checks.
To show available disk drives:
wmic logicaldisk get Description, DeviceID, DriveType, FileSystem, VolumeName
To get a list of available disk drives, use for /F loop (note %%G in a batch script):
for /F %%G in ('
wmic logicaldisk where "DriveType != 5" get DeviceID^, DriveType^|find ":"
') do echo %%G
or next oneliner (note %G in cmd): copy&paste into an open cmd window:
for /F %G in ('wmic logicaldisk where "DriveType != 5" get DeviceID^, DriveType^|find ":"') do #echo %G
Another approach (only a draft, needs more elaboration): build a list of available drive letters %drives% and use choice command instead of set /P:
set "drives=GX"
choice /C %drives% /M "Select SOURCE drive letter"
Okay, this is kind of complicated so ill try to explain it as best as is can.
I am currently writing a simple program for my own use using notepad.
I am using the language batch and running the program through Command Prompt.
Part of the Program lets you access an account you created with username and password:
set /p USERNAME1= Username?
findstr /n "%USERNAME1%" Usernames.txt
In Usernames.txt are the usernames of each account that has been created, one per line.
If your username is found, it is displayed along with the line number before it in the program.
It then asks for your password:
set /p PASSWORD1= Password?
This is where the problem starts. When the accounts are created, the usernames are stored, one per line, in Usernames.txt , and so are the passwords but in Passwords.txt
I need the program to check if the password you typed is the same as the password on the same line number the username is on, in Passwords.txt
I know this is complicated but if anyone can help it would be greatly appreciated!
Thanks
You're using findstr /N to get the line number of usernames.txt, which is a good start. You're getting the entire line, prefixed with the line number and a colon :.
So for /F can be used to extract the number only. The option string "tokens=1 delims=:" defines to divide the found line at the (first) :, so the line number is separated from the user name.
Finally, another for /F can be used to get the line of the passwords.txt files.
Putting all those things together, the following code snippet emerges:
set /P USERNAME1=Username?
set /P PASSWORD1=Password?
set /A LINENUMBER=0
for /F "tokens=1 delims=:" %%I in (
'findstr /N /I /X /C:"%USERNAME1%" "\path\to\usernames.txt"'
) do (
set LINENUMBER=%%I
goto :CONTINUE1
)
:CONTINUE1
set /A LINENUMBER-=1
if %LINENUMBER% lss 0 (
exit /B
) else if %LINENUMBER% equ 0 (
set SKIPPING=
) else (
set SKIPPING=skip=%LINENUMBER%
)
for /F "usebackq %SKIPPING% delims=" %%I in (
"\path\to\passwords.txt"
) do (
if "%%I" equ "%PASSWORD1%" (
goto :CONTINUE2
)
exit /B
)
:CONTINUE2
rem do something...
So the variable USERNAME1 holds the entered user name to search and PASSWORD1 the entered password.
If the user name cannot be found in usernames.txt, or the entered password does not match the found one, the script is terminated using exit /B.
Note that user names are compared in a case-insensitive manner (/I switch), but the password are compared case-sensitively.
I am trying to think of an easier way, read less prone to messing up the commands, to resolve an IP address and then set a variable to said resolved name. I tried the following:
FOR /F "TOKENS=3 DELIMS=gw" %%A IN ('PING -a -n 1 IPADDRESS') DO SET "PC=%%A"
Which on:
Pinging PCNAME [IPADDRESS} with 32 bytes of data:
Reply from IPADDRESS: bytes=32 time =15ms TTL=122
Ping statistics for IPADDRESS:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss)
Approximate round trip times in milli-seconds:
Minimum = 15ms, Maximum = 15ms, Average = 15ms
Works for most pc names. There are only a few "g" and "w" so the only line with a 3rd token is the first. The problem I experienced is that we have A LOT of pc names and a few have a "g" in them. For those resolved addresses it cuts of the variable at the 3rd "g" of the line.
I tried using it on NSLOOKUP, but the command gives me:
C:\Windows\System32>nslookup IPADDRESS
Server: SERVERNAME
Address: SERVERIP
Name: PCNAME
Address: IPADDRESS
I can skip in FOR /F (ie skip 2) but then the it reads lines 3 AND 4; ie it sets the variable to the pc name for line 3, but then changes it on line 4 to garbage I don't need.
Like I said is there a way to this easily? I can do a for in a for with calls and delayed expansion, but that is much harder to keep track of.
Filter the info you need ("Name:")
for /f "tokens=2 delims=: " %%i in ('nslookup 192.168.178.1^|find "Name:"') do set name=%%i
There is a simple method that may be used with both PING or NSLOOKUP.
With PING:
FOR /F "TOKENS=2" %%A IN ('PING -a -n 1 IPADDRESS') DO SET "PC=%%A" & GOTO CONTINUE
:CONTINUE
With NSLOOKUP:
FOR /F "SKIP=2 TOKENS=2" %%A in ('NSLOOKUP IPADDRESS') DO SET "PC=%%A" & GOTO CONTINUE
:CONTINUE
The GOTO CONTINUE is used to break the FOR after the desired information was taken.
#echo off
setlocal enableextensions
set "ip=127.0.0.1"
set "pcName="
for /f "tokens=1-3 delims=[]" %%a in ('ping -n 1 -a %ip% 2^>nul') do if not defined pcName if not "%%c"=="" (
set "pcName=%%a"
setlocal enabledelayedexpansion
for %%d in ("!pcName: =\!.") do ( endlocal & set "pcName=%%~nxd")
)
echo %pcName%
This reads the output of ping command, and splits the lines using []. The only line containing square brackets is the line with the pc name (if found). We will skip lines that are not splitted in three tokens (%%c is empty).
The first token will contain the pc name prefixed by a string indicating a ping operation is being done. This string is localized and changes depending of the windows language (in my case, spanish windows, it is "Haciendo ping a"). So, asking for a fixed token is not reliable. To avoid it, the string is converted to a false path by replacing the spaces with backslashes. Then the standard for replaceable parameter modifiers are used to retrieve the name of the last element in the false path, that is, the pc name.
Using the original approach:
FOR /F "TOKENS=2* DELIMS=gw" %%A IN ('PING -a -n 1 IPADDRESS') DO if not "%%B"=="" for /f %%C in ("%%B") do SET "PC=%%C"
(just to demo)
I basically need a script (batch) that automatically converts the users on the local network to IP addresses. What I mean is basically set every user that shows up in the command "net view" to a different variable, for example:
If I had 4 different users on the network, I would need the file to list:
1: (%1%)
2: (%2%)
3: (%3%)
4: (%4%)
So I need the script to set each user on the network to a different variable (starting at 1)
Also...
How would I set the local IP address of each computer name as a variable?
update for second part: I need to know how to set the ip address as a variable. I just need to cut out the excess stuff. If I type
ping (computername) -4
I get: well, I guess it is just easier to show you...
I think I need to use the findstr command, but I don't know.
Is this what you're looking for?
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1" %%a in ('net view') do (
set comp=%%a & set comp=!comp:\\=!
for /f "tokens=2 delims=[]" %%b in (
'ping -4 !comp!'
) do (Echo !comp! - %%b)
)