Batch file ping once every minute - batch-file

I got this bat file from Stackoverflow that someone had posted back in 2014 but it will not output any info to the filename?
The file is created but without any info.....
can anyone please help, trying to record the ping output every 1 minute on a windows 7 machine.
#ECHO off
set IPADDRESS=192.168.0.1
set INTERVAL=60
:PINGINTERVAL
ping %IPADDRESS% -n 1 >> filename.txt
timeout %INTERVAL%
GOTO PINGINTERVAL

Your batch file is called ping.bat or ping.cmd and is calling itself. Rename the batch file or replace ping %IPADDRESS% ... with ping.exe %IPADDRESS% ...

The "echo off" is ok. This will only omit output from the script, not from the "ping" app.
I think the output is created in an unexpected location. Add the following lines at the beginning of the script to verify the current folder.
#echo %cd%
pause
Or simply specify the full path like so:
>> d:\fullpath\filename.txt

Related

Bat file that executes command with input from a txt file

I'm trying to execute a batch file.
I want to open a URL using that batch file, but it needs to insert whatever what is inside a txt(or any file that supports easy editable text) before opening the URL.
For example:
bat file (which i will convert into an exe for easy usage)
#echo off
start http://website.com/WHATEVERISINTHEFILEBEHINDHERE
echo Opening website.
PING 1.1.1.1 -n 1 -w 2000 >NUL. 2000
echo closing tool
The file which the bat needs to read from
steamid:123456789
Thank you.
you can use for /f to get every line of a file, but when it's sure, there is just one line (or you need just the first line of the file), there is an easier way:
#echo off
<putsteamidhere.txt set /p steamid=
start http://website.com/%steamid%
echo Opening website.
PING 1.1.1.1 -n 1 -w 2000 >NUL
echo closing tool

Batch file - Output current cmd output to log?

I know > and >> redirect a command to a file, but how can I get every line of data from the batch file? I have many commands that echo stuff, but I want just 1 that will echo every single command that's been used in the window to a text document.
Batch file:
#echo off
Choice /n /c 12
If %errorlevel%==1 echo hi
Etc..
You know what works perfectly? Right click > edit > select all. HOW THE HELL DO I DO THAT IN CODE
Say your batch script is called myScript.bat, then redirect when you call it:
myScript >log.txt
You will want to add CALL if used from within another batch script.
You can do the redirection from within your script if you CALL a main routine:
#echo off
call :main >log.txt
exit /b
:main
rem rest of your code goes here.
You probably looking for the tee command. It allows for writing to both STDOUT and a textfile at the same time.
More info here : http://linux.101hacks.com/unix/tee-command-examples/

cmd dos check if a remote FTP folder exists

I want to know how to check if FTP directory exists, using a batch dos or command.
PS:
Obviously I do not need the root of the server, but a subdirectory, otherwise I would have been enough ping it.
thanks
You could use an ftp script that tries to change to the directory and then parse the output looking for the 550 error code that says you cannot. Something like the following works for me...
#Echo off
echo open ftp.mysite.com>test.ftp
echo ftpusername>>test.ftp
echo ftppassword>>test.ftp
echo cd %1>>test.ftp
echo quit>>test.ftp
for /f %%i in ('ftp -s:test.ftp') do if {%%i} EQU {550} echo Does not exist

How to ping a server only once from within a batch file?

I want to learn how to write batch scripts and tried to create a script which automatically runs this command in the command line once:
ping www.google.de -t
and displays the ping, so it would look like this:
Reply from XXX.XXX.X.XX: time=30ms
Reply from XXX.XXX.X.XX: time=31ms
Reply from XXX.XXX.X.XX: time=29ms
My problem is, that this will result in this when I execute this command as script:
My problem is that it will not execute the ping command at all, but just insert the command unlimited times in the console window as its shown in the screenshot.
I just created a new file, wrote ping www.google.de -t in it, saved it as ping.bat file and executed it with double clicking on it.
So how to write the batch file to start this command only once and display the ping result?
I am sure you must have named the resultant bat file as "ping.bat". If you rename your file to something else say pingXXX.bat. It will definitely work. Try it out.
my batch file contains below code only
ping 172.31.29.1 -t
with file name as ping.bat
with file name abc.bat
Enter in a command prompt window ping /? and read the short help output after pressing RETURN. Or take a look on:
ping - latest Microsoft documentation for this Windows command
ping - Windows XP documentation for this Windows command
Explanation for option -t given by Microsoft:
Specifies ping continue sending echo Request messages to the destination until interrupted. To interrupt and display statistics, press CTRL+ENTER. To interrupt and quit this command, press CTRL+C.
You may want to use:
#%SystemRoot%\system32\ping.exe -n 1 www.google.de
Or to check first if a server is available:
#echo off
set MyServer=Server.MyDomain.de
%SystemRoot%\system32\ping.exe -n 1 %MyServer% >nul
if errorlevel 1 goto NoServer
echo %MyServer% is available.
rem Insert commands here, for example one or more net use to connect network drives.
goto :EOF
:NoServer
echo %MyServer% is not available yet.
pause
goto :EOF
For bash (OSX) ping google.com -c 1 (incase search brought you here)
if you want to use the name "ping.bat", a small trick is to use this code:
#echo off
cd\
ping google.com -t
Just add that "cd\" and you are fine... ;)
Not sure exactly what you are trying but your posted code should work just fine. in case you don't want the command to be displayed, add #echo off at starting of your script. If i have the below code in a file named as test.bat and run it command prompt as test.bat it will work just fine.
#echo off
ping www.google.de -t
To address your EDIT: where the main concern is ping command was not recognizable. ping command generally will be located under C:\Windows\System32\ where C:\ being the root directory. In case, the root directory is different you can get the root directory using %SystemRoot% environment variable and can say like
%SystemRoot%\Windows\System32\PING.EXE www.google.de -t
Another way to see if the command you are trying to run is recognizable or not is using WHERE command like below
where ping
If the command is recognizable; it will output the path like
C:\Windows\System32\PING.EXE
Else will result in error
I know why, you are using the file name "ping" and you are using the code "ping", it just keeps trying to run itself because its selected directory in where that file is, if you want it to actually ping, put this before the ping command: "cd C:\Windows\system32", the actual file that pings the server is in there!
From Batch file, ping a ip only once using the following command:
Ping 192.168.199.10 -n 1
i used Mofi sample, and change some parameters, no you can do -t
#%SystemRoot%\system32\ping.exe -n -1 4.2.2.4
The only thing you need to think about in this case is, in which directory you are on your computer.
Your command line window shows C:\users\rei0d\desktop\ as your current directory.
So the only thing you really need to do is:
Remove the desktop by "going up" with the command cd ...
So the complete command would be:
cd ..
ping XXX.XXX.XXX.XXX -t
Having 2 scripts called test.bat and ping.bat in same folder:
Script test.bat contains one line:
ping google.com
Script ping.bat contains below lines:
#echo off
echo Hello!
pause
Executing "test.bat" the result on CMD will be:
Hello!
Press any key to continue . . .
Why? Because "test.bat" is calling the "ping.bat" ("ping google.com" is interpreted as calling the "ping.bat" script).
Same is happening if script "ping.bat" contains "ping google.com". The script will execute himself in a loop.
Easy ways to avoid this:
Do not name your script "ping.bat".
You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".
Create a text file with text "#%SystemRoot%\system32\ping.exe -t www.google.com" and save it with extension ".bat".
Just click and run it and you will get the result.
So basically what happens is that we run ping.exe application with parameters '-t' and 'www.google.com' (web-address).
The answer to your question is this
Ping -n 1 0.0.0.0
But if you want it to be faster than this, this will be your answer
Ping -n 1 -l 1 0.0.0.0
Note: Replace 0.0.0.0 with your desired IP address
Just
write the command "ping your server IP" without the double quote. save file name as filename.bat and then run the batch file as administrator

My batch script keeps writing something in CMD though I only use ping command

My problem is that i have created a .bat file which is looking like this
#echo off
ping google.com -n 2
if not errorlevel 1 echo %date% %time% We did get a response from Google >> C:\CMD\ping.log
if errorlevel 1 echo %date% %time% We didn't get a response from Google >> C:\CMD\ping.log
It's simple and all it does is try ping google.com two times, and if we get a response log "We did get a response from google" else log "we didn't get a response from Google", both with a timestamp to the file ping.log, which is just a text file.
The Batch Script is working when i am using it on my own pc (windows 7), but when i move it the the server then when i click run on the script, it opens up CMD and just keeps writing something over and over again. I don't know exactly what it is writing, but as far as I could spot (because it keeps write in the CMD very fast), is somehting that #echo off was not reckognized. When i run the script it doesn't create or write to the files as it's supposed to.
Please anyone who can help me.
It sounds like the file encoding is set to something other than ANSI.
Open your .bat file in Notepad on the server and select File->Save As.
Save it as the same name, but make sure the encoding is set to ANSI.

Resources