windows batch / windows cmd循环参数与变量

时间:2015-12-03 10:38:18

标签: batch-file cmd

我正在尝试使用参数中的变量制作Windows(7)批处理脚本,并且我一直在收到错误,是否有人知道如何解决此问题?

@ECHO OFF
::::   settings   ::::
SET "InputFile=D:\somefile.ext"
SET "linesToSkipCharacter="
SET "endOfLineCharacter="
SET "RowNoToRead=1"
SET "delimiterCharacter=;"
::::::::::::::::::::::

:: make shore the variables are emty
SET "linesToSkip=" "endOfLine=" "tokens=" "delims="

:: if set put parameter into variable
if not "%delimiterCharacter%"=="" SET "delims=delims=%delimiterCharacter% "
if not "%linesToSkipCharacter%"=="" if not "%linesToSkipCharacter%"=="0" SET "linesToSkip=skip=%linesToSkipCharacter% "
if not "%endOfLineCharacter%"=="" if not "%endOfLineCharacter%"=="0" SET "endOfLine=eol=%endOfLineCharacter% "
if not "%RowNoToRead%"=="" if not "%RowNoToRead%"=="0" SET "tokens=tokens=%RowNoToRead%"

:: loop File with the given parameters 
fOR /F "%delims%%linesToSkip%%endOfLine%%tokens%" %%A IN (%InputFile%) DO echo do something with %%A

1 个答案:

答案 0 :(得分:0)

您错误地将linesToSkip设置为" "endOfLine=" "tokens=" "delims= 只有SET /a可用于在逗号分隔列表中初始化多个变量,但在这种情况下不能,因为没有任何内容可以分配/计算。

使用正确的语法:

SET "linesToSkip=" & SET "endOfLine=" & SET "tokens=" & SET "delims="

P.S。删除@echo off并从命令提示符控制台运行批处理文件以查看错误。