如何使用FOR(令牌,delims)使用单独的Windows批处理文件?

时间:2018-09-17 03:54:48

标签: windows batch-file for-loop token

我想制作一个性能监控批处理程序。我自己做了插入VB。但问题是,我无法创建要插入的文件

  1. 转储性能监视文件
  2. 将文件处理为可插入的数据
  3. 通过FTP发送到服务器
  4. 插入数据

除了第二个我都做到了

有人能帮助您如何将数据与我想要的分离吗?

转储文件是这样的

------------------------------------------------
**# 20180917_CAFENOIR_PERF.cvs**
------------------------------------------------
"(PDH-CSV 4.0) (","\\CAFENOIR\Processor(_Total)\% Processor time","\\CAFENOIR\Memory\Available KBytes","\\CAFENOIR\LogicalDisk(C:)\% Free Space","\\CAFENOIR\LogicalDisk(D:)\% Free Space","\\CAFENOIR\LogicalDisk(E:)\% Free Space"
"09/17/2018 12:32:11.439"," ","2389340","73.031078258802481","99.758165860552879","92.077869960114995"
"09/17/2018 12:32:12.474","13.17071949707611","2393976","73.031078258802481","99.758165860552879","92.077869960114995"

我想制作这样的文件

------------------------------------------------
**# 20180917_12_CAFENOIR_perfmon.imp**
------------------------------------------------
CAFENOIR
"13.17071949707611"
"2393976"
"73.031078258802481"
"99.758165860552879"
"92.077869960114995"
"09/17/2018 12:32:12.474"

我尝试过这样的脚本,但它只输出计算机名

CAFENOIR

我希望任何人都能提供线索

添加了代码(对不起,我忘了)

for /F "tokens=1-6 skip=2 delims=," %%a IN ('type *.cvs') do (@echo %computername% %%b% %%c% %%d% %%e% %%f% %%a% > %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%computername%_perfmon.imp

2 个答案:

答案 0 :(得分:1)

看看您的答案,有很多方法可以解决它。您可能只运行了一个循环,就没有%%a%这样的东西了:

@echo off
for /F "tokens=1-6 skip=2 delims=," %%a IN ('type *.cvs') do (
    set "vDT=%%a"
    set "vCPU=%%b"
    set "vMEM=%%c" 
    set "vCfree=%%d"
    set "vDfree=%%e"
    set "vEfree=%%f"
echo %computername% %vCPU% %vMEM% %vCfree% %vDfree% %vEfree% %vDT% > %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%computername%_perfmon.imp
)

更好的是,即使您已经预定义了变量,您也无需设置变量:

@echo off
for /F "tokens=1-6 skip=2 delims=," %%a IN ('type *.cvs') do (
echo %computername% %%b %%c %%d %%e %%f %%a > %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%computername%_perfmon.imp
)

答案 1 :(得分:-1)

这很有趣。我为此挣扎了一个星期,但是在我发布问题之后,我自己做了

我希望它能对单独使用A文件参数的人有所帮助。

@SET vCPU   = none
@SET vMEM   = none
@SET vCfree = none
@SET vDfree = none
@SET vEfree = none
@SET vDT    = none
FOR /F "tokens=1 skip=2 delims=," %%a IN ('type *.cvs') DO @SET vDT=%%a%
FOR /F "tokens=2 skip=2 delims=," %%a IN ('type *.cvs') DO @SET vCPU=%%a%
FOR /F "tokens=3 skip=2 delims=," %%a IN ('type *.cvs') DO @SET vMEM=%%a%
FOR /F "tokens=4 skip=2 delims=," %%a IN ('type *.cvs') DO @SET vCfree=%%a%
FOR /F "tokens=5 skip=2 delims=," %%a IN ('type *.cvs') DO @SET vDfree=%%a%
FOR /F "tokens=6 skip=2 delims=," %%a IN ('type *.cvs') DO @SET vEfree=%%a%
@echo %vCPU%
@echo %vMEM%
@echo %vCfree%
@echo %vDfree%
@echo %vEfree%
@echo %vDT%
@echo %computername% %vCPU% %vMEM% %vCfree% %vDfree% %vEfree% %vDT% > %date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%_%computername%_perfmon.imp**