2015-12-03 100 views
0

我想使Windows(7)批處理腳本與參數中的變量,我不斷得到錯誤,是否有人知道如何解決這個問題?Windows批處理/ Windows cmd循環參數與變量

@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 
+2

你得到了什麼錯誤? – SomethingDark

回答

0

您錯誤地將linesToSkip設置爲" "endOfLine=" "tokens=" "delims=
只有SET /a可以用來在逗號分隔的列表中初始化多個變量,但在這種情況下不能用於分配/計算。

使用正確的語法:

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

附:刪除@echo off並從命令提示符控制檯運行批處理文件以查看錯誤。