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
)
Related
As specified in the Question title, I have the following DIR command to get most recently modified files in any directory provided the command or script with this command in running in the same directory whose files list is to be filtered.
dir /b /o:-d
But the quirk here is that I want to limit the number of files in the most-recently-modified files list such that I can run for loop on each. This list needs to be generated without using multiple FOR loops, which I can't think of any way to figure out. I can loop thro' the whole list like below:
for /F "delims=" %%a in ('dir /b /o:-d') do echo %%a
How do I reliably stop this loop strictly when most recent n files are already listed ??
I suggested you could use a prior example and modify it to accept 6 such as below where you can add a number via command line, you attempted to make a change but would be better off using this version with !count! rather than trying %%
Latest.cmd
usage: latest 6
#ECHO OFF
setlocal EnableDelayedExpansion
set "j=0"
set "count=%1"
FOR /f "delims=" %%i IN ('dir /b /a-d /o-d *.*') DO (
echo %%i
set /A j=j+1
if !j! geq !count! (
goto :end
)
)
:end
For a single line showing first 6 but continuing without break that translates to
cmd /V:ON /c "set "latest=6" & set "j=0" & echo/ & for /f "tokens=*" %F in ('dir /b /a-d /o-d *.*') do #set /A "j=!j!+1" >nul & if !j! leq !latest! echo %F"
A much simpler version for break after say 6 files would be a modification to dbenham's answer
#echo off
set n=0
set count=6
for /f "usebackq delims==" %%F in (`dir /b /a-d /o-d *.*`) do (
echo %%F
set /a "n+=1, 1/(%count%-n)" 2>nul || goto :break
)
:break
set n=
set count=
For a cmd one line command it needs to be a bit more convoluted Change the 6 towards the end, to your desired number. (I am sure others will do better! since I am nesting cmd from the cmd line)
cmd/r "#echo off&echo/&for /f "usebackq tokens=*" %F in (`dir /b /a-d /o-d *.*`)=do=(echo=%F&set /a "n+=1,1/(6-n)" 1>nul=2>nul||(exit /b))"
Another way to do it in a cmd batch-file.
SET "FILECOUNT=6"
powershell -NoLogo -NoProfile -Command ^
(Get-ChildItem -File ^| Sort-Object -Property LastWriteTime ^| Select-Object -Last %FILECOUNT%).Name
i'm working in cmd and i need a little help. I want to modify this code (which is working) :
#echo off
for /f %%a in ('findstr /s /m %3 "%1*.%2"') do (
findstr /S /R /N "^" "%%a" | find /C ":"
)
So, this code has 3 parameters (1.path, 2.extension and 3. string) and only searches for files that contain that string. The search is conducted in the directory sent as parameter and all the subfolders. This code will show how many lines each file has only if the file contains the string sent as parameter.
Now i want to modify this code in a way that i can send 4th parameter which is a number, and i want it to (obviously echo YES!) only if the number of lines in file is greater than the number sent as parameter. However, my code down below isn't working (i have tried many solutions which includes ( [ " ' etc. ) but i can't get it to work.
#echo off
for /f %%a in ('findstr /s /m %3 "%1*.%2"') do (
if findstr /S /R /N "^" "%%a" | find /C ":" gtr %4 echo YES!
)
Can you help me? :)
Here you go, made to look a little different.
This includes filters to prevent the Dir command from reading directories, system files and reparse points too:
#ECHO OFF
FOR /F "DELIMS=" %%A IN ('DIR /B /S /A:-D-S-L "%~1*.%2"') DO (
FOR /F "TOKENS=3 DELIMS=:" %%B IN ('FIND /I /C "%~3" "%%A"') DO (
IF %%B GTR %4 (
ECHO File %%A has more than %4 lines matching the string %3, it has%%B.
)
)
)
This uses FindStr as in your example, but it's a little slower:
#ECHO OFF
FOR /F "DELIMS=" %%A IN ('FINDSTR /S /M /I /C:"%~3" "%~1*.%2"') DO (
FOR /F "TOKENS=3 DELIMS=:" %%B IN ('FIND /I /C "%~3" "%%A"') DO (
IF %%B GTR %4 (
ECHO File %%A has more than %4 lines matching the string %3, it has%%B.
)
)
)
(Remember that the script was designed to accept specific format and order of input parameters).
I am making a program that checks if a user's IP is a certain IP address.
Currently, I created a successful internal IP version:
#echo off
set userIp=192.168.90.100
for /f "tokens=4 delims= " %%i in ('route print ^| find " 0.0.0.0"') do set localIp=%%i
for /f "delims=[] tokens=2" %%a in ('ping %computername% -4 -n 1 ^| findstr "["') do set thisip=%%a
goto :Check
:Check
if %localIp%==%userIp% goto :Good
if %thisip%==%userIp% goto :Good
goto :Bad
And I am trying to make the same thing that works with external IPs.
I researched online, and here is what I got so far.
#echo off
for /f "tokens=2 delims=:" %%a IN ('nslookup myip.opendns.com. resolver1.opendns.com ^| findstr /IC:"Address"') do if /i %%a=="10.11.12.13" goto :Good
goto :Bad
I need a bit of help on how to fix this.
With pure batch/already present tools:
EDIT: changed the batch to properly handle also IPv6 addresses
#Echo off
for /f "tokens=1* delims=: " %%A in (
'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%B
Echo External IP is : %ExtIP%
Reference
Another one with powershell:
#Echo off
For /f %%A in (
'powershell -command "(Invoke-Webrequest "http://api.ipify.org").content"'
) Do Set ExtIP=%%A
Echo External IP is : %ExtIP%
And another slightly different powershell variant:
#Echo off
For /f %%A in (
'powershell -nop -c "(Invoke-RestMethod http://ipinfo.io/json).IP"'
) Do Set ExtIP=%%A
Echo External IP is : %ExtIP%
To get your public IP without additional parsing do this:
curl "http://api.ipify.org"
EDIT:
This version is more reliable across windows language versions:
for /f "tokens=3 delims== " %%A in ('
nslookup -debug myip.opendns.com. resolver1.opendns.com 2^>NUL^|findstr /C:"internet address"
') do set "ext_ip=%%A"
First it seems there is a . too much after the first .com.
Second when using your command with simply google.com and echo %a I get the following:
" xx.xx.xx.x" without the quotes and with two leading spaces!
So your if will never be true!
Change it to something like this: if "%%a"==" xx.xx.xx.x" Goto:good and you should be fine.
I want to find two different strings in .txt file. I need two scripts, first script to find last row that contains these strings together and second script to find last row that contains these strings seperately. I tried to write somethings but I go off the project.
This what i have tried so far as code :
#echo off
for /f %%i in ('find /v /c "" ^< deneme.txt') do set /a lines=%%i
echo %lines%
set /a startLine=%lines% - 1
more /e +%startLine% deneme.txt > temp.txt
find "ali" temp.txt|find "veli"
del temp.txt
Thanks for help.
#echo off
set "string1=ali"
set "string2=veli"
set "file=deneme.txt"
for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i /v "\<%string2%\>" ') do set "out1=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string2%\>" %file% ^|findstr /i /v "\<%string1%\>" ') do set "out2=%%a"
for /f "delims=" %%a in ('findstr /i "\<%string1%\>" %file% ^|findstr /i "\<%string2%\>" ') do set "out3=%%a"
echo last line with %string1%: "%out1%"
echo last line with %string2%: "%out2%"
echo last line with both: "%out3%"
for explanations, see for /? and findstr /?
I have a local server on my Windows XP Virtual Box.
Every time my main network changes like from wifi to cable or I move to a different network the IP of my Win XP box changes.
I want to run a batch file to map the new IP to some common name in my hosts file
C:\WINDOWS\system32\drivers\etc\hosts
for example if I have now:
10.97.100.74 www.myAppServer.com
and if the ip changes to 192.168.11.9,
I want to search for the string myAppServer in the hosts file and replace the whole line with
192.168.11.9 www.myAppServer.com
I was able to get the current IP using:
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
set IP=%IP%
but how do I find that line and replace it?
I found this on DOS Tips
BatchSubstitute - parses a File line by line and replaces a substring
#echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
::BatchSubstitute - parses a File line by line and replaces a substring
::syntax: BatchSubstitute.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
::$changed 20100115
::$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)
If you want to replace just one line, you can do it this way:
#echo off
setlocal
cd "C:\WINDOWS\system32\drivers\etc"
for /F "delims=:" %%a in ('findstr /N "myAppServer" hosts') do set lineNum=%%a
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" hosts') do (
if %%a equ %lineNum% (
echo 192.168.11.9 www.myAppServer.com
) else (
echo %%a
)
)) > hosts.new
This program eliminate empty lines and will have problems if the hosts file contain Batch special characters, like | > < & !. These details may be fixed, if needed.
The simplest way of doing it is not replacing it. Remove it and add a new line.
If you have your ip address in %IP%, then
set "hostsFile=%systemroot%\system32\drivers\etc\hosts"
(
findstr /v /i /c:"www.MyAppServer.com" "%hostsFile%"
echo(
echo %IP% www.MyAppServer.com
) > "%temp%\hosts"
move /y "%temp%\hosts" "%hostsFile%" >nul 2>nul
And be sure you have rights enough to change the hosts file and that no antivirus is blocking changes to it.
this get the job done finally:
#echo off
REM Set a variable for the Windows hosts file location
set "hostpath=%systemroot%\system32\drivers\etc"
REM set "hostpath=C:\projectStar\Programs\etc"
set "hostfile=hosts"
REM Make the hosts file writable
attrib -r -s -h "%hostpath%\%hostfile%"
for /f "skip=1 delims={}, " %%A in ('wmic nicconfig get ipaddress') do for /f "tokens=1" %%B in ("%%~A") do set "IP=%%~B"
REM echo %IP%
set IP=%IP%
move /y "%hostpath%\%hostfile%" "Hs.txt"
findstr /v/c:"www.nexsoftnetwork.com" Hs.txt > "Hss2.txt"
echo %IP% www.nexsoftnetwork.com >>"Hss2.txt"
move /y "Hss2.txt" "%hostpath%\%hostfile%"
echo Done.
pause
thank you all for your inputs.
But I wanted to hit this server from my phone which will be on the same network as the server. and eventually I came to notice that unless I modify the hosts file of the host operating system or the mobile phone it won't still solve my problem. this solution works only if I want to hit this server with in the virtual box.