附加到现有批处理文件日志,但使用相同的批处理文件附加到其他列

时间:2016-05-26 20:04:16

标签: batch-file

我想运行我的脚本并创建一个日志文件。它做的。我想第二次运行该脚本并附加到同一个日志文件,但我希望它转到新列。更容易并排比较两个结果。下面是我的脚本到目前为止。先感谢您。

echo off
color a
cls
@echo Welcome to the Ping Scan CheckList Script v2
echo.
@echo This script is going to expect three octets, and test the fourth.
@echo You can break the store i.p. into 4 octets. I.E. 10.x.yy.zz where x and yy equal the store number.
@echo Please begin entering the first three now.
ECHO.
SET/p first=First Octet:
SET/p second=Second Octet:
SET/p third=Third Octet:
@ECHO %DATE% %TIME% >>%USERPROFILE%\DESKTOP\%second%%third%-%USERNAME%-%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%.csv
SET t=0
:start
SET /a t=t+1
if %t%==2 GoTo start
if %t%==4 GoTo start
if %t%==15 GoTo start
if %t%==18 GoTo start
if %t%==19 GoTo start
if %t%==34 GoTo start
if %t%==43 GoTo start
if %t%==44 GoTo start
if %t%==45 GoTo start
if %t%==47 GoTo start
if %t%==48 GoTo start
if %t%==55 GoTo start
if %t%==59 GoTo start
if %t%==61 GoTo start
if %t%==64 GoTo start
if %t%==67 GoTo start
if %t%==69 GoTo start
if %t%==73 GoTo start
if %t%==74 GoTo start
if %t%==87 GoTo start
if %t%==89 GoTo start
if %t%==98 GoTo start
if %t%==99 GoTo start
if %t%==100 GoTo start
if %t%==101 GoTo start
if %t%==102 GoTo start
if %t%==103 GoTo start
if %t%==104 GoTo start
if %t%==105 GoTo start
if %t%==106 GoTo start
if %t%==107 GoTo start
if %t%==108 GoTo start
if %t%==109 GoTo start
if %t%==110 GoTo start
if %t%==111 GoTo start
if %t%==112 GoTo start
if %t%==113 GoTo start
if %t%==114 GoTo start
if %t%==115 GoTo start
if %t%==116 GoTo start
if %t%==117 GoTo start
if %t%==118 GoTo start
if %t%==119 GoTo start
if %t%==120 GoTo start
if %t%==121 GoTo start
if %t%==122 GoTo start
if %t%==123 GoTo start
if %t%==124 GoTo start
if %t%==125 GoTo start
if %t%==126 GoTo start
if %t%==127 GoTo start
if %t%==128 GoTo start
if %t%==132 GoTo start
if %t%==133 GoTo start
if %t%==134 GoTo start
if %t%==158 GoTo start
if %t%==159 GoTo start
if %t%==166 GoTo start
if %t%==170 GoTo start
if %t%==179 GoTo start
if %t%==185 GoTo start
if %t%==191 GoTo start
if %t%==192 GoTo start
if %t%==194 GoTo start
if %t%==195 GoTo start
if %t%==198 GoTo start
if %t%==199 GoTo start
if %t%==202 GoTo start
if %t%==203 GoTo start
if %t%==204 GoTo start
if %t%==207 GoTo start
if %t%==208 GoTo start
if %t%==209 GoTo start
if %t%==212 GoTo start
if %t%==213 GoTo start
if %t%==214 GoTo start
echo Pinging .%t%
ping -w 100 -l 1 %first%.%second%.%third%.%t% > nul
if %errorlevel%==0 echo %first%.%second%.%third%.%t% is UP! >> %USERPROFILE%\DESKTOP\%second%%third%-%USERNAME%-%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%.csv
if %errorlevel%==1 echo ****%first%.%second%.%third%.%t%**** is DOWN! >> %USERPROFILE%\DESKTOP\%second%%third%-%USERNAME%-%DATE:~4,2%%DATE:~7,2%%DATE:~10,4%.csv
IF %t%==217 GoTo finish
Goto start
:finish
@echo The script is done, you can find the full log on your desktop
pause

1 个答案:

答案 0 :(得分:0)

首先,使用您想要的IP(最后一个八位字节)构建一个列表,如下所示:

IP
1
3
5
6
7
8
9
10
11
12
13
14
16
(and so on)

作为以下代码的基础:

@echo off
setlocal enabledelayedexpansion
break>b.csv
set "base=192.168.178"
for /f "tokens=1,*delims=," %%i in (a.csv) do (
 if "%%i"=="IP" (
   echo %%i,%%j,%time:~0,5%>>b.csv
 ) else (
   ping -n 1 -w 100 -4 %base%.%%i|find "TTL=" >nul && set "state=UP" || set "state=DOWN"
   echo %%i,%%j,!state!>>b.csv
 )
)
type b.csv>a.csv
type a.csv

我缩短了文件名和字符串以保持代码可读。

第一次迭代(两个连续的逗号)有一个小问题,可以用另一个if进行纠正 - 但是在下一次迭代时它会“自我纠正”。

注意:ping的错误级别不可靠,因此您应该查找“TTL =”。