Creating a bat file that runs a certain command on cmd - windows

So normally I can type this into the cmd window ping 216.52.241.254 -t and it tells me my ping to a certain server. How can I create a .bat file that automatically opens the cmd window and types it in so that I don't have to write it out every single time. I tried just putting in ping 216.52.241.254 -t and it just spams it over and over again.

Another way of doing this (potentially a lot easier for you) would be to create a shortcut:
Right Click in windows explorer and hover over "New"
Select "Shortcut"
A dialogue will pop-up. Enter the command you wish to utilize: ping 216.52.241.254 -t
Click Next and name the file.
Now whenever you open the shortcut, it will execute the command.
The advantage of this method over the other is its simpler and allows you to pin it to the Starmenu or Taskbar.
Mona.

It spammed it over and over because you called the batch file ping so it was launching itself.

Very simple:
#echo off
ping 216.52.241.254 -t
Echo.
pause
Open Notepad
Copy and paste this in.
Save as a .bat file, ensuring that you select "all files" option
Run the batch file any time you want to check your ping.
Done!

type the following:
cd\
ping -t 216.52.241.254

You must have keep the file name as 'ping.bat'.
Rename it to something else and it will definitely work.
ex : ping1.bat, something.bat etc., anything else but ping.

It spams it again and again because you used -t which "Pings the specified host until stopped." if you lose the -t it won't do that. Type Ping /? to look through all the options available when using ping and select what is appropriate for what you want it to do.

Related

Cant output batch file commands to txt file

i work as a network engineer and i would like to write some scripts to make my job more easy especially in situations where i cannot be on site and the client cannot connect to the internet so this is one script i made that would give me basic network test info
However my issue is i cant get it to output to a txt file after the script is ran, i want the user having the choice of weather to create the file or not. after i go through the prompt and click yes it will pause for a bit ( i assume its running the commands) however no file is made on my desktop where the file is stored.
At my work i am running windows 10, and at home xp but i need this to be able to run on either.
here is a copy of what i am having trouble with i have more questions but would first like to get this fixed
i have tried using >>output.txt (commands in here) i also tried command >> output.txt but either way there is no file on my desktop
>>output.txt (ipconfig /all ping google.com tracert google.com)
The file will be saved to your current directory. You'd need to use "%userprofile%\desktop\output.txt" as the file destination to put it on the desktop. You may need the quotes in case the resolution ofuserprofile` contains separators.
I think you have three (3) commands here and need to tell the shell to do them all.
>>"%USERPROFILE%\Desktop\output.txt" (ipconfig /all & ping google.com & tracert google.com)
Alternatively, the .bat script might look like:
>>"%USERPROFILE%\Desktop\output.txt" (
ipconfig /all
ping google.com
tracert google.com
)

How to avoid command window popping on cmd.exe

I have a command as:
cmd.exe /c ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here
But when executed it opens a command window. Is there a way to avoid the command window from popping up?
PS: I cannot remove cmd.exe from there. As you can see, I am trying to tie two commands one after the other in the same string.
Thanks.
Edit: Sorry. Its not a bat file. I want to execute 2 commands in the "UninstallString" of msiexec. I was trying so many things that my question got a bit carried away.
The command is:
msiexec <product> <package> && reg delete /xxx
The simplest option is to start the thing minimized. Shortcut Target:
cmd /c START /MIN \path\to\test.bat
or
cmd /c START /MIN cmd /k ( ping 1.1.1.1 -w 10000 -n 1 && #ECHO All OK)
It's not hidden or anything, but it doesn't show up on the desktop or—worse—steal the focus.
If you want the window to go away on its own, "cmd /c ..." will make that happen. "cmd /k ..." will leave the window open.
Plan B is referred to by #CodyGray in the SU link he posted. There are programs that don't open windows, like wperl, pythonw, or wscript (natively available on Windows). If you can pass your command through to one of those things, then you could effectively double-click an icon and have it run "silently."
If Perl's available, I'd certainly go with that because you can craft some pretty powerful one-liners that won't require creating other files.
wperl -MWin32 -MNet::Ping -e "$p=Net::Ping->new('icmp',10000); if ($p->ping('192.168.1.1')) { Win32::MsgBox('Ping Successful', 1 + MB_OK, 'All Good'); }"
In your example, you're chaining commands together, the latter is a notification. If you don't want to have a window open for the first command, it would be awkward to do it for the second when you're notifying the user of something. Having the process call "cmd /c start cmd /c #echo Everything's OK" would probably do it, but using CMD windows for user notification is probably not something the HCI guys would smile at.
No, all batch files open in command-line windows; this has nothing to do with the presence of cmd.exe in your particular file. A batch file is simply a number of command-line commands, one per line.
I don't understand why you write test.bat the way you do. I'd rather expect
ping 1.1.1.1 -n 1 -w 10000
echo second command goes here
If, for some bizzare reason, you really need to use only a single line, you can simply do
ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here
As Andreas Rejbrand already explained, the command prompt window is not from the explicit cmd.exe invocation within your script but from executing the .bat script itself. (And despite your claim, you haven't provided any evidence why explicitly invoking cmd.exe is necessary. The whole point of a .bat script is to batch commands together.)
That said, the silentbatch program that Paul Miner and I wrote can execute batch scripts and suppress the command prompt window. To use it, you would have to create a Windows shortcut that invokes silentbatch.exe test.bat and double-click on that rather than double-clicking on test.bat directly, however.
You could also create a shortcut to you batch script and then in the properties select to start the app minimized. It is explained here: CNET: How to automatically start a program minimized in Windows
Step 1: Right-click on the shortcut of the program you want to start minimized and select Properties.
Step 2: Click on the drop-down menu under Run.
Step 3: Select "Minimized," then click the OK button.
From win cmd:
start /b cmd.exe /c "ping 1.1.1.1 -n 1 -w 10000 & echo second command goes here"
runs both commands without opening another cmd window, leaving parent window unblocked for further commands without waiting.

make a file that will run in cmd

is it possible to make a file that when double clicked, it will run "ping 8.8.8.8 -t" to Windows Command Prompt automatically.
so dont need to "Windows+R > ping 8.8.8.8 -t > enter" to do that
do i need a compiler to do that?
Yes, make a .bat file with your text editor (the flat text one) and paste the command in that file. Such as:
ping 8.8.8.8 -t
Be sure to save it as .bat and not as .txt.

How do I make a hibernation batch file?

I'm trying to make a batch file that executes a simple command:
shutdown -h
I've created a shutdown.bat file with that exact line in it, yet when I run it, all it does is spam the command prompt like crazy.
I've looked at batch file sites, #echo off seems popular, but doesn't that just hide output while the commands are executed just the same?
It turns out that adding a change directory command to the root of the drive fixes the problem. The final text that I ended up using in the .bat file was:
cd c:\
shutdown /h
I believe I am very late for this but just in case someone else comes across this.
The issue was indeed with the name of the bat file. You were calling shutdown -h and the bat file was called shutdown.bat hence the bat file was calling itself causing the loop of the command prompts. To fix this you either rename the bat file or specify the directory where shutdown is located.
Would love to say I figured this out but I simply googled it.
The code you need is
%windir%System32rundll32.exe powrprof.dll,SetSuspendState
If you go to start -> run and then type this in it should work.
So if you have hibernate enabled in the poweroptions this should also work in a batch file.
Hope this helped you
Edit:
P.s. click the white little V under the arrows (left of this answer) to accept the answer ;)
For shutdown:
c:\windows\system32\shutdown -s -f -t 00
(or do ...shutdown -p -f).
E.g.: set the time -t 1000 and save and run it.
To abort just c:\windows\system32\shutdown -a in different batch file.
Very important point to note: if you locate this batch file in your startup then it will execute the s/h/r immediately. E.g. you create a logoff batch file and you locate it in startup
it will logoff the pc within the given time/immediately. However, when you hold shift when logging then it will abort the logoff batch file otherwise you pc will logoff again and again. You don't have to do this I am not sure if it works on every PC.
Create a user and try it there in case you could not log in. good luck
For restart:
c:\windows\system32\shutdown -r -t 00
For hibernate:
c:\windows\system32\shutdown /h
Reference: https://www.instructables.com/id/Shutdown-restart-or-hibernate-your-computer-on-a/

Make windows batch file not close upon program exit

When the program is over, I want it to say "Press any key to continue..." so I can scroll thru the output.
I believe you are looking for the command "pause". It should ask you to press any key.
You can even appear to change the prompt. Instead of just using the pause statement, you can:
echo "Your message here"
pause > nul
This gets rid of the original pause message and inserts yours.
Jacob
In Windows/DOS batch files:
pause
This prints a nice Press any key to continue . . . message
Or, if you don't want to show anything message, do this instead:
pause >nul
Create a shortcut to your batch file.
Right click on the file and select "Properties".
In the tab "Shortcut" is the target, something like this:
C:\folder\file.bat
Change it by this one:
C:\Windows\System32\cmd.exe /K "C:\folder\file.bat"
where "C:\Windows\System32" is the folder where cmd.exe is located, which may be another according to your Windows installation.
Then you can run the shortcut.
A part of me says that "pause" in the batch file should also do the trick. But also give the /K switch a try as well.
HTH
If you want the console to remain open, you can add the following at the end of batch file -
call cmd
This will open console with in the same one with all your environment variables set in your batch file and you can work in it.
you need to type in pause, which when you make it to the end, it should say
Press any key to continue . . .
but only if you put it at the end, because if you don't, it will pause it at the place you put it. Don't try '/k' because it doesn't work

Resources