朋友有個需求如下 : (很久沒寫了, 居然花了3小時才搞定他)
可以在批次啟動時,檔案是用時間當檔名
然後執行一個ping的指令
在批次啟動時,檔案是用時間當檔名
所以在執行的時候,可以建立一個新的檔案
然後再把ping的結果寫進去檔案裡
檔名規則
年-月-日_時間
副檔名 : .log or txt
P.S 有需要請自行取用
@echo off
setlocal
:: FQDN Address
set "FQDN=127.0.0.1"
:: tempLog location
set "LOG=log.txt"
:: Date/Time Define
set "FULLDATE=%date:~0,4%-%date:~5,2%-%date:~8,2%"
set "FULLTIME=%time:~0,2%%time:~3,2%%time:~6,2%"
:: Write Ping result in templog
>"%LOG%" ping -n 5 -w 1000 %FQDN%
:: rename file-name to date-time format
rename "%LOG%" %FULLDATE%_%FULLTIME%.log
請問為何不直接命名而要用更名的方式?
因為For看起來似乎用不到
感謝 Dream 大大的提醒
已做修正~
交流一下
<pre class="c" name="code">
@echo off
setlocal
:: FQDN Address
set "FQDN=127.0.0.1"
:: tempLog location
set "LOG=log.txt"
:: Date/Time Define
set "FULLDATE=%date:~0,4%-%date:~5,2%-%date:~8,2%"
set "FULLTIME=%time:~0,2%%time:~3,2%%time:~6,2%"
:: Write Ping result in templog
>" %FULLDATE%_%FULLTIME%.log" ping -n 5 -w 1000 %FQDN%
底下用不到
<pre class="c" name="code">
:: rename file-name to date-time format
rename "%LOG%" %FULLDATE%_%FULLTIME%.log
或像底下這樣較易閱讀
<pre class="c" name="code">
setlocal
:: FQDN Address
set "FQDN=127.0.0.1"
:: Date/Time Define
set FULLDATE=%date:~0,10%
set FULLTIME=%time:~0,8%
:: Write Ping result in templog
>"%FULLDATE:/=-%_%FULLTIME::=%.log" ping -n 5 -w 1000 %FQDN%
感謝分享...
再加入可輸入IP位置.
<pre class="c" name="code">@echo off
setlocal
:: FQDN Address
set "FQDN=192.168.1.254"
set /p FQDN=請輸入IP位置,如127.0.0.1:
rem if %FQDN% == "" echo 您輸入位置: %FQDN%
rem pause
echo 請稍後產生log檔.....
:: tempLog location
set "LOG=log.txt"
:: Date/Time Define
set FULLDATE=%date:~0,10%
set FULLTIME=%time:~0,8%
:: Write Ping result in templog
>"%FULLDATE:/=-%_%FULLTIME::=%.log" ping -n 5 -w 1000 %FQDN%
...偶覺得Batch 應該是自動的啦~ 加上了可輸入 IP
那就用 ping [xxx.xxx.xxx.xxx] >> xxx.log 就好了