Windows cmd one-liner that finds the default gateway and pings it
2014-07
Want to create a one-liner to:
- Obtain an address via DHCP
- Retrieve the default gateway
- Ping the default gateway
I am partially there. I do the DHCP renewal and am able to extract the gateway by doing the following
> ipconfig /renew * > nul & ipconfig | findstr "Default Gateway" | findstr /o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"
I then perform the ping
. I would rather do it all in one line. However, I cannot figure out how to extract just the gateway from this command.
Hate to answer my own question so quickly but I think I got it:
> FOR /F "tokens=13" %x IN ('"ipconfig /renew * > nul & ipconfig | findstr "Default Gateway" | findstr "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*""') DO ping -t %x
Leaving open. Maybe someone knows a more elegant way?
I'm a noob trying to figure out how to simplify this .bat file I'm creating (sorts out movies into another alphabetized folder) so that it will have far fewer lines. I want to get it to use loop(s) that cycles through the whole alphabet and than numbers 0-9. Any help on this would be greatly appreciated.
setlocal EnableDelayedExpansion
::REN Need this for loop with !var! I think
TITLE Automated Symlink Session
set alphachar=A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
:SYMSTART
REN Change directory than find all files in "C:\Movies\" than output to the file named list
cd /d "C:\Movies\"
Dir A* /b > list
REN for each line of the file named list do an mklink and output it to Categorized folder under corresponding letter
for /F "usebackq delims==" %%i in ("C:\Movies\list") do set &(mklink /d "C:\Categorized\A\%%i" "C:\Movies\%%i")
Dir B* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\B\%%i" "C:\Movies\%%i")
Dir C* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\C\%%i" "C:\Movies\%%i")
Dir Z* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\Z\%%i" "C:\Movies\%%i")
Dir 1* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\#\%%i" "C:\Movies\%%i")
Dir 9* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\#\%%i" "C:\Movies\%%i")
Dir 0* /b > list
for /F "usebackq delims==" %%i in ("C:\Movies\list") do (mklink /d "C:\Categorized\#\%%i" "C:\Movies\%%i")
REN Clean up
del "C:\Movies\list"
:SYMEND
ECHO Symlinking Complete
Pause
To simply print a-z and then 0-9:
@echo off
setlocal enabledelayedexpansion
set loop=abcdefghijklmnopqrstuvwxyz0123456789
for /l %%i in (0,1,35) do echo !loop:~%%i,1!
To save the loop counter to a variable so you can access it:
@echo off
setlocal enabledelayedexpansion
set loop=abcdefghijklmnopqrstuvwxyz0123456789
for /l %%i in (0,1,35) do (
set lc=!loop:~%%i,1!
echo !lc!
)
Will this do what you want. Obviously set local That is all one line. sorry I am new here, but have written batch files for 25 years now.
For %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9) do For /F "usebackq delims==" %%k in ('dir C:\Movies\%%i* /b') do (mklink /d "C:\Categorized\%%i\%%k" "C:\Movies\%%k")
I finally ended up giving up on using the For statement inside the set part of the command line. No matter what I did it didn't work. This is what I ended up with for anyone trying to do the same thing.
For %%M in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #) do (mkdir "C:\My Videos\Categorized\"%%M)
For %%N in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ((Dir "G:\My Videos\TV Shows\"%%N* /b) >> "C:\My Videos\List\%%N.lst")
For %%O in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (For /F "usebackq delims==" %%P in ("C:\My Videos\List\%%O.lst") do (mklink /d "G:\My Videos\Categorized\%%O\%%P" "G:\My Videos\TV Shows\%%P"))
For %%L in (0 1 2 3 4 5 6 7 8 9) do ((Dir "G:\My Videos\TV Shows\"%%L* /b) >> "G:\My Videos\List\#.lst")
For /F "usebackq delims==" %%Q in ("G:\My Videos\List\#.lst") do (mklink /d "G:\My Videos\Categorized\#\%%Q"
Perhaps this can be even shorter but it's much better than the 36+ lines I had to deal with before
This is what I was trying to do, but unless some else can show one that does actually work I don't think it does
FOR /D %variable IN (FOR /D %variable IN (set) DO command [command-parameters]) DO command [command-parameters]