Windows: How to programatically connect to wireless networks? - windows

With Windows 10 is it possible to setup up known networks and be able to connect to them without all the mouse movement and click?

Using Windows batch files, you can set it up to connect to networks you already know (Network1 or Network2, below) without ever touching the mouse.
#echo off
setlocal EnableDelayedExpansion
for %%i in ("Network1"
"Network2") do (
netsh wlan show networks mode=ssid | findstr /C:%%i
if !ERRORLEVEL! EQU 0 (
echo "Found %%~i - connecting..."
netsh wlan connect name=%%i
exit /b
) else (
echo "Did not find %%~i"
)
)
#echo on
Save the above to .bat and run it from cmd.exe or a program like Listary.
Some comments about the code:
If more than one of your listed networks are available, it will connect to whichever is first in the for loop list. You could also put the list in a file and change for %%i to for /F %%i
EnableDelayedExpansion and "!" around ERRORLEVEL
are needed to keep the variable ERRORLEVEL from being assigned
whatever it was at the beginning of the script. Since I don't
normally program Windows batch files, this is 2 hours of my life
gone that you won't have to deal with.
All the echoing is for debugging; the echo off at the top squelches it.
%% needed for variables in Windows batch files. The variable is referenced with % at the command line.
%%~i strips the quotation marks around the string when outputting to stdout.

Related

Batch : ECHO outputs chinese character instead of numbers

I'm trying to work around some logs to extract the data I want and to push it into another simplified .txt file (before going for the next step).
Here's the bit of code I've been trying to use to reach my goal:
setlocal ENABLEDELAYEDEXPANSION
for %%i in (C:\Test_Analyse\*files*.txt) do (
SET va=%%i
SET va=!va:~16,-31!
find /v /c "" %%i | FINDSTR /V /R /C:"^$">>C:\test_results\!va!log3.txt
set /p var=<C:\test_results\!va!log3.txt
set var=!var:~68,10!
echo !var!>>C:\test_results\!va!log2.txt
)
endlocal
The C:\test_results\!va!log3.txt file content is : ---------- C:\TEST_ANALYSE\1K43782_TEST_RENAMED_FILES_20210915.TXT: 223856.
As far as I know, it does its job except for the echo !var!>>C:\test_results\!va!log2.txt part. It prints Chinese characters in my output file instead of 223856. On a side note, when I discard the #echo OFF, I notice the ECHO line working properly in CMD - so I guess it's maybe about encryption? But I tried a few things around that, without success sadly.

%errorlevel% returning 0 in loop command

Ok, I've installed Dropbox but it didn't corresponded to what I was looking for so I uninstalled it with Revo Pro.
But, when i open the taskmanager there are still processes related to it running in my computer so I decided to make a batch to look out and delete all files that are related to it.
#echo off
cd c:\
:a
set /p a=Phrase that might be realted to it
for /r %%d IN (*.*) DO (
(
findstr /i /m /c:%a% "%%d"
if "%errorlevel%"=="0" del "%%d"
echo %errorlevel%
)
)
pause
The problem is: when I run findstr using loop even when there is no match for my variable "%a%" in an analized file %errorlevel% returns as 0. But when I use findstr alone and there isn't a match %ERRORLEVEL% returns as 1 and 0 for a match.
If I use it, I'll delete all my PC files haha. What's wrong with the code?
Within a parenthesised series of statements, any %var% is replaced by the value of that variable at the time the verb controlling that statement-sequence (or block) is encountered.
Here, the block is the entire sequence of statements controlled by the for. %errorlevel% is replaced by the status of errorlevel at the time the for is encountered, so probably 0.
If you use
findstr /i /m /c:%a% "%%d"
if not errorlevel 1 del "%%d"
echo %errorlevel%
then the run-time value of errorlevel is used (ie. as it changes through the operation of the loop) and the command means "if errorlevel is not (1 or greater than 1) do this..."
The findstr will set errorlevel to 0 on found, 1 on not found and 2 for file not found(IIRC) so NOT (1 or greater than 1) selects 0 only. Note that in certain esoteric circumstances, errorlevel may become negative, but after a findstr I believe 0..2 is the allowed range.
Not sure what's wrong with the code, but you can probably skip it using the && operand.
findstr /i /m /c:%a% "%%d" && del "%%d" echo %errorlevel%
Thanks to Stephan for correcting the example.
Whenever Windows command interpreter encounters ( being interpreted as begin of a command block, it parses the entire command block up to matching ) marking end of the command block and replaces all %variable% by current value of the variable.
This means in this case that before command FOR is the first time executed, everything from ( after DO up to last ) is processed already with replacing all %variable% references by current value of the appropriate variable. Then the already preprocessed block is executed one (on command IF) or more times (on command FOR).
This behavior can be seen by debugging the batch file. For debugging a batch file first #echo off must be removed or commented out with command REM or changed to #echo on. Then a command prompt window must be opened and the batch file is executed from within this command prompt window by typing its name with full path enclosed in double quotes if path or name contains a space character. The Windows command interpreter shows now all command lines and command blocks after preprocessing before executing and of course the standard messages and the error messages output by the commands or by Windows command interpreter itself in case of a syntax error in batch file.
Opening a command prompt window means running cmd.exe with option /K to Keep window open after execution of a command or a batch script. Double clicking on a batch file starts also cmd.exe for processing the batch file, but with parameter /C to Close the window automatically after batch processing terminated independent on cause - successful finished or an error occurred.
The command prompt window opened before running the batch file remains open after batch processing finished successfully or with an error except the batch file contains command EXIT without parameter /B. So experts in batch code writing test batch files always by running them from within a command prompt window instead of double clicking on them.
Delayed variable expansion is needed for variables set or modified and referenced within same command block as explained by help of command SET output on running in a command prompt window set /?.
#echo off
setlocal EnableDelayedExpansion
cd /D C:\
:a
set /P "a=Phrase that might be realted to it: "
for /r %%d in (*) do (
%SystemRoot%\System32\findstr.exe /i /m /c:"%a%" "%%d"
if "!errorlevel!" == "0" del "%%d" >nul
)
endlocal
But for checking the exit code of a previous command there is also if errorlevel syntax as explained by Microsoft in support article Testing for a Specific Error Level in Batch Files.
#echo off
setlocal EnableDelayedExpansion
cd /D C:\
:a
set /P "a=Phrase that might be realted to it: "
for /r %%d in (*) do (
%SystemRoot%\System32\findstr.exe /i /m /c:"%a%" "%%d" >nul
if not errorlevel 1 del "%%d" >nul
)
endlocal
if errorlevel X tests if exit code of previous command or application when it modifies the errorlevel variable at all is greater or equal X. By using if not errorlevel X the check is if last exit code is lower than X which is here a test if exit code is 0.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
del /?
echo /?
for /?
if /?
set /?
And see also
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line
Microsoft article about Using command redirection operators
Answer on question Single line with multiple commands using Windows batch file
How to set environment variables with spaces?

How do I make a batchscript that displays only the last line of a textfile?

this might have already been answered before but I couldn't find anything on that topic. Im trying to make a chat messenger in batch and for that I need to display the last line of a textfile. Here's what I tried (not very elegant):
#echo off
FOR /F %%x in (address.txt) DO set address=%%x
:A
IF NOT EXIST "%address%" GOTO A
GOTO B
:B
SET skipcount=1
:C
FOR /f "skip=%skipcount%" %%m in (%address%) DO ECHO %%m
SET m1=%%m
:D
FOR /f %%m IN (%address%) DO ECHO %%m > NUL
IF NOT %%m==%m1% SET skipcount=%skipcount%+1 GOTO D
GOTO C
This might work but I think it is full of mistakes for example syntax errors^^ So I am just trying to get a few hints to what is wrong:)
here's a pure batch utility (requires no external tools) that can shows a range of numbered lines
to show the last line use it like this:
call tailHead.bat -file=address.txt -end=1
You can use my JREPL.BAT regular expression text processing utility to create a tail command. JREPL.BAT is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
The following command will show the last line in chat.txt
call jrepl "^.*" "" /match /inc -1 /f chat.txt
But there is a much better way to develop a batch chat program (assuming that is a worthwhile goal)
You can have a batch process in a loop, with input redirected outside the loop, and the loop will read and write newly added lines as they appear. You could use SET /P and ECHO, but it is simpler to use a single FINDSTR. This works because FINDSTR does not reset the file pointer when called, as explained at http://www.dostips.com/forum/viewtopic.php?p=9720#p9720.
You should use some command to suspend processing briefly within the display loop to prevent the loop from consuming 100% of a CPU core. You could use TIMEOUT, or the PING hack, but they introduce a ~1 second delay. I chose to use PATHPING to introduce a ~0.2 second delay.
Also, you must worry about preventing collisions if two processes write to the same text file simultaneously. This can be solved by using lock files, as explained at How do you have shared log files under Windows?.
Below is the beginning of a rudimentary batch chat program. It works by having two or more users each navigate to the same shared directory, and then run chat.bat sessionName, where sessionName is an agreed upon name for the shared chat file. Each user will get the shared chat dialog displayed in their master console window, and a new console window will open where they can write their contributions to the conversation. Enter :quit to exit the chat program.
#echo off
setlocal disableDelayedExpansion
if "%~1" equ ":input" goto :startInput
if "%~1" equ ":display" goto :display
set "base=%~1"
set "dialog=%base%.chat"
set "quitfile=%base%_%username%.chat.quit"
start "" "%~f0" :input
del "%quitfile%" 2>nul
cmd /c "%~f0" :display
del "%quitfile%" 2>nul
exit /b
:display
title Chat Dialog
set "quit="
if not exist "%dialog%" (call ) >>"%dialog%"
<"%dialog%" ( for /l %%N in () do (
if exist "%quitfile%" set "quit=1"
findstr "^"
if defined quit exit
pathping -p 150 -q 2 localhost >nul
))
:startInput
setlocal enableDelayedExpansion
title Chat Input
call :write ">>> %username% has joined the conversation"
:input
cls
set "text="
set /p "text=>"
if /i !text! equ :quit (
call :write "<<< %username% has left the conversation"
copy nul "!quitfile!"
exit
)
call :write
goto :input
:write
if "%~1" neq "" (set "text=%~1") else (set "text=%username%: !text!")
2>nul (
>>"!dialog!" (
echo(!text!
(call )
) || goto :write
)
exit /b
Still to be done:
Provide a mechanism to invite users to a chat.
Provide an option to clear chat contents at the beginning of a new chat session (in case an old sessionName is reused)
Provide a :list command to list the currently participating users. This would require creation of sessionNameUserId files that would remain locked as long as the user is still listening and/or participating. The display loop could receive a :list command via a file, the same way as I implemented :quit, and then it could attempt to open each sessionNameuserID file for writing. If it fails then the user is still active, and the name should be listed.
I'm sure there are other things that might be useful.

How to read net use generated text file and use the net use command to remap the network drive

How to read net use generated text file lets called it network_drive.txt that consist of the current machine mapped network drive as image below:
New connections will be remembered.
Status --- Local --- Remote ------------------ Network
-------------------------------------------------------------------------------
OK ---------- H: ---- \\server\users\john -----
Microsoft Windows Network
OK ---------- Y: ---- \\server\e$\ --------------
Microsoft Windows Network
The command completed successfully.
How to read the file above to map the network drive again with the same Letter path and path only if the status is ok and ignore the unavailable one?
update!
#echo off
set drive=\\server\users\john\
net use > %drive%\%USERNAME%_temp_Networkdrive.txt <-- generating net use file
for /f "skip=6 delims=*" %%a in (%drive%\%USERNAME%_temp_Networkdrive.txt) do (
echo %%a >>%drive%\%USERNAME%_del_Networkdrive.txt ) <-- deleting the first 6 lines
xcopy %drive%\%USERNAME%_del_Networkdrive.txt %drive%\%USERNAME%_Networkdrive.txt /y <-- make a new copy of the network drive file after deleting the lines
findstr /v "The command completed successfully." %drive%\%USERNAME%_del_Networkdrive.txt > %drive%\%USERNAME%_Networkdrive.txt <-- find the string and delete them and make a new copy of file with just the network drives
del %drive%\%USERNAME%_del_Networkdrive.txt /f /q
del %drive%\%USERNAME%_temp_Networkdrive.txt /f /q
for /f "tokens=2,3,4" %%a in (%drive%\%USERNAME%_Networkdrive.txt) do ( net use %%a %%b ) **<-- find the letter path and the drive path and map accordingly.**
however..
in some cases, sometimes the "Microsoft Windows Network" is on the same line as the letter and Drive path and hence deleting the record/line.
can someone help pls?
Update.
I removed Microsoft Windows Network from the findstr line because the tokens in the for loop would only pick up the second and third strings for the net use command.
I have tested it and it works.
Also, it would be a good idea to use if exist command on the second line just to see if the file is exist before running the other commands.
#ECHO OFF
SETLOCAL
FOR /f "tokens=2*delims=: " %%a IN ('type q20294868.txt^|find "\"^|findstr /b "OK"') DO ECHO NET use %%a: %%b
ECHO(======================
FOR /f "tokens=2*delims=: " %%a IN ('type q20294868.txt^|find "\"^|findstr /b "OK"'') DO FOR /f %%c IN ("%%b") DO ECHO NET use %%a: %%c
GOTO :eof
This should do what you appear to want.
You should replace type q20294868.txt with net use for your situation. q20294868.txt is simply a file I used to save your test data.
There are two separate methods here. The text is filtered first for lines containing \ and then for those /b beginning "OK"
I'm unsure whether the network name may potentially be included on a data line rather than on a line by itself, consequently I've devised the second method. The first is simpler, the second more robust.
Note that your original findstr would have eliminated any lines containing ANY of the individual words contained in the quotes - see findstr /? from the prompt for more information.
And of course, the resultant NET USE command is merely ECHOed to the screen, not executed.

Changing a batch file when its running

I am running a long running batch file. I now realize that I have to add some more commands at the end of the batch file (no changes to exisiting content, just some extra commands). Is it possible to do this, given that most batch files are read incrementally and executed one by one? Or does the system read the entire contents of the file and then runs the job?
I just tried it, and against my intuition, it picked up the new commands at the end (on Windows XP)
I created a batch file containing
echo Hello
pause
echo world
I ran the file, and while it was paused, added
echo Salute
Saved it and pressed enter to contine the pause, all three prompts were echoed to the console.
So, go for it!
The command interpreter remembers the line position byte offset it's at in the batch file. You will be fine as long as you modify the batch file after the current executing line position byte offset at the end of the most recently parsed line of code.
If you modify it before then it will start doing strange things (repeating commands etc..).
jeb's example is a lot of fun, but it is very dependent on the length of the text that is added or deleted. I think the counter-intuitive results are what rein meant when he said "If you modify it before then it will start doing strange things (repeating commands etc..)".
I've modified jeb's code to show how dynamic code of varying length can be freely modified at the beginning of an executing batch file as long as appropriate padding is in place. The entire dynamic section is completely replaced with each iteration. Each dynamic line is prefixed with a non interfering ;. This conveniently allows FOR /F to strip the dynamic code because of the implicit EOL=; option.
Instead of looking for a particular line number, I look for a specific comment to locate where the dynamic code begins. This is easier to maintain.
I use lines of equal signs to harmlessly pad the code to allow for expansion and contraction. Any combination of the following characters could be used: comma, semicolon, equal, space, tab and/or newline. (Of course the padding cannot begin with a semicolon.) The equal signs within the parentheses allow for code expansion. The equal signs after the parentheses allow for code contraction.
Note that FOR /F strips empty lines. This limitation could be overcome by using FINDSTR to prefix each line with the line number and then strip out the prefix within the loop. But the extra code slows things down, so it's not worth doing unless the code is dependent on blank lines.
#echo off
setlocal DisableDelayedExpansion
echo The starting filesize is %~z0
:loop
echo ----------------------
::*** Start of dynamic code ***
;set value=1
::*** End of dynamic code ***
echo The current value=%value%
::
::The 2 lines of equal signs amount to 164 bytes, including end of line chars.
::Putting the lines both within and after the parentheses allows for expansion
::or contraction by up to 164 bytes within the dynamic section of code.
(
call :changeBatch
==============================================================================
==============================================================================
)
================================================================================
================================================================================
set /p "quit=Enter Q to quit, anything else to continue: "
if /i "%quit%"=="Q" exit /b
goto :loop
:changeBatch
(
for /f "usebackq delims=" %%a in ("%~f0") do (
echo %%a
if "%%a"=="::*** Start of dynamic code ***" (
setlocal enableDelayedExpansion
set /a newValue=value+1, extra=!random!%%9
echo ;set value=!newValue!
for /l %%n in (1 1 !extra!) do echo ;echo extra line %%n
endlocal
)
)
) >"%~f0.tmp"
::
::The 2 lines of equal signs amount to 164 bytes, including end of line chars.
::Putting the lines both within and after the parentheses allows for expansion
::or contraction by up to 164 bytes within the dynamic section of code.
(
move /y "%~f0.tmp" "%~f0" > nul
==============================================================================
==============================================================================
)
================================================================================
================================================================================
echo The new filesize is %~z0
exit /b
The above works, but things are much easier if the dynamic code is moved to a subroutine at the end of the file. The code can expand and contract without limitation, and without the need for padding. FINDSTR is much faster than FOR /F at removing the dynamic portion. Dynamic lines can be safely be prefixed with a semicolon (including labels!). Then the FINDSTR /V option is used to exclude lines that begin with a semicolon and the new dynamic code can simply be appended.
#echo off
setlocal DisableDelayedExpansion
echo The starting filesize is %~z0
:loop
echo ----------------------
call :changeBatch
call :dynamicCode1
call :dynamicCode2
echo The current value=%value%
set /p "quit=Enter Q to quit, anything else to continue: "
if /i "%quit%"=="Q" exit /b
goto :loop
:changeBatch
(
findstr /v "^;" "%~f0"
setlocal enableDelayedExpansion
set /a newValue=value+1, extra=!random!%%9
echo ;:dynamicCode1
echo ;set value=!newValue!
echo ;exit /b
echo ;
echo ;:dynamicCode2
for /l %%n in (1 1 !extra!) do echo ;echo extra line %%n
echo ;exit /b
endlocal
) >"%~f0.tmp"
move /y "%~f0.tmp" "%~f0" > nul
echo The new filesize is %~z0
exit /b
;:dynamicCode1
;set value=33
;exit /b
;
;:dynamicCode2
;echo extra line 1
;exit /b
Short answer: yes, batch files can modify themselves whilst running. As others have already confirmed.
Years and years ago, back before Windows 3, the place I worked had an inhouse menu system in MS-DOS. The way it ran things was quite elegant: it actually ran from a batch file that the main program (written in C) modified in order to run scripts. This trick meant that the menu program itself was not taking up memory space whilst selections were running. And this included things like the LAN Mail program and the 3270 terminal program.
But running from a self-modifying batch file meant its scripts could also do things like load TSR programs and in fact could do pretty much anything you could put in a batch file. Which made it very powerful. Only the GOTO command didn't work, until the author eventually figured out how to make the batch file restart itself for each command.
Nearly like rein said, cmd.exe remember the file position (not only the line position) it's currently is, and also for each call it push the file position on an invisble stack.
That means, you can edit your file while it's running behind and before the actual file position, you only need to know what you do ...
A small sample of an self modifying batch
It changes the line set value=1000 continuously
#echo off
setlocal DisableDelayedExpansion
:loop
REM **** the next line will be changed
set value=1000
rem ***
echo ----------------------
echo The current value=%value%
<nul set /p ".=Press a key"
pause > nul
echo(
(
call :changeBatch
rem This should be here and it should be long
)
rem ** It is neccessary, that this is also here!
goto :loop
rem ...
:changeBatch
set /a n=0
set /a newValue=value+1
set /a toggle=value %% 2
set "theNewLine=set value=%newValue%"
if %toggle%==0 (
set "theNewLine=%theNewLine% & rem This adds 50 byte to the filesize.........."
)
del "%~f0.tmp" 2> nul
for /F "usebackq delims=" %%a in ("%~f0") DO (
set /a n+=1
set "line=%%a"
setlocal EnableDelayedExpansion
if !n!==5 (
(echo !theNewLine!)
) ELSE (
(echo !line!)
)
endlocal
) >> "%~f0.tmp"
(
rem the copy should be done in a parenthesis block
copy "%~f0.tmp" "%~f0" > nul
if Armageddon==TheEndOfDays (
echo This can't never be true, or is it?
)
)
echo The first line after the replace action....
echo The second line comes always after the first line?
echo The current filesize is now %~z0
goto :eof
The command interpreter appears to remember the byte offset within each command file it is reading, but the file itself is not locked, so it is possible to make changes, say with a text editor, whilst it is running.
If a change is made to the file after this remembered location, the interpreter should happily continue to execute the now modified script. However if the change is made before that point, and that modification changes the length of the text at that point (for example you've inserted or removed some text), that remembered location is now no longer referring to the start of that next command. When the interpreter tries to read the next 'line' it will instead pick up a different line, or possibly part of a line depending on how much text was inserted or removed. If you're lucky, it will probably not be able to process whatever word it happen to land on, give an error and continue to execute from the next line - but still probably not what you want.
However, with understanding of what's going on, you can structure your scripts to reduce the risk. I have scripts that implement a simply menu system, by displaying a menu, accepting input from the user using the choice command and then processing the selection. The trick is to ensure that the point where the script waits for input is near the top of the file, so that any edits you might wish to make will occur after that point and so have no nasty impacts.
Example:
:top
call :displayMenu
:prompt
REM The script will spend most of its time waiting here.
choice /C:1234 /N "Enter selection: "
if ERRORLEVEL == 4 goto DoOption4
if ERRORLEVEL == 3 goto DoOption3
if ERRORLEVEL == 2 goto DoOption2
goto DoOption1
:displayMenu
(many lines to display menu)
goto prompt
:DoOption1
(many lines to do Option 1)
goto top
:DoOption2
(many lines to do Option 2)
goto top
(etc)

Resources