for / f"令牌*" %% a in(echo variable>> anotherfile.csv)do(2>> failprotocol.log)

时间:2016-10-14 09:05:04

标签: batch-file

我实际上几乎陷入了一个我无法解决的问题: 我做了一个ping,它每秒都对服务器执行ping操作,然后将其写入CSV文件。

问题是:当文件打开时,它无法访问,并且没有数据写入我的文件。

我尝试提取错误"该进程无法访问该文件..."将数据发送到另一个文件。

for /f "tokens*" %%a in ('echo %t2%;%t3%;%late2%>>%USERPROFILE%\Desktop\Dauerping.csv') do (2>>%TEMP%\Dauerfail.log)

我试图获得"过程......"的任何错误级别,但它不会发送任何错误级别。它使标准差保持在1。

echo %errorlevel%>>dauerping.csv 2>Nul

.csv-File是打开的,因此它得到错误,该进程无法访问该文件等等,但是Errorlevel保持与以前相同并且仍然显示错误而不是送到涅。。


有没有办法让我的Batchfile看到这个错误并将我的数据发送到另一个.csv文件以减少信息丢失? 有没有办法让那些">>"进入for循环作为" in-expression"?

非常感谢你的帮助!

完整代码:

@echo off
setlocal enableDelayedExpansion
chcp 1252>nul
set ue=ü
set ae=ä
set oe=ö
set Uue=Ü
set Aae=Ä
set Ooe=Ö
set ss=ß
chcp 850>nul
del %TEMP%\Dauerping.log /s /q>Nul 2>Nul
del %TEMP%\Dauertime.log /s /q>Nul 2>nul
del %TEMP%\Dauerfail.log /s /q>Nul 2>nul
echo Dauerping l%ae%uft!
set srv=srv-file-02
:Schleife
ping -n 1 %srv% | find "Antwort von" >>%TEMP%\Dauerping.log
for /f "tokens=*" %%i in ('ping -n 1 %srv%') do set fehler=%%i
set fail=0
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% >>%TEMP%\Dauertime.log
FOR /f "tokens=1,3,4,5 " %%f  IN (%TEMP%\Dauerping.log) DO set late=%%i
FOR /f "tokens=1,2" %%a IN (%TEMP%\Dauertime.log) DO set t=%%a %%b
set t2=%t:~0,10%
set t3=%t:~11,19%
set late2=%late:~5,9%
echo %fehler%|findstr "nicht finden">nul && set fail=1
if %fail%==1 echo Fehler && set late2=Fehler
echo %t2%;%t3%;%late2%>>%USERPROFILE%\Desktop\Dauerping.csv
for /f "tokens*" %%a in ('echo %t2%;%t3%;%late2%>>%USERPROFILE%\Desktop\Dauerping.csv') do (2>>%TEMP%\Dauerfail.log)
Pause>Nul
if %errorlevel%==1 echo %t2%;%t3%;%late2%>>%USERPROFILE%\Desktop\Dauerpingproc.csv
waitfor test /t:1 2>Nul 1>Nul
GOTO Schleife

工作脚本:

@echo off
chcp 1252>nul
set ue=ü
set ae=ä
set oe=ö
set Uue=Ü
set Aae=Ä
set Ooe=Ö
set ss=ß
chcp 850>nul
del %TEMP%\Dauerping.log /s /q>Nul 2>Nul
del %TEMP%\Dauertime.log /s /q>Nul 2>nul
echo Dauerping l%ae%uft!
set srv=srv-file-02
:Schleife
ping -n 1 %srv% | find "Antwort von" >>%TEMP%\Dauerping.log
for /f "tokens=*" %%i in ('ping -n 1 %srv%') do set fehler=%%i
set fail=0
set fail2=0
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% >>%TEMP%\Dauertime.log
FOR /f "tokens=1,3,4,5 " %%f  IN (%TEMP%\Dauerping.log) DO set late=%%i
FOR /f "tokens=1,2" %%a IN (%TEMP%\Dauertime.log) DO set t=%%a %%b
set t2=%t:~0,10%
set t3=%t:~11,19%
set late2=%late:~5,9%
echo %fehler%|findstr "nicht finden">nul && set fail=1
if %fail%==1 echo Fehler && set late2=Fehler
echo %t2%;%t3%;%late2%>>%USERPROFILE%\Desktop\Dauerpingproc.csv
waitfor test /t:1 2>Nul 1>Nul
GOTO Schleife

1 个答案:

答案 0 :(得分:0)

以下是对我认为您的脚本应该做的事情的猜测:

@echo off
setlocal

>nul chcp 1252
set ae=ä
>nul chcp 850

echo Dauerping l%ae%uft!

set "srv=srv-file-02"

:Schleife
set "late="
for /f "tokens=7 delims== " %%a in (
    'ping -n 1 %srv% ^|find "TTL"') do set "late=%%a"
if not defined late (set "late=Fehler" & echo Fehler)
>>"%USERPROFILE%\Desktop\Dauerping.csv" (
    echo %date%;%time:~,8%;%late%) 2>>"%USERPROFILE%\Desktop\Dauerpingproc.csv"
waitfor test /t:1 2>Nul 1>&2
goto :Schleife

如果不正确,请道歉,但我希望它可以帮助您找到解决方案。

如果您不打算输出,可能是:

:Schleife
set "late="
for /f "tokens=7 delims== " %%a in (
    'ping -n 1 %srv% ^|find "TTL"') do set "late=%%a"
if not defined late (set "late=Fehler" & echo Fehler)
echo %date%;%time:~,8%;%late% >>"%USERPROFILE%\Desktop\Dauerping.csv" 2>nul|| (
    echo %date%;%time:~,8%;%late% >>"%USERPROFILE%\Desktop\Dauerpingproc.csv")
waitfor test /t:1 2>Nul 1>&2
goto :Schleife