0

I'm currently writing a batch files that records the ping of an ip and records the time. Unfortunately when i try to use the %time% command and store it, it always returns nothing.

when i run the command alone it works, just not with my other code

Set packet=0
Set Down=0
Set Up=0
Set time=0
SET /P IP=enter ip.
set counter=0
:ping
Rem ping -n 1 -w 2500 %IP%>>ping.txt
ping -n 1 -w 2500 %IP% > nul 2>&1 && set Test=Pass || set Test=Fail
FOR /F "tokens=1-9 delims==< " %%a IN ('PING -n 1 -w 2500 %IP%') DO IF "%%h"=="TTL" SET RESPONSE=%%g 
mode 100
Set time= %TIME:~0,5%
echo %time%
pause
Albatross
  • 19
  • 1
  • 3
    Don't use the name of a system variable to store a local variable. – SomethingDark Dec 28 '17 at 23:45
  • 3
    Type `set` to see all set variables and `set /?` (at the end) for the names of the volatile variables (that change like `%time%` does). – ACatInLove Dec 28 '17 at 23:50
  • Read answer on [Defining a variable in a batch file not working](https://stackoverflow.com/a/47930012/3074564) containing the relevant part of help of command __SET__ output on running `set /?` in a command prompt window about __dynamic environment variables__. It is clearly described in help that a line like `Set time=0` overwrites dynamic variable `TIME` making it not further useful as you would like. See also [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) – Mofi Dec 29 '17 at 19:17

1 Answers1

1

Let me reiterate what @SomethingDark already said.

And that is precisely what is causing your problem, along with the fact that you have a space after the = sign in: Set time= %TIME:~0,5%

This prefixes a space before the current time, and the value is saved as ' 17:3' (5 characters), for example. Next time you run the same command, the value stored in the time variable will be ' 17:' (with 2 spaces) and so on. Eventually, you will end up with 5 spaces being saved in the time variable.

senpai
  • 74
  • 5
  • so how would i make the variable local? I removed the space and im still having the same issue – Albatross Dec 29 '17 at 19:56
  • The links in @Mofi's comment above may have helped you with this issue, already. If not, would you mind posting a latest version of your script? I have a simple script that look like this and works fine, each time I run it: `SET MY_TIME=%TIME:~0,5%` `echo %MY_TIME%` – senpai Jan 04 '18 at 19:34
  • I removed the space and added the extra "%" on each side but now it just returns %TIME:~0,6% Set time=%%TIME:~0,6%% echo %time% pause – Albatross Jan 12 '18 at 19:12
  • Is there a reason why you added those extra **%**s? Also, as suggested earlier, please stop using `set time`. Just give that variable a different name, as in, `CURR_TIME` or something like that. Any particular reason for changing to `0,6`? That will give you an extra colon at the end. Please follow the code that I had shown previously and let me know if it still does not work for you. Here is some additional explanation: `echo %TIME%` displays the current time. It displays 11:48:08.58, for example. You needed a sub-string of that, just HH:MM, which is what `%TIME:~0,5%` returns. – senpai Jan 12 '18 at 22:55
  • In one of the links above stated it need 2 "%" on each side cause it was in a batch file, i put the 6 in cause i was trying to get this thing to work, I changed the variable name to potato and it still returns 0 but for some reason %date% works with no issues. Set potato=%TIME:~0,5% echo %potato% pause set date=%date% echo %date% echo %time% pause Set /? – Albatross Jan 15 '18 at 17:13
  • Ok i fixed the issue, looks like i had a variable at the top that was also named time that was set to 0 so i guess it was pulling that. thanks for all the help guys. – Albatross Jan 15 '18 at 18:58