I'm trying to write a short batch file to monitor the data traffic hourly and write it in TXT files. My problem is that I want to give the TXT files the date and time of their creation as their filename.
I already tried to use the %date% and %time% commands, the "date /t" and "time /t" commands or to generate a file beforehand and access it afterwards, but I'm simply not well-versed enough in batch programming to access this file.
netstat -e > C:\Users\User1\Documents\%date%.%time%.txt exit
There should be a file with the name of e.g. "20.02.2016 , 12:06:12.txt" , but I'm either getting a &date file or an error in cmd: "The syntax for filename, directory name, or volume label is incorrect.".
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set filedate=%%c%%a%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set filetime=%%a%%b)
netstat -e > C:\Users\User1\Documents\%filedate=%.%filetime%.txt exit
The first for loop does date /t then is set "/" as a delimiter and reassembles the date without a delimiter.
The second for loop does time /t then using ":" as a delimiter and reassembles the time without a delimiter.
Then we do your netstat command, placing our variables into the filename.
This will not work for all locales, but the general idea is the same. If you live a locale where this solution does not work, then just adapt the code to your own needs. This works for me on my own system, in North America.