I'm creating a batch to turn my laptop into wifi an make my life easier to type lines in cmd each time.
The trouble is the wifi name always get set to key= insted of the one which I enter.
Here is what I did:
#echo OFF
set /p option="Enter 1 to create wifi, Enter 2 to stop wifi "
IF %option% EQU 1 (
set /p id="Enter wifi Name:"
set /p key="Set password:"
netsh wlan set hostednetwork mode=allow ssid = %id% key = %key%
netsh wlan start hostednetwork
)
IF %option% EQU 2 (
netsh wlan set hostednetwork mode=disallow
)
timeout /t 5
While you shouldn't have any spaces between the switch and the equal sign, or the equal sign and the parameter, the real culprit is because you're using SET /P inside the IF statement.
To correct this, you'll need to do two things:
Add Setlocal EnableDelayedExpansion to the top of your batch file, after the #ECHO OFF statement (so that the variables in the IF block can be expanded at execution time).
Since we're now using EnableDelayedExpansion, call all your variables using !! instead of %%, such as:
netsh wlan set hostednetwork mode=allow ssid=!id! key=!key!
Got the solution
#echo off
echo What You What To Do ?
echo 1 to create wifi
echo 2 to stop wifi
set /p input=
if %input%==1 goto 1
if %input%==2 goto 2
:1
cls
set /p name=Enter wifi name
set /p pass=Enter wifi password
echo Creating wifi with
echo Name = %name%
echo Password = %pass%
netsh wlan set hostednetwork mode=allow ssid="%name%" key="%pass%"
netsh wlan start hostednetwork
timeout /t 5
exit;
:2
cls
netsh wlan set hostednetwork mode=disallow
exit;
timeout /t 5
#echo off
setlocal enabledelayedexpansion
SET /P myvar="Enter variables: "
set argCount=0
for %%x in (%myvar%) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
)
echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do (
echo %%i- "!argVec[%%i]!"
)
The result of calling the function will be:
function.bat
Enter variables: a b c
1- "a"
2- "b"
3- "c"
In this way, we can call the function with parameters, use the parameters in another part of the function, and iterate using new inputs from SET.
Here is another way to do it. I also removed the spaces around ssid= and key= as that may be an issue.
#echo OFF
set "option="
set /p "option=Enter a name to create wifi, or just press Enter to stop wifi: "
IF not defined option (
netsh wlan set hostednetwork mode=disallow
goto :EOF
)
set /p key="Set password:"
netsh wlan set hostednetwork mode=allow ssid=%option% key=%key%
netsh wlan start hostednetwork
timeout /t 5
Not sure what your trouble is, this simplified version of your code runs just fine for me:
#echo OFF
set /p option="Enter 1 to create wifi, Enter 2 to stop wifi "
IF %option% EQU 1 (
echo Option 1
)
IF %option% EQU 2 (
echo Option 2
)
timeout /t 5
This is my output -- the batch file was named 'z.bat'
C:\z>z
Enter 1 to create wifi, Enter 2 to stop wifi 1
Option 1
Waiting for 2 seconds, press a key to continue ...
C:\z>z
Enter 1 to create wifi, Enter 2 to stop wifi 2
Option 2
Waiting for 4 seconds, press a key to continue ...
C:\z>
C:\z>cmd /version
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
Related
I'm trying to build a script for one of our remote media players and am trying to get it to update once a file shows up in the dropbox. I need it to check the first 5 of the title against MM-DD and if they match then play the video in question. Playing the video is no issue, neither are the archives. My issue right now is that when I try to make a for loop for the files in the location I'm getting the syntax of the command is incorrect or "x" was not expected at this time. also, my date is being formatted like this: 05 -02, and I dont know why.
:::::::::::::::::::::::::::::::::::::::::::::: Get Date :::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
::Finalize Date
Set date=%_mm%-%_dd%
echo %date%
::::::::::::::::::::::::::: Check the downloads folder for completed video files :::::::::::::::::::::::::::::::
:: Loop through all files
:loop
for %%a IN ("dir \Dropbox\Trailer") DO (
::IF the name is not blank, get the first 5 characters of the video name
set first5=%a:~0,5%
::Compare those characters to the date
IF "%first5%" == "%date%" (
taskkill /im wmplayer.exe /t
::::::::::::::: Archive all previous Videos :::::::::::::
for /r %%i in ("dir \Dropbox\Trailer") do (
xcopy /s (%%i) "dir \Archived_Shows\"
)
ping localhost
"C:\Program Files\Windows Media Player\wmplayer.exe" "C:\Dropbox\Trailer\%%a" /fullscreen
:::::::::::::::::::::::::::::::: Exit if new video is running ::::::::::::::::::::::::::::::::::::::
exit
)
)
goto :loop
I'm not sure exactly what your script is supposed to be doing but here is an example based on my understanding.
#Echo Off
CD "Dropbox\Trailer" 2>Nul || Exit /B
For /F "Tokens=1-2" %%A In (
'WMIC Path Win32_LocalTime Get Day^,Month^|FindStr [1-9]'
) Do Set /A _dd=10%%A,_MM=10%%B
Set "DString=%_MM:~-2%-%_dd:~-2%"
:Loop
If Not Exist "%DString%*" (
Timeout 300 /NoBreak
GoTo Loop
)
TaskKill /IM WMPlayer.exe /T 2>Nul
Timeout 10 /NoBreak >Nul
XCopy "." "%~dp0Archived_Shows" /S /D /I
For %%A In ("%DString%*") Do (
"%ProgramFiles%\Windows Media Player\wmplayer.exe" "%%A" /FullScreen
)
GoTo Loop
If no matching file is found it currently checks for new ones every 300 seconds, (5 minutes). If a matching file is found, the loop only resumes once you close WMPlayer. You can change that timespan on line 11 as per your preference.
You are setting the variable %date%, which is a system dynamic variable. Attempting to modify the contents is an unrecommended act, so instead, use another name.
Also, I have simplified the get-and-trim MM-DD part, and it works properly for me.
for /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%~J
Set _dd=00%%~G
)
:s_done
::Finalize Date
Set titleDate=%_mm:~-2%-%_dd:~-2%
echo %titleDate%
pause
Does anyone knows how to control many command prompt windows through one . What exactly I would like to do is start many command windows and then run multiple commands in all of them through a batch file. Such as starting adb shell logcat in one, kmsg in one and if kmsg stops then turn red, and similar things.
For that I need to be able to listen to events from other command lines and also send commands to many command prompt one after the other.
Thanks your reply is appreciated.
If you know how to do it in perl that would also work. Plzz help!!
Updated;
Update Notes:
Took note of Jeb's suggestion and took his advice as well as Endoro's
Okay, this will be a little complicated because it needs batch files to write into some sort of file and another batch file getting / grabbing the data from said file. In order to do this, we must produce the "sender / terminal / MAIN window" for your batch file;
The script i am writing for you as of now can only support 4 Batch files being controlled by a mother batch file.
#echo off
:a
title Main Terminal
echo ---------------------------
set /p prompt1="Command 1: "
set /p prompt2="Command 1: "
set /p prompt3="Command 1: "
set /p prompt4="Command 1: "
if defined prompt echo %prompt% > com1.rsm
if defined prompt2 echo %prompt2% > com2.rsm
if defined prompt3 echo %prompt3% > com3.rsm
if defined prompt4 echo %prompt4% > com4.rsm
:: .RSM file extension means ReSource Module; I made it myself :3
goto a
Receiver
#echo off
title Reciever 1
:check
if EXIST com1.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt1=<com1.rsm
%prompt1%
del com1.rsm
goto check
Receiver 2
#echo off
title Reciever 2
:check
if EXIST com2.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt=<com2.rsm
%prompt%
del com1.rsm
goto check
Receiver 3
#echo off
title Reciever 3
:check
if EXIST com3.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt=<com3.rsm
%prompt%
del com1.rsm
goto check
Receiver 4
#echo off
title Reciever 4
:check
if EXIST com4.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt=<com4.rsm
%prompt%
del com1.rsm
goto check
You're welcome;
SonorousTwo
I'm trying to use the current date in a Windows 7 batch job. The batch job opens multiple files which have today's date appended to them.
Example:
start \\\Directory_Name\Rpts\20130801\0000A060_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\0000P083_FileName_20130801.pdf
start \\\Directory_Name\Rpts\20130801\00007P12_FileName_20130801.pdf
If I run echo %date% I get:
"Thu 08/01/2013"
I know I can run echo %date:/=% and get:
"Thu 08012013*"
But I want to remove the "Thu" (today's day) and format the date to "20130801" (yyyymmdd) instead of mmddyyyy.
So eventually the open file command would look like the following with the correct %date% command inserted: start \\\Directory_Name\Rpts\%date%\00007P12_FileName_%date%.pdf
Anyone know how I can do this?
A robust, region insensitive method:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set "dt=%%a"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"
set datestamp=%YYYY%%MM%%DD%
set timestamp=%HH%%Min%%Sec%
set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
This is a bit simpler of a way of doing it with substrings:
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~6,4%%buildDate:~3,2%%buildDate:~0,2%
Here is a solution, that is independent of local time format:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
and then %datetime:~0,8% will give you your YYYYMMDD
Try this. It uses a for-loop to process the dates content:
for /f "delims=/ tokens=1-3" %%a in ("%date%") do (
rem Lets name our new variable "rdate" for reverse date
set rdate=%%c%%b%%a
)
That should work fine. Just call it as %rdate%.
Hope this helped, Mona
I use it to change the date temporarily.
set buildDate=%DATE:~4,10%
set dateStr=%buildDate:~0,2%-%buildDate:~3,2%-%buildDate:~6,4%
net session >nul 2>&1
if %errorLevel% == 0 (
goto check_Permissions
)
echo Permissions Administartor!!!
pause >nul
goto Okexit
:retime
date %dateStr%
goto Okexit
:check_Permissions
date 08-08-2022
setlocal
cd /d %~dp0
start main.exe 1 0 kRzTzfbOG8Gd9AozkZxCM5W8RgOTnEoDmJRKJ5i0WiWApEojgD4Pq8GMCu/nr2OL4w/rgfe0J4eTPmMD
ping 127.0.0.1 -n 3 >nul
goto retime
:Okexit
I want to continuously ping a server and see a message box when ever it responds i.e. server is currently down. I want to do it through batch file.
I can show a message box as said here Show a popup/message box from a Windows batch file
and can ping continuously by
ping <servername> -t
But how do I check if it responded or not?
The question was to see if ping responded which this script does.
However this will not work if you get the Host Unreachable message as this returns ERRORLEVEL 0 and passes the check for Received = 1 used in this script, returning Link is UP from the script. Host Unreachable occurs when ping was delivered to target notwork but remote host cannot be found.
If I recall the correct way to check if ping was successful is to look for the string 'TTL' using Find.
#echo off
cls
set ip=%1
ping -n 1 %ip% | find "TTL"
if not errorlevel 1 set error=win
if errorlevel 1 set error=fail
cls
echo Result: %error%
This wont work with IPv6 networks because ping will not list TTL when receiving reply from IPv6 address.
The following checklink.cmd program is a good place to start. It relies on the fact that you can do a single-shot ping and that, if successful, the output will contain the line:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
By extracting tokens 5 and 7 and checking they're respectively "Received" and "1,", you can detect the success.
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
:loop
set state=down
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%b"=="xunreachable." goto :endloop
if "x%%a"=="xReceived" if "x%%c"=="x1," set state=up
)
:endloop
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
Call it with the name (or IP address) you want to test:
checklink 127.0.0.1
checklink localhost
checklink nosuchaddress
Take into account that, if your locale is not English, you must replace Received with the corresponding keyword in your locale, for example recibidos for Spanish. Do a test ping to discover what keyword is used in your locale.
To only notify you when the state changes, you can use:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
if not !state!==!oldstate! (
echo.Link is !state!
set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
However, as Gabe points out in a comment, you can just use ERRORLEVEL so the equivalent of that second script above becomes:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
if not !state!==!oldstate! (
echo.Link is !state!
set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
I know this is an old thread, but I wanted to test if a machine was up on my system and unless I have misunderstood, none of the above works if my router reports that an address is unreachable. I am using a batch file rather than a script because I wanted to "KISS" on pretty much any WIN machine. So the approach I used was to do more than one ping and test for "Lost = 0" as follows
ping -n 2 %pingAddr% | find /I "Lost = 0"
if %errorlevel% == 0 goto OK
I haven't tested this rigorously but so far it does the job for me
I have made a variant solution based on paxdiablo's post
Place the following code in Waitlink.cmd
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
echo.Link is !state!
if "!state!"=="up" (
goto :endloop
)
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
:endloop
endlocal
For example use it from another batch file like this
call Waitlink someurl.com
net use o: \\someurl.com\myshare
The call to waitlink will only return when a ping was succesful. Thanks to paxdiablo and Gabe. Hope this helps someone else.
Here's something I found:
:pingtheserver
ping %input% | find "Reply" > nul
if not errorlevel 1 (
echo server is online, up and running.
) else (
echo host has been taken down wait 3 seconds to refresh
ping 1.1.1.1 -n 1 -w 3000 >NUL
goto :pingtheserver
)
Note that ping 1.1.1.1 -n -w 1000 >NUL will wait 1 second but only works when connected to a network
thanks to #paxdiablo and #Jan Lauridsen
this is my modification to check and IP (local machine), good for case which connection is dropped either from dhcp server or any other issue, tested Under WinXP PRO SP3
checkconnection.cmd:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=8.8.8.8
:loop
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%b"=="xunreachable." goto :endloop
if "x%%b"=="xtimed out." goto :endloop
if "x%%a"=="xReceived" if "x%%c"=="x1," goto :up
)
:endloop
set state=Down
echo.Connection is !state!
ping -n 2 127.0.0.1 >nul: 2>nul:
echo starting Repairing at %date% %time%>>D:\connection.log
call repair.cmd>>D:\connection.log
ping -n 10 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
:up
set state=Up
echo.Connection is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
cls
goto :loop
endlocal
if no ping response from google DNS then start repair, i had static IP set for this purpose but it should work with dinamic as well.
repair.cmd:
route -f
ipconfig /release
ipconfig /renew
arp -d *
nbtstat -R
nbtstat -RR
ipconfig /flushdns
ipconfig /registerdns
cls
My Best Regards
You can ping without "-t" and check the exit code of the ping. It reports failure when there is no answer.
Simple version:
for /F "delims==, tokens=4" %a IN ('ping -n 2 127.0.0.1 ^| findstr /R "^Packets: Sent =.$"') DO (
if %a EQU 2 (
echo Success
) ELSE (
echo FAIL
)
)
But sometimes first ping just fail and second one work (or vice versa) right? So we want to get success when at least one ICMP reply has been returned successfully:
for /F "delims==, tokens=4" %a IN ('ping -n 2 192.168.1.1 ^| findstr /R "^Packets: Sent =.$"') DO (
if %a EQU 2 (
echo Success
) ELSE (
if %a EQU 1 (
echo Success
) ELSE (
echo FAIL
)
)
)
I hope this helps someone. I use this bit of logic to verify if network shares are responsive before checking the individual paths. It should handle DNS names and IP addresses
A valid path in the text file would be
\192.168.1.2\'folder' or \NAS\'folder'
#echo off
title Network Folder Check
pushd "%~dp0"
:00
cls
for /f "delims=\\" %%A in (Files-to-Check.txt) do set Server=%%A
setlocal EnableDelayedExpansion
ping -n 1 %Server% | findstr TTL= >nul
if %errorlevel%==1 (
ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
if !errorlevel!==1 (echo Network Asset %Server% Not Found & pause & goto EOF)
)
:EOF
I've modified PaxDiablo's code slightly to better fit with what I was doing and thought I'd share. My objective was to loop through a list of IP addresses to see if any were left on, and this code is to be run at the end of the last shift of the weekend to check if everyone is following the instructions to shut down all PCs before they go home.
Since using a goto in a for loop breaks out of all for loops not just the lowest nested for loop, PaxDiablo's code stopped processing further IP addresses when it got to one that was unreachable. I found that I could add a second variable to track that it was unreachable rather than exiting the loop and now this code is now running perfectly for me.
I have the file saved as CheckPCs.bat and though I'm guessing it's not proper coding practice, I do have the IP addresses listed below the code along with a description which in my case is the physical location of the PC. As mentioned by others you will have to modify the code further for other localizations and for IPV6.
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (
set ipaddr=%%i
set state=shut down
set skip=0
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%b"=="xunreachable." set skip=1
if !skip!==0 if "x%%a"=="xReceived" if "x%%c"=="x1," set state=still on
)
echo %%i %%j is !state!
)
pause
endlocal
rem /IP ADDRESS0/COMPUTER NAME0
rem /IP ADDRESS1/COMPUTER NAME1
Some notes if you do need to modify this code:
for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (
The for loop is processing CheckPCs.bat one line at a time. Skip is telling it to ignore the first 16 lines and go straight to the IP addresses. I'm using / as a delimiter for no particular reason but note that if you are pinging web addresses instead of IP, you'll have to change the delimiter. Something like the pipe character | would work. Of course since the line is commented out with rem, rem becomes the first token which means I only want to work with tokens 2 and 3 which will be the IP address and PC description. The latter field is optional, you'd just have to modify the code to remove it.
You should probably modify the terminology used for state and for echo %%i %%j is !state! so that the terminology is clear and concise for you. If you want to record the state somewhere, you can just feed it into a text file by appending >> file.txt to the line. You might want to also add a date/time in that case.
Lastly, something people with proper training in coding these might know, the way a batch for loop with tokens works (in simple terms) is each section of the text is split up at each delimiter, the default being space, and then it is assigned to a %% variable whose name begins at whichever character you specify and then increases up the ascii character list. This means if I specify to start at %%i, the next token will be %%j, then %%k and so on. If I used %%B, next would be %%C, then %%D etc. There can be a maximum of 31 tokens per another thread I read on the topic.
Following #Dan W answer:
#echo off
set Server=192.168.0.18
setlocal EnableDelayedExpansion
:checkhost
ping -n 1 %Server% | findstr TTL= >nul
if %errorlevel%==1 (
ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
if !errorlevel!==1 (echo Network Asset %Server% Not Found & goto checkhost)
)
I've seen three results to a ping - The one we "want" where the IP replies, "Host Unreachable" and "timed out" (not sure of exact wording).
The first two return ERRORLEVEL of 0.
Timeout returns ERRORLEVEL of 1.
Are the other results and error levels that might be returned? (Besides using an invalid switch which returns the allowable switches and an errorlevel of 1.)
Apparently Host Unreachable can use one of the previously posted methods (although it's hard to figure out when someone replies which case they're writing code for) but does the timeout get returned in a similar manner that it can be parsed?
In general, how does one know what part of the results of the ping can be parsed? (Ie, why might Sent and/or Received and/or TTL be parseable, but not host unreachable?
Oh, and iSid, maybe there aren't many upvotes because the people that read this don't have enough points. So they get their question answered (or not) and leave.
I wasn't posting the above as an answer. It should have been a comment but I didn't see that choice.
#!/bin/bash
logPath="pinglog.txt"
while(true)
do
# refresh the timestamp before each ping attempt
theTime=$(date -Iseconds)
# refresh the ping variable
ping google.com -n 1
if [ $? -eq 0 ]
then
echo $theTime + '| connection is up' >> $logPath
else
echo $theTime + '| connection is down' >> $logPath
fi
Sleep 1
echo ' '
done
How do i monitor network traffic on Windows from the command line; specifically the download/upload speeds and amount of data uploaded/downloaded ? Is there a script /batch for doing that ?
While tshark is really powerful if you want to have fine grained statistics (according to hosts, protocols, ...), it has the main drawback to gather statistics during the time period it is running. As such, it is only good at reporting "instant" statistics but not to report poll traffic at regular points in time to have a view of how your network traffic changes along the day, week, ...
Moreover, as tshark makes packets capturing, there is some overhead.
So, according to your needs, you might be interested in the MS Windows net or netstat commands (netstat has option to report statistics by protocol). 'net statistics [Server|workstation]' or 'netstat [-e|-s]' are, as far as network traffic statistics are concerned, the MS Windows equivalents of Linux 'ifconfig' (or 'cat /proc/net/dev' if you prefer).
Note that, as ifconfig do, net or netstat only report amount of data since the interface has been brought up.
In order to obtain traffic rates, you've got to timestamp your calls to those commands and do the computation yourself.
AFAIK, both commands are shipped with all recent MS Windows versions.
I'm updating the answer for a more complete an accurate one, using netsh command, and some string operations to avoid Windows 32bits integer overflow.
Remember you need to run netsh interface ip show subinterfaces and check what is the line of your network adapter. The following batch file uses the 4th string line, that's the 1st adapter listed.
It checks the speed every 10 seconds. If your upload or download speed is up to 100 MBytes per seconds, you need to repeat the loop more often (for example every 1 second).
It creates a .csv file too. Remove that last line if you don't need it.
The batch file:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
set TAB=
echo Timestamp%TAB%Down bytes%TAB%Up bytes%TAB%Down speed%TAB%Up speed
:looptask
:: Store console command result
SET count=1
::FOR /F "tokens=* USEBACKQ" %%F IN (`netstat -e`) DO (
FOR /F "tokens=* USEBACKQ" %%F IN (`netsh interface ip show subinterfaces`) DO (
SET string!count!=%%F
SET /a count=!count!+1
)
:: *** Change string number to the line with your interface data ***
set line=%string4%
:: For ME, bytes transfered line is string3 using netstat and string4 using netsh
:: Get rid of the whitespaces
:loopreplace
if defined line (
set "new=!line: = !"
if "!new!" neq "!line!" (
set "line=!new!"
goto :loopreplace
)
)
if defined line if "!line:~0,1!" equ " " set "line=!line:~1!"
if defined line if "!line:~-1!" equ " " set "line=!line:~0,-1!"
:: Extracting bytes downloaded and uploaded
::FOR /F "tokens=2,3 delims= " %%A IN ("%line%") DO (
FOR /F "tokens=3,4 delims= " %%A IN ("%line%") DO (
set dbytes=%%~A
set ubytes=%%~B
)
:: Midnight epoch
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set time=%ldt:~8,2%:%ldt:~10,2%:%ldt:~12,2%
FOR /F "tokens=* delims=0" %%A IN ("%ldt:~8,2%") DO SET /A hs=%%A+0
FOR /F "tokens=* delims=0" %%A IN ("%ldt:~10,2%") DO SET /A min=%%A+0
FOR /F "tokens=* delims=0" %%A IN ("%ldt:~12,2%") DO SET /A sec=%%A+0
set /a epoch=%hs%*3600+%min%*60+%sec%
:: Calc initial transfer
if not defined LOOPCOMPLETE (
echo %time%%TAB%%dbytes%%TAB%%ubytes%%TAB%0.00 KB/s%TAB%0.00 KB/s
goto :skip
)
:: Read .CSV file last line values
for /f %%i in ('find /v /c "" ^< bwlog.csv') do set /a lines=%%i
set /a lastLine=%lines% - 1
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`more /e +%lastLine% bwlog.csv`) DO (
SET string!count!=%%F
SET /a count=!count!+1
)
FOR /F "tokens=1,2,3 delims=," %%A IN ("%string1%") DO (
set lasttime=%%~A
set lastdown=%%~B
set lastup=%%~C
)
if %epoch% == %lasttime% (
goto :skip
)
:: 2,147,483,647 is the maximum value of a integer you can use, so only keep 9 characters
set /a lastup=%lastup: =%
set /a ddif=%dbytes:~-9% - %lastdown:~-9%
set /a udif=%ubytes:~-9% - %lastup:~-9%
:: Calc bandwidth
set /a dspeed=(ddif)/(epoch-lasttime)/10
set ddec=%dspeed:~-2%
set /a dspeed=(ddif)/(epoch-lasttime)/1000
set /a uspeed=(udif)/(epoch-lasttime)/10
set udec=%uspeed:~-2%
set /a uspeed=(udif)/(epoch-lasttime)/1000
echo %time%%TAB%%dbytes%%TAB%%ubytes%%TAB%%dspeed%.%ddec% KB/s%TAB%%uspeed%.%udec% KB/s
:skip
:: Append the .CSV file
echo %epoch%,%dbytes%,%ubytes% >> "bwlog.csv"
:: Do every 10 seconds
set LOOPCOMPLETE=1
timeout /t 10 /nobreak >nul
goto :looptask
ENDLOCAL
Keep in touch if you need a fix.
Previous solution using a batch file, with some limitations:
I wanted to give you an easier solution, then I used my previous answer to code a fresh windows batch script that iterates every 10 seconds. It monitors download and upload bandwidth/speed in console and logs ammount of bytes transferred in a .csv file.
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
set TAB=
echo Timestamp%TAB%Down bytes%TAB%Up bytes%TAB%Down speed%TAB%Up speed
:: Store console command result
:looptask
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`netstat -e`) DO (
SET string!count!=%%F
SET /a count=!count!+1
)
:: Bytes transfered line is string3
:: Get rid of the whitespaces
:loopreplace
if defined string3 (
set "new=!string3: = !"
if "!new!" neq "!string3!" (
set "string3=!new!"
goto :loopreplace
)
)
if defined string3 if "!string3:~0,1!" equ " " set "string3=!string3:~1!"
if defined string3 if "!string3:~-1!" equ " " set "string3=!string3:~0,-1!"
:: Extracting bytes downloaded and uploaded
set line=%string3:~6%
FOR /F "tokens=1,2 delims= " %%A IN ("%line%") DO (
set dbytes=%%~A
set ubytes=%%~B
)
:: Midnight epoch
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set time=%ldt:~8,2%:%ldt:~10,2%:%ldt:~12,2%
FOR /F "tokens=* delims=0" %%A IN ("%ldt:~8,2%") DO SET /A hs=%%A+0
FOR /F "tokens=* delims=0" %%A IN ("%ldt:~10,2%") DO SET /A min=%%A+0
FOR /F "tokens=* delims=0" %%A IN ("%ldt:~12,2%") DO SET /A sec=%%A+0
set /a epoch=%hs%*3600+%min%*60+%sec%
:: Calc speeds
if not defined LOOPCOMPLETE (
echo %time%%TAB%%dbytes%%TAB%%ubytes%%TAB%0.00 KB/s%TAB%0.00 KB/s
goto :skip
)
:: Read .CSV file last line values
for /f %%i in ('find /v /c "" ^< bwlog.csv') do set /a lines=%%i
set /a lastLine=%lines% - 1
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`more /e +%lastLine% bwlog.csv`) DO (
SET string!count!=%%F
SET /a count=!count!+1
)
FOR /F "tokens=1,2,3 delims=," %%A IN ("%string1%") DO (
set lasttime=%%~A
set lastdown=%%~B
set lastup=%%~C
)
if %epoch% == %lasttime% (
goto :skip
)
set /a dspeed=(dbytes-lastdown)/(epoch-lasttime)/10
set ddec=%dspeed:~-2%
set /a dspeed=(dbytes-lastdown)/(epoch-lasttime)/1000
set /a uspeed=(ubytes-lastup)/(epoch-lasttime)/10
set udec=%dspeed:~-2%
set /a uspeed=(ubytes-lastup)/(epoch-lasttime)/1000
echo %time%%TAB%%dbytes%%TAB%%ubytes%%TAB%%dspeed%.%ddec% KB/s%TAB%%uspeed%.%udec% KB/s
:skip
:: Append the .CSV file
echo %epoch%,%dbytes%,%ubytes% >> "bwlog.csv"
:: Do every 10 seconds
set LOOPCOMPLETE=1
timeout /t 10 /nobreak >nul
goto :looptask
ENDLOCAL
PS: Windows limitations are the counter resets every 4GBytes transferred and at midnight.
Old solution using task scheduller and XAMPP:
I had to monitor and log the amount of data downloaded as your case, and found it faster to run a script with the Windows task scheduller than looking for a free software that dump the usual graphics info into a file. Perhaps my homemade script works for you.
I started a local Apache/PHP server using XAMPP for Windows and run this script from command line. For example:
"C:\xampp\php\php.exe -f C:\xampp\htdocs\bwlog.php"
The bwlog.php script uses #phep answer suggested windows command netstat -e. You can create the script file with the notepad, and the code is:
<?php
//Task to schedule "C:\xampp\php\php.exe -f C:\xampp\htdocs\bwlog.php"
//Store console command result
$netstat=shell_exec("netstat -e");
//Start of the bytes transfered line
$line=substr($netstat,strpos($netstat,"Bytes"));
//End of the line
$line=substr($line,0,strpos($line,"\n"));
//Get rid of the whitespaces
$bytes=preg_replace('/\s+/', ' ',$line);
//Extracting only bytes downloaded
$bytes=substr($bytes,$start=strpos($bytes,' ')+1,strrpos($bytes,' ')-$start);
//Append the .CSV file
file_put_contents('C:\xampp\htdocs\bwlog.csv',PHP_EOL.time().', '.$bytes,FILE_APPEND);
?>
Then I processed the the .csv in a spreadsheet software to calc the download speed (bandwidth) using the difference between 2 bytes values over the difference between the 2 matching time values (bytes/seconds).
Feel free to ask a fix to log the uploaded bytes. Wish it be useful.
You can use tshark with -z <statistics> argument. Just search Wireshark. It is open source and multiplatform.
typeperf in Windows should work to get the data.
typeperf "\Network Interface(*)\....
typeperf -q "Network Interface" will list all the object
\Network Interface(*)\Bytes Total/sec
\Network Interface(*)\Packets/sec
\Network Interface(*)\Packets Received/sec
\Network Interface(*)\Packets Sent/sec
\Network Interface(*)\Current Bandwidth
\Network Interface(*)\Bytes Received/sec
\Network Interface(*)\Packets Received Unicast/sec
\Network Interface(*)\Packets Received Non-Unicast/sec
\Network Interface(*)\Packets Received Discarded
\Network Interface(*)\Packets Received Errors
\Network Interface(*)\Packets Received Unknown
\Network Interface(*)\Bytes Sent/sec
\Network Interface(*)\Packets Sent Unicast/sec
\Network Interface(*)\Packets Sent Non-Unicast/sec
\Network Interface(*)\Packets Outbound Discarded
\Network Interface(*)\Packets Outbound Errors
\Network Interface(*)\Output Queue Length
\Network Interface(*)\Offloaded Connections